forked from centrifugal/centrifugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
172 lines (157 loc) · 4.07 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
package server
import (
"sync"
"time"
"github.com/centrifugal/centrifugo/libcentrifugo/conns"
"github.com/gorilla/websocket"
"github.com/igm/sockjs-go/sockjs"
)
const (
// CloseStatus is status code set when closing client connections.
CloseStatus = 3000
)
type sockjsSession struct {
mu sync.RWMutex
closed bool
closeCh chan struct{}
sess sockjs.Session
}
func newSockjsSession(sess sockjs.Session) *sockjsSession {
return &sockjsSession{
sess: sess,
closeCh: make(chan struct{}),
}
}
func (sess *sockjsSession) Send(msg *conns.QueuedMessage) error {
select {
case <-sess.closeCh:
return nil
default:
return sess.sess.Send(string(msg.Payload))
}
}
func (sess *sockjsSession) Close(advice *conns.DisconnectAdvice) error {
sess.mu.Lock()
defer sess.mu.Unlock()
if sess.closed {
// Already closed, noop.
return nil
}
sess.closed = true
close(sess.closeCh)
if advice == nil {
advice = conns.DefaultDisconnectAdvice
}
reason, err := advice.JSONString()
if err != nil {
return err
}
return sess.sess.Close(CloseStatus, reason)
}
// websocketConn is an interface to mimic gorilla/websocket methods we use in Centrifugo.
// Needed only to simplify wsConn struct tests. Description can be found in gorilla websocket docs:
// https://godoc.org/github.com/gorilla/websocket.
type websocketConn interface {
ReadMessage() (messageType int, p []byte, err error)
WriteMessage(messageType int, data []byte) error
WritePreparedMessage(pm *websocket.PreparedMessage) error
WriteControl(messageType int, data []byte, deadline time.Time) error
SetWriteDeadline(t time.Time) error
EnableWriteCompression(enable bool)
Close() error
}
// wsSession is a wrapper struct over websocket connection to fit session interface so
// client will accept it.
type wsSession struct {
mu sync.RWMutex
ws websocketConn
closed bool
closeCh chan struct{}
pingInterval time.Duration
writeTimeout time.Duration
compressionMinSize int
pingTimer *time.Timer
}
func newWSSession(ws websocketConn, pingInterval time.Duration, writeTimeout time.Duration, compressionMinSize int) *wsSession {
sess := &wsSession{
ws: ws,
closeCh: make(chan struct{}),
pingInterval: pingInterval,
writeTimeout: writeTimeout,
compressionMinSize: compressionMinSize,
}
if pingInterval > 0 {
sess.addPing()
}
return sess
}
func (sess *wsSession) ping() {
select {
case <-sess.closeCh:
return
default:
deadline := time.Now().Add(sess.pingInterval / 2)
err := sess.ws.WriteControl(websocket.PingMessage, []byte("ping"), deadline)
if err != nil {
sess.Close(&conns.DisconnectAdvice{Reason: "write ping error", Reconnect: true})
return
}
sess.addPing()
}
}
func (sess *wsSession) addPing() {
sess.mu.Lock()
if sess.closed {
sess.mu.Unlock()
return
}
sess.pingTimer = time.AfterFunc(sess.pingInterval, sess.ping)
sess.mu.Unlock()
}
func (sess *wsSession) Send(msg *conns.QueuedMessage) error {
select {
case <-sess.closeCh:
return nil
default:
if sess.compressionMinSize > 0 {
sess.ws.EnableWriteCompression(msg.Len() > sess.compressionMinSize)
}
if sess.writeTimeout > 0 {
sess.ws.SetWriteDeadline(time.Now().Add(sess.writeTimeout))
}
var err error
if msg.UsePrepared {
err = sess.ws.WritePreparedMessage(msg.Prepared())
} else {
err = sess.ws.WriteMessage(websocket.TextMessage, msg.Payload)
}
if sess.writeTimeout > 0 {
sess.ws.SetWriteDeadline(time.Time{})
}
return err
}
}
func (sess *wsSession) Close(advice *conns.DisconnectAdvice) error {
sess.mu.Lock()
if sess.closed {
// Already closed, noop.
sess.mu.Unlock()
return nil
}
close(sess.closeCh)
sess.closed = true
if sess.pingTimer != nil {
sess.pingTimer.Stop()
}
sess.mu.Unlock()
if advice != nil {
deadline := time.Now().Add(time.Second)
reason, err := advice.JSONString()
if err != nil {
return err
}
msg := websocket.FormatCloseMessage(int(CloseStatus), reason)
sess.ws.WriteControl(websocket.CloseMessage, msg, deadline)
}
return sess.ws.Close()
}