-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
session_manager.go
196 lines (168 loc) · 5.27 KB
/
session_manager.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
package webtransport
import (
"context"
"sync"
"time"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/quic-go/quicvarint"
)
// session is the map value in the conns map
type session struct {
created chan struct{} // is closed once the session map has been initialized
counter int // how many streams are waiting for this session to be established
conn *Session
}
type sessionManager struct {
refCount sync.WaitGroup
ctx context.Context
ctxCancel context.CancelFunc
timeout time.Duration
mx sync.Mutex
conns map[quic.ConnectionTracingID]map[sessionID]*session
}
func newSessionManager(timeout time.Duration) *sessionManager {
m := &sessionManager{
timeout: timeout,
conns: make(map[quic.ConnectionTracingID]map[sessionID]*session),
}
m.ctx, m.ctxCancel = context.WithCancel(context.Background())
return m
}
// AddStream adds a new bidirectional stream to a WebTransport session.
// If the WebTransport session has not yet been established,
// it starts a new go routine and waits for establishment of the session.
// If that takes longer than timeout, the stream is reset.
func (m *sessionManager) AddStream(connTracingID quic.ConnectionTracingID, str quic.Stream, id sessionID) {
sess, isExisting := m.getOrCreateSession(connTracingID, id)
if isExisting {
sess.conn.addIncomingStream(str)
return
}
m.refCount.Add(1)
go func() {
defer m.refCount.Done()
m.handleStream(str, sess)
m.mx.Lock()
defer m.mx.Unlock()
sess.counter--
// Once no more streams are waiting for this session to be established,
// and this session is still outstanding, delete it from the map.
if sess.counter == 0 && sess.conn == nil {
m.maybeDelete(connTracingID, id)
}
}()
}
func (m *sessionManager) maybeDelete(connTracingID quic.ConnectionTracingID, id sessionID) {
sessions, ok := m.conns[connTracingID]
if !ok { // should never happen
return
}
delete(sessions, id)
if len(sessions) == 0 {
delete(m.conns, connTracingID)
}
}
// AddUniStream adds a new unidirectional stream to a WebTransport session.
// If the WebTransport session has not yet been established,
// it starts a new go routine and waits for establishment of the session.
// If that takes longer than timeout, the stream is reset.
func (m *sessionManager) AddUniStream(connTracingID quic.ConnectionTracingID, str quic.ReceiveStream) {
idv, err := quicvarint.Read(quicvarint.NewReader(str))
if err != nil {
str.CancelRead(1337)
}
id := sessionID(idv)
sess, isExisting := m.getOrCreateSession(connTracingID, id)
if isExisting {
sess.conn.addIncomingUniStream(str)
return
}
m.refCount.Add(1)
go func() {
defer m.refCount.Done()
m.handleUniStream(str, sess)
m.mx.Lock()
defer m.mx.Unlock()
sess.counter--
// Once no more streams are waiting for this session to be established,
// and this session is still outstanding, delete it from the map.
if sess.counter == 0 && sess.conn == nil {
m.maybeDelete(connTracingID, id)
}
}()
}
func (m *sessionManager) getOrCreateSession(connTracingID quic.ConnectionTracingID, id sessionID) (sess *session, existed bool) {
m.mx.Lock()
defer m.mx.Unlock()
sessions, ok := m.conns[connTracingID]
if !ok {
sessions = make(map[sessionID]*session)
m.conns[connTracingID] = sessions
}
sess, ok = sessions[id]
if ok && sess.conn != nil {
return sess, true
}
if !ok {
sess = &session{created: make(chan struct{})}
sessions[id] = sess
}
sess.counter++
return sess, false
}
func (m *sessionManager) handleStream(str quic.Stream, sess *session) {
t := time.NewTimer(m.timeout)
defer t.Stop()
// When multiple streams are waiting for the same session to be established,
// the timeout is calculated for every stream separately.
select {
case <-sess.created:
sess.conn.addIncomingStream(str)
case <-t.C:
str.CancelRead(WebTransportBufferedStreamRejectedErrorCode)
str.CancelWrite(WebTransportBufferedStreamRejectedErrorCode)
case <-m.ctx.Done():
}
}
func (m *sessionManager) handleUniStream(str quic.ReceiveStream, sess *session) {
t := time.NewTimer(m.timeout)
defer t.Stop()
// When multiple streams are waiting for the same session to be established,
// the timeout is calculated for every stream separately.
select {
case <-sess.created:
sess.conn.addIncomingUniStream(str)
case <-t.C:
str.CancelRead(WebTransportBufferedStreamRejectedErrorCode)
case <-m.ctx.Done():
}
}
// AddSession adds a new WebTransport session.
func (m *sessionManager) AddSession(qconn http3.Connection, id sessionID, requestStr http3.Stream) *Session {
conn := newSession(id, qconn, requestStr)
connTracingID := qconn.Context().Value(quic.ConnectionTracingKey).(quic.ConnectionTracingID)
m.mx.Lock()
defer m.mx.Unlock()
sessions, ok := m.conns[connTracingID]
if !ok {
sessions = make(map[sessionID]*session)
m.conns[connTracingID] = sessions
}
if sess, ok := sessions[id]; ok {
// We might already have an entry of this session.
// This can happen when we receive a stream for this WebTransport session before we complete the HTTP request
// that establishes the session.
sess.conn = conn
close(sess.created)
return conn
}
c := make(chan struct{})
close(c)
sessions[id] = &session{created: c, conn: conn}
return conn
}
func (m *sessionManager) Close() {
m.ctxCancel()
m.refCount.Wait()
}