good morning!!!!

Skip to content
Snippets Groups Projects
Commit fbcb603d authored by a's avatar a
Browse files

properly increase read limit

parent d058ad78
No related branches found
No related tags found
No related merge requests found
...@@ -34,7 +34,7 @@ const ( ...@@ -34,7 +34,7 @@ const (
wsPingInterval = 60 * time.Second wsPingInterval = 60 * time.Second
wsPingWriteTimeout = 5 * time.Second wsPingWriteTimeout = 5 * time.Second
wsPongTimeout = 30 * time.Second wsPongTimeout = 30 * time.Second
wsMessageSizeLimit = 32 * 1024 * 1024 wsMessageSizeLimit = 128 * 1024 * 1024
) )
// WebsocketHandler returns a handler that serves JSON-RPC to WebSocket connections. // WebsocketHandler returns a handler that serves JSON-RPC to WebSocket connections.
...@@ -154,11 +154,11 @@ func heartbeat(ctx context.Context, c *websocket.Conn, d time.Duration) { ...@@ -154,11 +154,11 @@ func heartbeat(ctx context.Context, c *websocket.Conn, d time.Duration) {
} }
func newWebsocketCodec(ctx context.Context, c *websocket.Conn, host string, req http.Header) ServerCodec { func newWebsocketCodec(ctx context.Context, c *websocket.Conn, host string, req http.Header) ServerCodec {
c.SetReadLimit(wsMessageSizeLimit)
jsonWriter := func(v any) error { jsonWriter := func(v any) error {
return wsjson.Write(context.Background(), c, v) return wsjson.Write(context.Background(), c, v)
} }
jsonReader := func(v any) error { jsonReader := func(v any) error {
c.SetReadLimit(wsMessageSizeLimit)
return wsjson.Read(context.Background(), c, v) return wsjson.Read(context.Background(), c, v)
} }
conn := websocket.NetConn(ctx, c, websocket.MessageText) conn := websocket.NetConn(ctx, c, websocket.MessageText)
......
...@@ -70,18 +70,17 @@ func Write(ctx context.Context, c *websocket.Conn, v interface{}) error { ...@@ -70,18 +70,17 @@ func Write(ctx context.Context, c *websocket.Conn, v interface{}) error {
} }
func write(ctx context.Context, c *websocket.Conn, v interface{}) (err error) { func write(ctx context.Context, c *websocket.Conn, v interface{}) (err error) {
w, err := c.Writer(ctx, websocket.MessageText) w, err := c.Writer(ctx, websocket.MessageText)
if err != nil { if err != nil {
return err return err
} }
// json.Marshal cannot reuse buffers between calls as it has to return // json.Marshal cannot reuse buffers between calls as it has to return
// a copy of the byte slice but Encoder does as it directly writes to w. // a copy of the byte slice but Encoder does as it directly writes to w.
err = jzon.NewEncoder(w).Encode(v) st := jsoniter.NewStream(jzon, w, 1024)
st.WriteVal(v)
err = st.Flush()
if err != nil { if err != nil {
return fmt.Errorf("failed to marshal JSON: %w", err) return fmt.Errorf("failed to marshal JSON: %w", err)
} }
return w.Close() return w.Close()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment