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