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

Add configuration support for nyhoor websocket read limit #1023

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
10 changes: 10 additions & 0 deletions go/grpcweb/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type options struct {
enableWebsockets bool
websocketPingInterval time.Duration
websocketOriginFunc func(req *http.Request) bool
websocketReadLimit int64
allowNonRootResources bool
endpointsFunc *func() []string
}
Expand Down Expand Up @@ -132,6 +133,15 @@ func WithWebsocketOriginFunc(websocketOriginFunc func(req *http.Request) bool) O
}
}

// WithWebsocketsMessageReadLimit sets the maximum message read limit on the underlying websocket.
//
// The default message read limit is 32769 bytes
func WithWebsocketsMessageReadLimit(websocketReadLimit int64) Option {
return func(o *options) {
o.websocketReadLimit = websocketReadLimit
}
}

// WithAllowNonRootResource enables the gRPC wrapper to serve requests that have a path prefix
// added to the URL, before the service name and method placeholders.
//
Expand Down
7 changes: 7 additions & 0 deletions go/grpcweb/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type WrappedGrpcServer struct {
originFunc func(origin string) bool
enableWebsockets bool
websocketOriginFunc func(req *http.Request) bool
websocketReadLimit int64
allowedHeaders []string
endpointFunc func(req *http.Request) string
endpointsFunc func() []string
Expand Down Expand Up @@ -97,6 +98,7 @@ func wrapGrpc(options []Option, handler http.Handler, endpointsFunc func() []str
originFunc: opts.originFunc,
enableWebsockets: opts.enableWebsockets,
websocketOriginFunc: websocketOriginFunc,
websocketReadLimit: opts.websocketReadLimit,
allowedHeaders: allowedHeaders,
endpointFunc: endpointFunc,
endpointsFunc: endpointsFunc,
Expand Down Expand Up @@ -160,6 +162,11 @@ func (w *WrappedGrpcServer) HandleGrpcWebsocketRequest(resp http.ResponseWriter,
grpclog.Errorf("Unable to upgrade websocket request: %v", err)
return
}

if w.websocketReadLimit > 0 {
wsConn.SetReadLimit(w.websocketReadLimit)
}

headers := make(http.Header)
for _, name := range w.allowedHeaders {
if values, exist := req.Header[name]; exist {
Expand Down
5 changes: 5 additions & 0 deletions go/grpcwebproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (

useWebsockets = pflag.Bool("use_websockets", false, "whether to use beta websocket transport layer")
websocketPingInterval = pflag.Duration("websocket_ping_interval", 0, "whether to use websocket keepalive pinging. Only used when using websockets. Configured interval must be >= 1s.")
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.")

flagHttpMaxWriteTimeout = pflag.Duration("server_http_max_write_timeout", 10*time.Second, "HTTP server config, max write duration.")
flagHttpMaxReadTimeout = pflag.Duration("server_http_max_read_timeout", 10*time.Second, "HTTP server config, max read duration.")
Expand Down Expand Up @@ -84,6 +85,10 @@ func main() {
if *websocketPingInterval >= time.Second {
logrus.Infof("websocket keepalive pinging enabled, the timeout interval is %s", websocketPingInterval.String())
}
if *websocketReadLimit > 0 {
options = append(options, grpcweb.WithWebsocketsMessageReadLimit(*websocketReadLimit))
}

options = append(
options,
grpcweb.WithWebsocketPingInterval(*websocketPingInterval),
Expand Down