-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.go
56 lines (49 loc) · 1.35 KB
/
statistics.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
package pinger4
import (
"context"
"sync"
"github.com/umenosuke/labelinglog"
)
func (thisPinger *Pinger) statistics(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
defer thisPinger.logger.Log(labelinglog.FlgDebug, "finish")
wgChild := sync.WaitGroup{}
defer wgChild.Wait()
defer thisPinger.cancelFunc()
for {
select {
case <-ctx.Done():
thisPinger.logger.Log(labelinglog.FlgDebug, "stop request received")
return
case result := <-thisPinger.chIcmpResult:
switch result.ResultType {
case IcmpResultTypeReceive:
thisPinger.addResult(result.IcmpTargetID, 1)
case IcmpResultTypeReceiveAfterTimeout:
case IcmpResultTypeTTLExceeded:
thisPinger.addResult(result.IcmpTargetID, 0)
case IcmpResultTypeTimeout:
thisPinger.addResult(result.IcmpTargetID, 0)
default:
thisPinger.addResult(result.IcmpTargetID, 0)
}
for _, ch := range thisPinger.chIcmpResultsSubscriber {
select {
case ch <- result:
default:
thisPinger.logger.Log(labelinglog.FlgWarn, "busy results subscriber skip")
}
}
}
}
}
func (thisPinger *Pinger) addResult(targetID BinIPv4Address, res int64) {
target := thisPinger.statisticsData.targets[targetID]
target.Lock()
defer target.Unlock()
target.Res[target.Index] = res
target.Index++
if target.Index >= thisPinger.config.StatisticsCountsNum {
target.Index = 0
}
}