-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv.go
223 lines (177 loc) · 5.87 KB
/
recv.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package udp
import (
"fmt"
"log"
"github.com/qmsk/close/stats"
"time"
)
type RecvConfig struct {
ListenAddr string `long:"listen-addr" default:"0.0.0.0:1337"`
}
func (self RecvConfig) Apply() (*Recv, error) {
return NewRecv(self)
}
type Recv struct {
config RecvConfig
sockRecv SockRecv
statsChan chan stats.Stats
statsTick <-chan time.Time
}
type RecvState struct {
id ID
seq uint64
stats RecvStats
}
type RecvStats struct {
ID ID // from recv ID
Time time.Time // stats were init/reset
Duration time.Duration // stats were collected
PacketTime time.Time // time of most recent packet
PacketStart uint64 // stats for packets following this seq (non-inclusive)
PacketSeq uint64 // most recent packet
PacketSize uint // total size of received packets
PacketErrors uint // invalid packets
PacketCount uint // in-sequence packets
PacketSkips uint // skipped in-sequence packets
PacketDups uint // out-of-sequence packets
}
func (self RecvStats) StatsID() stats.ID {
return stats.ID{
Type: "udp_recv",
Instance: self.ID.String(),
}
}
func (self RecvStats) StatsTime() time.Time {
return self.Time
}
// check if we have any received packets to report on
func (self RecvStats) Valid() bool {
return (self.PacketSeq > 0)
}
// proportion of delivered packets, not counting reordered or duplicated packets
// this ratio only applies when .Valid()
func (self RecvStats) PacketWin() float64 {
return float64(self.PacketCount) / float64(self.PacketSeq - self.PacketStart)
}
// proportion of lost or reordered packets, not counting duplicates
// this ratio only applies when .Valid()
func (self RecvStats) PacketLoss() float64 {
return float64(self.PacketSkips) / float64(self.PacketSeq - self.PacketStart)
}
func (self RecvStats) StatsFields() map[string]interface{} {
fields := map[string]interface{} {
"packet_errors": self.PacketErrors,
"packets": self.PacketCount,
"packet_skips": self.PacketSkips,
"packet_dups": self.PacketDups,
}
if self.Valid() {
fields["packet_win"] = self.PacketWin()
fields["packet_loss"] = self.PacketLoss()
}
return fields
}
func (self RecvStats) String() string {
packetOffset := self.PacketSeq - self.PacketStart
packetRate := float64(self.PacketCount) / self.Duration.Seconds()
packetThroughput := float64(self.PacketSize) / 1000 / 1000 * 8 / self.Duration.Seconds()
packetLoss := 1.0 - float64(self.PacketCount) / float64(packetOffset)
return fmt.Sprintf("%5.2f: recv %10d / %10d = %10.2f/s %8.2fMb/s @ %6.2f%% loss",
self.Duration.Seconds(),
self.PacketCount, packetOffset,
packetRate, packetThroughput,
packetLoss * 100.0,
)
}
func NewRecv(config RecvConfig) (*Recv, error) {
recv := &Recv{}
if err := recv.apply(config); err != nil {
return nil, err
}
return recv, nil
}
func (self *Recv) String() string {
return fmt.Sprintf("%v", self.sockRecv)
}
func (self *Recv) Config() *RecvConfig {
return &self.config
}
func (self *Recv) apply(config RecvConfig) error {
sockUDP := &SockUDP{}
if err := sockUDP.initListen(config.ListenAddr); err != nil {
return err
}
self.sockRecv = sockUDP
// config
self.config = config
return nil
}
func (self *Recv) StatsWriter(statsWriter *stats.Writer) error {
self.statsTick = statsWriter.IntervalTick()
self.statsChan = statsWriter.StatsWriter()
return nil
}
func (self *Recv) Run() error {
recvStates := make(map[ID]*RecvState)
recvChan := self.sockRecv.recvChan()
for {
select {
case packet := <-recvChan:
if recvState, exists := recvStates[packet.Payload.ID]; !exists {
recvStates[packet.Payload.ID] = self.makeState(packet)
} else {
recvState.handlePacket(packet)
}
case <- self.statsTick:
for id, recvState := range recvStates {
if recvState.alive() {
self.statsChan <- recvState.takeStats()
} else {
// cleanup
delete(recvStates, id)
}
}
}
}
return nil
}
// First packet received for Payload.ID
func (self *Recv) makeState(packet Packet) *RecvState {
log.Printf("Start from %v:%v: %v@%v\n", packet.SrcIP, packet.SrcPort, packet.Payload.ID, packet.Payload.Seq)
// skip first packet
return &RecvState{
id: packet.Payload.ID,
seq: packet.Payload.Seq,
stats: RecvStats{
ID: packet.Payload.ID,
Time: time.Now(),
PacketStart: packet.Payload.Seq,
},
}
}
func (self *RecvState) handlePacket(packet Packet) {
self.stats.PacketTime = time.Now()
if packet.Payload.Seq > self.seq {
self.stats.PacketSeq = packet.Payload.Seq
self.stats.PacketSkips += uint(packet.Payload.Seq - self.seq - 1) // normally 0 if delivered in sequence
self.stats.PacketCount++
self.stats.PacketSize += packet.PayloadSize
self.seq = packet.Payload.Seq
} else {
log.Printf("Skip %v <= %v\n", packet.Payload.Seq, self.seq)
self.stats.PacketDups++
}
}
func (self *RecvState) alive() bool {
return self.stats.PacketSeq > self.stats.PacketStart
}
func (self *RecvState) takeStats() RecvStats {
stats := self.stats
stats.Duration = time.Since(stats.Time)
self.stats = RecvStats{
ID: self.id,
Time: time.Now(),
PacketStart: self.seq,
}
return stats
}