-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_helper.go
45 lines (39 loc) · 1.01 KB
/
ping_helper.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
package quic
import (
"log"
"sync"
"time"
"github.com/ami-GS/gQUIC/qtype"
)
type PingHelper struct {
Ticker *time.Ticker
infoMutex *sync.Mutex
timePingSent map[qtype.PacketNumber]time.Time
pingDuration map[qtype.PacketNumber]time.Duration
}
func NewPingHelper(interval time.Duration) *PingHelper {
return &PingHelper{
Ticker: time.NewTicker(interval),
infoMutex: new(sync.Mutex),
timePingSent: make(map[qtype.PacketNumber]time.Time),
pingDuration: make(map[qtype.PacketNumber]time.Duration),
}
}
func (p *PingHelper) storeSendTime(pn qtype.PacketNumber) {
p.infoMutex.Lock()
defer p.infoMutex.Unlock()
p.timePingSent[pn] = time.Now()
}
func (p *PingHelper) calcPingDuration(pn qtype.PacketNumber) {
// TODO: not good for performance?
if timeSent, ok := p.timePingSent[pn]; ok {
p.infoMutex.Lock()
p.pingDuration[pn] = time.Now().Sub(timeSent)
delete(p.timePingSent, pn)
p.infoMutex.Unlock()
if LogLevel >= 0 {
// TODO: needs more f
log.Println(p.pingDuration)
}
}
}