forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet_handler_map.go
74 lines (61 loc) · 1.52 KB
/
packet_handler_map.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
package quic
import (
"sync"
"time"
"github.com/lucas-clemente/quic-go/internal/protocol"
)
type packetHandlerMap struct {
mutex sync.RWMutex
handlers map[string] /* string(ConnectionID)*/ packetHandler
closed bool
deleteClosedSessionsAfter time.Duration
}
var _ packetHandlerManager = &packetHandlerMap{}
func newPacketHandlerMap() packetHandlerManager {
return &packetHandlerMap{
handlers: make(map[string]packetHandler),
deleteClosedSessionsAfter: protocol.ClosedSessionDeleteTimeout,
}
}
func (h *packetHandlerMap) Get(id protocol.ConnectionID) (packetHandler, bool) {
h.mutex.RLock()
sess, ok := h.handlers[string(id)]
h.mutex.RUnlock()
return sess, ok
}
func (h *packetHandlerMap) Add(id protocol.ConnectionID, handler packetHandler) {
h.mutex.Lock()
h.handlers[string(id)] = handler
h.mutex.Unlock()
}
func (h *packetHandlerMap) Remove(id protocol.ConnectionID) {
h.mutex.Lock()
h.handlers[string(id)] = nil
h.mutex.Unlock()
time.AfterFunc(h.deleteClosedSessionsAfter, func() {
h.mutex.Lock()
delete(h.handlers, string(id))
h.mutex.Unlock()
})
}
func (h *packetHandlerMap) Close() {
h.mutex.Lock()
if h.closed {
h.mutex.Unlock()
return
}
h.closed = true
var wg sync.WaitGroup
for _, handler := range h.handlers {
if handler != nil {
wg.Add(1)
go func(handler packetHandler) {
// session.Close() blocks until the CONNECTION_CLOSE has been sent and the run-loop has stopped
_ = handler.Close(nil)
wg.Done()
}(handler)
}
}
h.mutex.Unlock()
wg.Wait()
}