forked from q191201771/lal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_session_stat.go
184 lines (155 loc) · 5.88 KB
/
basic_session_stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2022, Chef. All rights reserved.
// https://github.com/forkiss/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package base
import (
"github.com/forkiss/naza/pkg/connection"
"github.com/forkiss/naza/pkg/nazalog"
)
type IStatable interface {
GetStat() connection.Stat // TODO(chef): [refactor] 考虑为 nazanet.UdpConnection 实现这个接口
}
// BasicSessionStat
//
// 包含两部分功能:
// 1. 维护 StatSession 的一些静态信息
// 2. 计算带宽
//
// 计算带宽有两种方式,一种是通过外部的 connection.Connection 获取最新状态,一种是内部自己管理状态
//
type BasicSessionStat struct {
stat StatSession
prevConnStat connection.Stat
staleStat *connection.Stat
currConnStat connection.StatAtomic
}
// ---------------------------------------------------------------------------------------------------------------------
// NewBasicSessionStat
//
// @param remoteAddr: 如果当前未知,填入""空字符串
//
func NewBasicSessionStat(sessionType SessionType, remoteAddr string) BasicSessionStat {
var s BasicSessionStat
s.stat.typ = sessionType
s.stat.StartTime = ReadableNowTime()
s.stat.RemoteAddr = remoteAddr
// TODO(chef): [fix] 为customize pub添加 202205
switch sessionType {
case SessionTypeRtmpServerSession:
s.stat.SessionId = GenUkRtmpServerSession()
s.stat.BaseType = SessionBaseTypePubSubStr
s.stat.Protocol = SessionProtocolRtmpStr
case SessionTypeRtmpPush:
s.stat.SessionId = GenUkRtmpPushSession()
s.stat.BaseType = SessionBaseTypePushStr
s.stat.Protocol = SessionProtocolRtmpStr
case SessionTypeRtmpPull:
s.stat.SessionId = GenUkRtmpPullSession()
s.stat.BaseType = SessionBaseTypePullStr
s.stat.Protocol = SessionProtocolRtmpStr
case SessionTypeRtspPub:
s.stat.SessionId = GenUkRtspPubSession()
s.stat.BaseType = SessionBaseTypePubStr
s.stat.Protocol = SessionProtocolRtspStr
case SessionTypeRtspSub:
s.stat.SessionId = GenUkRtspSubSession()
s.stat.BaseType = SessionBaseTypeSubStr
s.stat.Protocol = SessionProtocolRtspStr
case SessionTypeRtspPush:
s.stat.SessionId = GenUkRtspPushSession()
s.stat.BaseType = SessionBaseTypePushStr
s.stat.Protocol = SessionProtocolRtspStr
case SessionTypeRtspPull:
s.stat.SessionId = GenUkRtspPullSession()
s.stat.BaseType = SessionBaseTypePullStr
s.stat.Protocol = SessionProtocolRtspStr
case SessionTypeFlvSub:
s.stat.SessionId = GenUkFlvSubSession()
s.stat.BaseType = SessionBaseTypeSubStr
s.stat.Protocol = SessionProtocolFlvStr
case SessionTypePsPub:
s.stat.SessionId = GenUkPsPubSession()
s.stat.BaseType = SessionBaseTypePubStr
s.stat.Protocol = SessionProtocolPsStr
}
return s
}
func (s *BasicSessionStat) SetBaseType(baseType string) {
s.stat.BaseType = baseType
}
func (s *BasicSessionStat) SetRemoteAddr(addr string) {
s.stat.RemoteAddr = addr
}
// ---------------------------------------------------------------------------------------------------------------------
func (s *BasicSessionStat) AddReadBytes(n int) {
s.currConnStat.ReadBytesSum.Add(uint64(n))
}
func (s *BasicSessionStat) AddWriteBytes(n int) {
s.currConnStat.WroteBytesSum.Add(uint64(n))
}
func (s *BasicSessionStat) UpdateStat(intervalSec uint32) {
s.updateStat(s.currConnStat.ReadBytesSum.Load(), s.currConnStat.WroteBytesSum.Load(), s.stat.BaseType, intervalSec)
}
func (s *BasicSessionStat) UpdateStatWitchConn(conn IStatable, intervalSec uint32) {
currStat := conn.GetStat()
s.updateStat(currStat.ReadBytesSum, currStat.WroteBytesSum, s.stat.BaseType, intervalSec)
}
func (s *BasicSessionStat) GetStat() StatSession {
s.stat.ReadBytesSum = s.currConnStat.ReadBytesSum.Load()
s.stat.WroteBytesSum = s.currConnStat.WroteBytesSum.Load()
return s.stat
}
func (s *BasicSessionStat) GetStatWithConn(conn IStatable) StatSession {
connStat := conn.GetStat()
s.stat.ReadBytesSum = connStat.ReadBytesSum
s.stat.WroteBytesSum = connStat.WroteBytesSum
return s.stat
}
func (s *BasicSessionStat) IsAlive() (readAlive, writeAlive bool) {
return s.isAlive(s.currConnStat.ReadBytesSum.Load(), s.currConnStat.WroteBytesSum.Load())
}
func (s *BasicSessionStat) IsAliveWitchConn(conn IStatable) (readAlive, writeAlive bool) {
currStat := conn.GetStat()
return s.isAlive(currStat.ReadBytesSum, currStat.WroteBytesSum)
}
// ---------------------------------------------------------------------------------------------------------------------
func (s *BasicSessionStat) BaseType() string {
return s.stat.BaseType
}
func (s *BasicSessionStat) UniqueKey() string {
return s.stat.SessionId
}
// ---------------------------------------------------------------------------------------------------------------------
func (s *BasicSessionStat) updateStat(readBytesSum, wroteBytesSum uint64, typ string, intervalSec uint32) {
rDiff := readBytesSum - s.prevConnStat.ReadBytesSum
s.stat.ReadBitrate = int(rDiff * 8 / 1024 / uint64(intervalSec))
wDiff := wroteBytesSum - s.prevConnStat.WroteBytesSum
s.stat.WriteBitrate = int(wDiff * 8 / 1024 / uint64(intervalSec))
switch typ {
case SessionBaseTypePubStr, SessionBaseTypePullStr:
s.stat.Bitrate = s.stat.ReadBitrate
case SessionBaseTypeSubStr, SessionBaseTypePushStr:
s.stat.Bitrate = s.stat.WriteBitrate
default:
nazalog.Errorf("invalid session base type. type=%s", typ)
}
s.prevConnStat.ReadBytesSum = readBytesSum
s.prevConnStat.WroteBytesSum = wroteBytesSum
}
func (s *BasicSessionStat) isAlive(readBytesSum, wroteBytesSum uint64) (readAlive, writeAlive bool) {
if s.staleStat == nil {
s.staleStat = new(connection.Stat)
s.staleStat.ReadBytesSum = readBytesSum
s.staleStat.WroteBytesSum = wroteBytesSum
return true, true
}
readAlive = !(readBytesSum-s.staleStat.ReadBytesSum == 0)
writeAlive = !(wroteBytesSum-s.staleStat.WroteBytesSum == 0)
s.staleStat.ReadBytesSum = readBytesSum
s.staleStat.WroteBytesSum = wroteBytesSum
return
}