From 9e26c891d8e373b3d8d00ab36cdbb0de14991528 Mon Sep 17 00:00:00 2001 From: su chen Date: Wed, 6 Dec 2023 16:00:06 +0800 Subject: [PATCH] support unlimit websocket single message (#1160) --- docs/02.Tutorials/2.6.Websocket.md | 12 +++++++++++- docs/07.Reference/7.02.Filters.md | 6 ++++++ pkg/filters/proxies/httpproxy/wspool.go | 4 ++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/02.Tutorials/2.6.Websocket.md b/docs/02.Tutorials/2.6.Websocket.md index e0128ec125..28ab9c7ceb 100644 --- a/docs/02.Tutorials/2.6.Websocket.md +++ b/docs/02.Tutorials/2.6.Websocket.md @@ -21,7 +21,7 @@ ## Design - The `WebSocketProxy` is a filter of Easegress, and can be put into a pipeline. -- Easegress uses `github.com/golang/x/net/websocket` to implement +- Easegress uses `nhooyr.io/websocket` to implement `WebSocketProxy` filter. 1. Spec @@ -42,6 +42,16 @@ filters: pools: - servers: - url: ws://127.0.0.1:12345 + - url: ws://127.0.0.1:9095 + - url: ws://127.0.0.1:9096 + - url: ws://127.0.0.1:9097 + loadBalance: + policy: roundRobin + # the max number of bytes to read for a single message in + # client/server connection. + # default is 32769, set -1 to disable limit. + clientMaxMsgSize: 32769 + serverMaxMsgSize: 32769 ``` * HTTPServer to route traffic to the websocket-pipeline: diff --git a/docs/07.Reference/7.02.Filters.md b/docs/07.Reference/7.02.Filters.md index 4c86983bcc..476db4246d 100644 --- a/docs/07.Reference/7.02.Filters.md +++ b/docs/07.Reference/7.02.Filters.md @@ -361,6 +361,12 @@ pools: - url: ws://127.0.0.1:9095 - url: ws://127.0.0.1:9096 - url: ws://127.0.0.1:9097 + loadBalance: + policy: roundRobin + # the max number of bytes to read for a single message in client/server connection. + # default is 32769, set -1 to disable limit. + clientMaxMsgSize: 32769 + serverMaxMsgSize: 32769 ``` Same as the `Proxy` filter: diff --git a/pkg/filters/proxies/httpproxy/wspool.go b/pkg/filters/proxies/httpproxy/wspool.go index c240bfc32a..24f02bd7df 100644 --- a/pkg/filters/proxies/httpproxy/wspool.go +++ b/pkg/filters/proxies/httpproxy/wspool.go @@ -156,7 +156,7 @@ func (sp *WebSocketServerPool) dialServer(svr *Server, req *httpprot.Request) (* } conn, _, err := websocket.Dial(stdctx.Background(), u, opts) - if err == nil && sp.spec.ServerMaxMsgSize > 0 { + if err == nil && (sp.spec.ServerMaxMsgSize > 0 || sp.spec.ServerMaxMsgSize == -1) { conn.SetReadLimit(sp.spec.ServerMaxMsgSize) } return conn, err @@ -204,7 +204,7 @@ func (sp *WebSocketServerPool) handle(ctx *context.Context) (result string) { metric.StatusCode = http.StatusBadRequest return resultClientError } - if sp.spec.ClientMaxMsgSize > 0 { + if sp.spec.ClientMaxMsgSize > 0 || sp.spec.ClientMaxMsgSize == -1 { clntConn.SetReadLimit(sp.spec.ClientMaxMsgSize) }