Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: customization of WebSocket-related parameters #651

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/pages/Schema/SchemaConfig/List/SpaceStats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ const SpaceStats = () => {
}
};
const showTime = updateTime && jobId == null && !loading;
console.log('=====updateTime', updateTime);
return (
<div className={styles.nebulaStats}>
<div className={styles.row}>
Expand Down
9 changes: 6 additions & 3 deletions app/utils/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,20 @@ export class NgqlRunner {
onError = (e: Event) => {
console.error('=====ngqlSocket error', e);
message.error('WebSocket error, try to reconnect...');
this.onDisconnect();
this.onDisconnect(e);
};

onDisconnect = () => {
console.log('=====onDisconnect');
onDisconnect = (e?: CloseEvent | Event) => {
console.log('=====onDisconnect', e);
this.socket?.removeEventListener('close', this.onDisconnect);
this.socket?.removeEventListener('error', this.onError);

this.clearMessageReceiver();
this.closeSocket();

const { code, reason } = (e as CloseEvent) || {};
reason && message.error(`WebSocket closed unexpectedly, code: ${code}, reason: \`${reason}\`, try to reconnect...`);

// try reconnect
this.socketUrl && setTimeout(this.reConnect, 3000);
};
Expand Down
17 changes: 17 additions & 0 deletions server/api/studio/etc/studio-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ Log:
KeepDays: 7
Debug:
Enable: false
WebSocket:
# The maximum wait time (secend) for the pong message from peer.
# If a peer does not respond to the ping message within this time, websocket connection will be closed.
# default 60s, 0 means no limit
WriteDeadline: 60
# The maximum wait time (secend) for the ping message from peer.
# If a peer does not send a ping message within this time, websocket connection will be closed.
# default 60s, 0 means no limit
ReadDeadline: 60
# The maximum message size allowed from peer.
# If a peer sends a message larger than this, an error will be thrown and websocket connection will be closed.
# default: 64MB (32 * 1024 * 1024), 0 means no limit or system limit
WriteLimit: 33554432
# The maximum message size allowed from peer.
# If a peer sends a message larger than this, websocket connection will be closed.
# default: 8MB (8 * 1024 * 1024), 0 means no limit or system limit
ReadLimit: 8388608
Auth:
TokenName: "studio_token"
AccessSecret: "login_secret"
Expand Down
19 changes: 19 additions & 0 deletions server/api/studio/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ type Config struct {
TasksDir string
} `json:",optional"`

WebSocket struct {
// The maximum wait time (secend) for the pong message from peer.
// If a peer does not respond to the ping message within this time, websocket will close the connection.
// default 60s, 0 means no limit
WriteDeadline int64 `json:",default=60"`
// The maximum wait time (secend) for the ping message from peer.
// If a peer does not send a ping message within this time, websocket will close the connection.
// default 60s, 0 means no limit
ReadDeadline int64 `json:",default=60"`
// The maximum message size allowed from peer.
// If a peer sends a message larger than this, a `websocket: write limit exceeded` error will be returned.
// default: 64MB (32 * 1024 * 1024), 0 means no limit or system limit
WriteLimit int64 `json:",default=33554432"`
// The maximum message size allowed from peer.
// If a peer sends a message larger than this, websocket will close the connection.
// default: 8MB (8 * 1024 * 1024), 0 means no limit or system limit
ReadLimit int64 `json:",default=8388608"`
} `json:",optional"`

DB struct {
LogLevel int `json:",default=4"`
IgnoreRecordNotFoundError bool `json:",default=true"`
Expand Down
63 changes: 38 additions & 25 deletions server/api/studio/pkg/ws/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,19 @@ package utils
import (
"bytes"
"encoding/json"
"fmt"
"sync"
"time"

"github.com/gorilla/websocket"
uuid "github.com/satori/go.uuid"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/config"
"github.com/zeromicro/go-zero/core/logx"
)

const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second

// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second

// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10

// Maximum message size allowed from peer.
// Max ngql length can be executed is 4MB
maxMessageSize = 8 * 1024 * 1024

// send buffer size
bufSize = 512

heartbeatRequest = "1"

bufSize = 512 // send buffer size
heartbeatRequest = "1"
heartbeatResponse = "2"
)

Expand Down Expand Up @@ -157,10 +143,15 @@ func (c *Client) Destroy() {
// reads from this goroutine.
func (c *Client) readPump() {
defer c.Destroy()

c.Conn.SetReadLimit(maxMessageSize)
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
c.Conn.SetPongHandler(func(string) error { c.Conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
wsConfig := config.GetConfig().WebSocket
if wsConfig.ReadLimit > 0 {
c.Conn.SetReadLimit(wsConfig.ReadLimit)
}
if wsConfig.ReadDeadline > 0 {
pongWait := time.Duration(wsConfig.ReadDeadline) * time.Second
c.Conn.SetReadDeadline(time.Now().Add(pongWait))
c.Conn.SetPongHandler(func(string) error { c.Conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
}
for {
_, message, err := c.Conn.ReadMessage()
if err != nil {
Expand Down Expand Up @@ -191,7 +182,15 @@ func (c *Client) readPump() {
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *Client) writePump() {
ticker := time.NewTicker(pingPeriod)
wsConfig := config.GetConfig().WebSocket

writeWait := 0 * time.Second
ticker := time.NewTicker(30 * time.Second)

if wsConfig.WriteDeadline > 0 {
writeWait = time.Duration(wsConfig.WriteDeadline) * time.Second
}

defer func() {
ticker.Stop()
c.Destroy()
Expand All @@ -200,14 +199,25 @@ func (c *Client) writePump() {
for {
select {
case message, ok := <-c.send:
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
// The hub closed the channel.
logx.Errorf("[WebSocket writePump]: c.send length: %v", len(c.send))
c.Conn.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(time.Second))
return
}

if writeWait > 0 {
// no timeout limit if writeWait is 0
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
}

msgLen := len(message)
if wsConfig.WriteLimit > 0 && int64(msgLen) > wsConfig.WriteLimit {
errMsg := websocket.FormatCloseMessage(websocket.CloseMessageTooBig, fmt.Sprintf("message length (%d) exceeds config limit (%d)", msgLen, wsConfig.WriteLimit))
c.Conn.WriteControl(websocket.CloseMessage, errMsg, time.Now().Add(time.Second))
return
}

w, err := c.Conn.NextWriter(websocket.TextMessage)
if err != nil {
logx.Errorf("[WebSocket WriteMessage]: %v", err)
Expand All @@ -220,7 +230,10 @@ func (c *Client) writePump() {
return
}
case <-ticker.C:
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
if writeWait > 0 {
// no timeout limit if writeWait is 0
c.Conn.SetWriteDeadline(time.Now().Add(writeWait))
}
if err := c.Conn.WriteMessage(websocket.PingMessage, nil); err != nil {
logx.Errorf("[WebSocket ticker error]: %v", err)
return
Expand Down