Skip to content

Commit 9277758

Browse files
committed
feat: customization of WebSocket-related parameters
1 parent 19628e6 commit 9277758

File tree

4 files changed

+65
-26
lines changed

4 files changed

+65
-26
lines changed

app/pages/Schema/SchemaConfig/List/SpaceStats/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ const SpaceStats = () => {
127127
}
128128
};
129129
const showTime = updateTime && jobId == null && !loading;
130-
console.log('=====updateTime', updateTime);
131130
return (
132131
<div className={styles.nebulaStats}>
133132
<div className={styles.row}>

server/api/studio/etc/studio-api.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ Log:
1212
KeepDays: 7
1313
Debug:
1414
Enable: false
15+
WebSocket:
16+
# The maximum wait time (secend) for the pong message from peer.
17+
# If a peer does not respond to the ping message within this time, websocket will close the connection.
18+
# default 60s, 0 means no limit
19+
WriteDeadline: 60
20+
# The maximum wait time (secend) for the ping message from peer.
21+
# If a peer does not send a ping message within this time, websocket will close the connection.
22+
# default 60s, 0 means no limit
23+
ReadDeadline: 60
24+
# The maximum message size allowed from peer.
25+
# If a peer sends a message larger than this, a `websocket: write limit exceeded` error will be returned.
26+
# default: 64MB (32 * 1024 * 1024), 0 means no limit or system limit
27+
WriteLimit: 33554432
28+
# The maximum message size allowed from peer.
29+
# If a peer sends a message larger than this, websocket will close the connection.
30+
# default: 8MB (8 * 1024 * 1024), 0 means no limit or system limit
31+
ReadLimit: 8388608
1532
Auth:
1633
TokenName: "studio_token"
1734
AccessSecret: "login_secret"

server/api/studio/internal/config/config.go

+19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ type Config struct {
3232
TasksDir string
3333
} `json:",optional"`
3434

35+
WebSocket struct {
36+
// The maximum wait time (secend) for the pong message from peer.
37+
// If a peer does not respond to the ping message within this time, websocket will close the connection.
38+
// default 60s, 0 means no limit
39+
WriteDeadline int64 `json:",default=60"`
40+
// The maximum wait time (secend) for the ping message from peer.
41+
// If a peer does not send a ping message within this time, websocket will close the connection.
42+
// default 60s, 0 means no limit
43+
ReadDeadline int64 `json:",default=60"`
44+
// The maximum message size allowed from peer.
45+
// If a peer sends a message larger than this, a `websocket: write limit exceeded` error will be returned.
46+
// default: 64MB (32 * 1024 * 1024), 0 means no limit or system limit
47+
WriteLimit int64 `json:",default=33554432"`
48+
// The maximum message size allowed from peer.
49+
// If a peer sends a message larger than this, websocket will close the connection.
50+
// default: 8MB (8 * 1024 * 1024), 0 means no limit or system limit
51+
ReadLimit int64 `json:",default=8388608"`
52+
} `json:",optional"`
53+
3554
DB struct {
3655
LogLevel int `json:",default=4"`
3756
IgnoreRecordNotFoundError bool `json:",default=true"`

server/api/studio/pkg/ws/utils/client.go

+29-25
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,13 @@ import (
88

99
"github.com/gorilla/websocket"
1010
uuid "github.com/satori/go.uuid"
11+
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/config"
1112
"github.com/zeromicro/go-zero/core/logx"
1213
)
1314

1415
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-
16+
bufSize = 512 // send buffer size
17+
heartbeatRequest = "1"
3318
heartbeatResponse = "2"
3419
)
3520

@@ -157,10 +142,15 @@ func (c *Client) Destroy() {
157142
// reads from this goroutine.
158143
func (c *Client) readPump() {
159144
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 })
145+
wsConfig := config.GetConfig().WebSocket
146+
if wsConfig.ReadLimit > 0 {
147+
c.Conn.SetReadLimit(wsConfig.ReadLimit)
148+
}
149+
if wsConfig.ReadDeadline > 0 {
150+
pongWait := time.Duration(wsConfig.ReadDeadline) * time.Second
151+
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
152+
c.Conn.SetPongHandler(func(string) error { c.Conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
153+
}
164154
for {
165155
_, message, err := c.Conn.ReadMessage()
166156
if err != nil {
@@ -191,7 +181,15 @@ func (c *Client) readPump() {
191181
// application ensures that there is at most one writer to a connection by
192182
// executing all writes from this goroutine.
193183
func (c *Client) writePump() {
194-
ticker := time.NewTicker(pingPeriod)
184+
wsConfig := config.GetConfig().WebSocket
185+
186+
writeWait := 0 * time.Second
187+
ticker := time.NewTicker(30 * time.Second)
188+
189+
if wsConfig.WriteDeadline > 0 {
190+
writeWait = time.Duration(wsConfig.WriteDeadline) * time.Second
191+
}
192+
195193
defer func() {
196194
ticker.Stop()
197195
c.Destroy()
@@ -200,7 +198,10 @@ func (c *Client) writePump() {
200198
for {
201199
select {
202200
case message, ok := <-c.send:
203-
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
201+
if writeWait > 0 {
202+
// no timeout limit if writeWait is 0
203+
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
204+
}
204205
if !ok {
205206
// The hub closed the channel.
206207
logx.Errorf("[WebSocket writePump]: c.send length: %v", len(c.send))
@@ -220,7 +221,10 @@ func (c *Client) writePump() {
220221
return
221222
}
222223
case <-ticker.C:
223-
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
224+
if writeWait > 0 {
225+
// no timeout limit if writeWait is 0
226+
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
227+
}
224228
if err := c.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
225229
logx.Errorf("[WebSocket ticker error]: %v", err)
226230
return

0 commit comments

Comments
 (0)