@@ -3,33 +3,19 @@ package utils
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
+ "fmt"
6
7
"sync"
7
8
"time"
8
9
9
10
"github.com/gorilla/websocket"
10
11
uuid "github.com/satori/go.uuid"
12
+ "github.com/vesoft-inc/nebula-studio/server/api/studio/internal/config"
11
13
"github.com/zeromicro/go-zero/core/logx"
12
14
)
13
15
14
16
const (
15
- // Time allowed to write a message to the peer.
16
- writeWait = 10 * time .Second
17
-
18
- // Time allowed to read the next pong message from the peer.
19
- pongWait = 60 * time .Second
20
-
21
- // Send pings to peer with this period. Must be less than pongWait.
22
- pingPeriod = (pongWait * 9 ) / 10
23
-
24
- // Maximum message size allowed from peer.
25
- // Max ngql length can be executed is 4MB
26
- maxMessageSize = 8 * 1024 * 1024
27
-
28
- // send buffer size
29
- bufSize = 512
30
-
31
- heartbeatRequest = "1"
32
-
17
+ bufSize = 512 // send buffer size
18
+ heartbeatRequest = "1"
33
19
heartbeatResponse = "2"
34
20
)
35
21
@@ -157,10 +143,15 @@ func (c *Client) Destroy() {
157
143
// reads from this goroutine.
158
144
func (c * Client ) readPump () {
159
145
defer c .Destroy ()
160
-
161
- c .Conn .SetReadLimit (maxMessageSize )
162
- c .Conn .SetReadDeadline (time .Now ().Add (pongWait ))
163
- c .Conn .SetPongHandler (func (string ) error { c .Conn .SetReadDeadline (time .Now ().Add (pongWait )); return nil })
146
+ wsConfig := config .GetConfig ().WebSocket
147
+ if wsConfig .ReadLimit > 0 {
148
+ c .Conn .SetReadLimit (wsConfig .ReadLimit )
149
+ }
150
+ if wsConfig .ReadDeadline > 0 {
151
+ pongWait := time .Duration (wsConfig .ReadDeadline ) * time .Second
152
+ c .Conn .SetReadDeadline (time .Now ().Add (pongWait ))
153
+ c .Conn .SetPongHandler (func (string ) error { c .Conn .SetReadDeadline (time .Now ().Add (pongWait )); return nil })
154
+ }
164
155
for {
165
156
_ , message , err := c .Conn .ReadMessage ()
166
157
if err != nil {
@@ -191,7 +182,15 @@ func (c *Client) readPump() {
191
182
// application ensures that there is at most one writer to a connection by
192
183
// executing all writes from this goroutine.
193
184
func (c * Client ) writePump () {
194
- ticker := time .NewTicker (pingPeriod )
185
+ wsConfig := config .GetConfig ().WebSocket
186
+
187
+ writeWait := 0 * time .Second
188
+ ticker := time .NewTicker (30 * time .Second )
189
+
190
+ if wsConfig .WriteDeadline > 0 {
191
+ writeWait = time .Duration (wsConfig .WriteDeadline ) * time .Second
192
+ }
193
+
195
194
defer func () {
196
195
ticker .Stop ()
197
196
c .Destroy ()
@@ -200,14 +199,25 @@ func (c *Client) writePump() {
200
199
for {
201
200
select {
202
201
case message , ok := <- c .send :
203
- c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
204
202
if ! ok {
205
203
// The hub closed the channel.
206
204
logx .Errorf ("[WebSocket writePump]: c.send length: %v" , len (c .send ))
207
205
c .Conn .WriteControl (websocket .CloseMessage , []byte {}, time .Now ().Add (time .Second ))
208
206
return
209
207
}
210
208
209
+ if writeWait > 0 {
210
+ // no timeout limit if writeWait is 0
211
+ c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
212
+ }
213
+
214
+ msgLen := len (message )
215
+ if wsConfig .WriteLimit > 0 && int64 (msgLen ) > wsConfig .WriteLimit {
216
+ errMsg := websocket .FormatCloseMessage (websocket .CloseMessageTooBig , fmt .Sprintf ("message length (%d) exceeds config limit (%d)" , msgLen , wsConfig .WriteLimit ))
217
+ c .Conn .WriteControl (websocket .CloseMessage , errMsg , time .Now ().Add (time .Second ))
218
+ return
219
+ }
220
+
211
221
w , err := c .Conn .NextWriter (websocket .TextMessage )
212
222
if err != nil {
213
223
logx .Errorf ("[WebSocket WriteMessage]: %v" , err )
@@ -220,7 +230,10 @@ func (c *Client) writePump() {
220
230
return
221
231
}
222
232
case <- ticker .C :
223
- c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
233
+ if writeWait > 0 {
234
+ // no timeout limit if writeWait is 0
235
+ c .Conn .SetWriteDeadline (time .Now ().Add (writeWait ))
236
+ }
224
237
if err := c .Conn .WriteMessage (websocket .PingMessage , nil ); err != nil {
225
238
logx .Errorf ("[WebSocket ticker error]: %v" , err )
226
239
return
0 commit comments