Skip to content

Commit 53e1aaa

Browse files
bryanmcgraneBryan McGrane
andauthored
Add configuration support for nyhoor websocket read limit (#1023)
* Add configuration support for nyhoor websocket read limit * Add support for websocket read limit in grpc web proxy Co-authored-by: Bryan McGrane <[email protected]>
1 parent 189bea5 commit 53e1aaa

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

go/grpcweb/options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type options struct {
2424
enableWebsockets bool
2525
websocketPingInterval time.Duration
2626
websocketOriginFunc func(req *http.Request) bool
27+
websocketReadLimit int64
2728
allowNonRootResources bool
2829
endpointsFunc *func() []string
2930
}
@@ -132,6 +133,15 @@ func WithWebsocketOriginFunc(websocketOriginFunc func(req *http.Request) bool) O
132133
}
133134
}
134135

136+
// WithWebsocketsMessageReadLimit sets the maximum message read limit on the underlying websocket.
137+
//
138+
// The default message read limit is 32769 bytes
139+
func WithWebsocketsMessageReadLimit(websocketReadLimit int64) Option {
140+
return func(o *options) {
141+
o.websocketReadLimit = websocketReadLimit
142+
}
143+
}
144+
135145
// WithAllowNonRootResource enables the gRPC wrapper to serve requests that have a path prefix
136146
// added to the URL, before the service name and method placeholders.
137147
//

go/grpcweb/wrapper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type WrappedGrpcServer struct {
3535
originFunc func(origin string) bool
3636
enableWebsockets bool
3737
websocketOriginFunc func(req *http.Request) bool
38+
websocketReadLimit int64
3839
allowedHeaders []string
3940
endpointFunc func(req *http.Request) string
4041
endpointsFunc func() []string
@@ -97,6 +98,7 @@ func wrapGrpc(options []Option, handler http.Handler, endpointsFunc func() []str
9798
originFunc: opts.originFunc,
9899
enableWebsockets: opts.enableWebsockets,
99100
websocketOriginFunc: websocketOriginFunc,
101+
websocketReadLimit: opts.websocketReadLimit,
100102
allowedHeaders: allowedHeaders,
101103
endpointFunc: endpointFunc,
102104
endpointsFunc: endpointsFunc,
@@ -160,6 +162,11 @@ func (w *WrappedGrpcServer) HandleGrpcWebsocketRequest(resp http.ResponseWriter,
160162
grpclog.Errorf("Unable to upgrade websocket request: %v", err)
161163
return
162164
}
165+
166+
if w.websocketReadLimit > 0 {
167+
wsConn.SetReadLimit(w.websocketReadLimit)
168+
}
169+
163170
headers := make(http.Header)
164171
for _, name := range w.allowedHeaders {
165172
if values, exist := req.Header[name]; exist {

go/grpcwebproxy/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141

4242
useWebsockets = pflag.Bool("use_websockets", false, "whether to use beta websocket transport layer")
4343
websocketPingInterval = pflag.Duration("websocket_ping_interval", 0, "whether to use websocket keepalive pinging. Only used when using websockets. Configured interval must be >= 1s.")
44+
websocketReadLimit = pflag.Int64("websocket_read_limit", 0, "sets the maximum message read limit on the underlying websocket. The default message read limit is 32769 bytes.")
4445

4546
flagHttpMaxWriteTimeout = pflag.Duration("server_http_max_write_timeout", 10*time.Second, "HTTP server config, max write duration.")
4647
flagHttpMaxReadTimeout = pflag.Duration("server_http_max_read_timeout", 10*time.Second, "HTTP server config, max read duration.")
@@ -84,6 +85,10 @@ func main() {
8485
if *websocketPingInterval >= time.Second {
8586
logrus.Infof("websocket keepalive pinging enabled, the timeout interval is %s", websocketPingInterval.String())
8687
}
88+
if *websocketReadLimit > 0 {
89+
options = append(options, grpcweb.WithWebsocketsMessageReadLimit(*websocketReadLimit))
90+
}
91+
8792
options = append(
8893
options,
8994
grpcweb.WithWebsocketPingInterval(*websocketPingInterval),

0 commit comments

Comments
 (0)