-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
233 lines (195 loc) · 5.54 KB
/
sessions.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package engineio
import (
"context"
"sync"
"sync/atomic"
"time"
eios "github.com/oarkflow/socketio/engineio/session"
eiot "github.com/oarkflow/socketio/engineio/transport"
)
func loadDuration(addr *time.Duration) time.Duration {
return time.Duration(atomic.LoadInt64((*int64)(addr)))
}
func storeDuration(addr *time.Duration, val time.Duration) {
atomic.StoreInt64((*int64)(addr), int64(val))
}
type transportSessions interface {
Set(eiot.Transporter) error
Get(SessionID) (eiot.Transporter, error)
WithCancel(ctx context.Context) context.Context
WithTimeout(ctx context.Context, d time.Duration) context.Context
WithInterval(ctx context.Context, d time.Duration) context.Context
WithDisconnectOnClose(ctx context.Context) context.Context
}
type sessions struct {
*transport
*lifecycle
}
func NewSessions() *sessions {
tr := transport{
ʘ: new(sync.RWMutex),
s: make(map[SessionID]eiot.Transporter),
}
li := lifecycle{
t: new(sync.Map),
i: new(sync.Map),
cancel: new(sync.Map),
shave: 10 * time.Millisecond,
removeTransport: func(sessionID SessionID) {
tr.ʘ.Lock()
if t, ok := tr.s[sessionID]; ok {
t.SendTimeout()
}
delete(tr.s, sessionID)
tr.ʘ.Unlock()
},
}
return &sessions{transport: &tr, lifecycle: &li}
}
type transport struct {
ʘ *sync.RWMutex
s map[SessionID]eiot.Transporter
}
func (t *transport) Set(tr eiot.Transporter) error {
t.ʘ.Lock()
t.s[tr.ID()] = tr
t.ʘ.Unlock()
return nil
}
func (t *transport) Get(sessionID SessionID) (eiot.Transporter, error) {
t.ʘ.RLock()
defer t.ʘ.RUnlock()
if tr, ok := t.s[sessionID]; ok {
return tr, nil
}
return nil, ErrUnknownSessionID
}
type lifecycle struct {
id, td, shave time.Duration
t *sync.Map
i *sync.Map
cancel *sync.Map
removeTransport func(SessionID)
}
func (c *lifecycle) WithDisconnectOnClose(ctx context.Context) context.Context {
sessionID, ok := ctx.Value(ctxSessionID).(SessionID)
if !ok {
// there is no session to attach the context to
return ctx
}
ctx = context.WithValue(ctx, eios.SessionDisconnectFunctionKey, func() func() {
return func() {
c.removeSession(sessionID)
if c.removeTransport != nil {
c.removeTransport(sessionID)
}
}
})
return ctx
}
func (c *lifecycle) WithCancel(ctx context.Context) context.Context {
sessionID, ok := ctx.Value(ctxSessionID).(SessionID)
if !ok {
// there is no session to attach the timer to
return ctx
}
var chanPrefix = "chan:done:"
c.cancel.LoadOrStore(sessionID.PrefixID(chanPrefix), make(chan func(), 1))
ctx = context.WithValue(ctx, eios.SessionCloseChannelKey, func() <-chan func() {
if ch, ok := c.cancel.Load(sessionID.PrefixID(chanPrefix)); ok {
return ch.(chan func())
}
return nil
})
// Cancel will wait for another connections to close before closing this connection.
// As of now this requires all of the sessions to be on a single server, by using
// sticky sessions, otherwise this may not work as expected.
ctx = context.WithValue(ctx, eios.SessionCloseFunctionKey, func() func() {
if fn, ok := c.cancel.Load(sessionID.PrefixID(chanPrefix)); ok {
syn := new(sync.WaitGroup)
syn.Add(1)
fn.(chan func()) <- func() { syn.Done() }
close(fn.(chan func()))
syn.Wait()
return func() {
c.removeSession(sessionID)
if c.removeTransport != nil {
c.removeTransport(sessionID)
}
c.cancel.Delete(sessionID.PrefixID(chanPrefix))
}
}
return nil
})
return ctx
}
func (c *lifecycle) WithInterval(ctx context.Context, d time.Duration) context.Context {
if d <= 0 {
return ctx
}
sessionID, ok := ctx.Value(ctxSessionID).(SessionID)
if !ok {
// there is no session to attach the timer to
return ctx
}
storeDuration(&c.id, d)
c.i.LoadOrStore(sessionID, time.NewTicker(c.id))
var interval eios.IntervalChannel = func() <-chan time.Time {
if val, ok := c.i.Load(sessionID); ok {
val.(*time.Ticker).Reset(loadDuration(&c.id))
return val.(*time.Ticker).C
}
return nil
}
return context.WithValue(ctx, eios.SessionIntervalKey, interval)
}
func (c *lifecycle) WithTimeout(ctx context.Context, d time.Duration) context.Context {
if d <= 0 {
return ctx
}
sessionID, ok := ctx.Value(ctxSessionID).(SessionID)
if !ok {
// there is no session to attach the timer to
return ctx
}
storeDuration(&c.td, d)
if val, ok := c.t.Load(sessionID); ok {
reset := (loadDuration(&c.td) + loadDuration(&c.id)) - loadDuration(&c.shave)
val.(*time.Timer).Stop()
val.(*time.Timer).Reset(reset)
} else {
reset := (loadDuration(&c.td) + loadDuration(&c.id)) - loadDuration(&c.shave)
c.t.Store(sessionID, time.NewTimer(reset))
c.setTimeout(sessionID, time.Now())
}
x, cancel := context.WithCancel(ctx)
c.cancel.Store(sessionID, func() { cancel() })
var timeout eios.TimeoutChannel = func() <-chan struct{} {
return x.Done()
}
x = context.WithValue(x, eios.SessionExtendTimeoutKey, eios.ExtendTimeoutFunc(func() {
if val, ok := c.t.Load(sessionID); ok {
reset := (loadDuration(&c.td) + loadDuration(&c.id)) - loadDuration(&c.shave)
val.(*time.Timer).Stop()
val.(*time.Timer).Reset(reset)
}
}))
return context.WithValue(x, eios.SessionTimeoutKey, timeout)
}
func (c *lifecycle) setTimeout(sessionID SessionID, start time.Time) {
go func() {
val, _ := c.t.Load(sessionID)
<-val.(*time.Timer).C
cancel, _ := c.cancel.Load(sessionID)
cancel.(func())()
c.removeSession(sessionID)
if c.removeTransport != nil {
c.removeTransport(sessionID)
}
c.cancel.Delete(sessionID)
}()
}
func (c *lifecycle) removeSession(sessionID SessionID) {
c.t.Delete(sessionID)
c.i.Delete(sessionID)
}