From 2cba13896e2f46dee127071d0777a6bff65e21b6 Mon Sep 17 00:00:00 2001 From: Dustin Deus Date: Mon, 18 Nov 2024 17:41:02 +0100 Subject: [PATCH] fix: do netPoll detection once and set it globally (#1384) --- router/core/router.go | 21 +++++++++++++++++++++ router/core/websocket.go | 29 +++++++---------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/router/core/router.go b/router/core/router.go index a2ef32029..fec7458a0 100644 --- a/router/core/router.go +++ b/router/core/router.go @@ -6,6 +6,7 @@ import ( "crypto/x509" "errors" "fmt" + "github.com/wundergraph/graphql-go-tools/v2/pkg/netpoll" "net" "net/http" "net/url" @@ -515,6 +516,26 @@ func NewRouter(opts ...Option) (*Router, error) { r.logger.Info("Kafka Event source enabled", zap.String("provider_id", source.ID), zap.Strings("brokers", source.Brokers)) } + if !r.engineExecutionConfiguration.EnableNetPoll { + r.logger.Warn("Net poller is disabled by configuration. Falling back to less efficient connection handling method.") + } else if err := netpoll.Supported(); err != nil { + + // Disable netPoll if it's not supported. This flag is used everywhere to decide whether to use netPoll or not. + r.engineExecutionConfiguration.EnableNetPoll = false + + if errors.Is(err, netpoll.ErrUnsupported) { + r.logger.Warn( + "Net poller is only available on Linux and MacOS. Falling back to less efficient connection handling method.", + zap.Error(err), + ) + } else { + r.logger.Warn( + "Net poller is not functional by the environment. Ensure that the system supports epoll/kqueue and that necessary syscall permissions are granted. Falling back to less efficient connection handling method.", + zap.Error(err), + ) + } + } + return r, nil } diff --git a/router/core/websocket.go b/router/core/websocket.go index 5384605b8..53eae6540 100644 --- a/router/core/websocket.go +++ b/router/core/websocket.go @@ -122,30 +122,15 @@ func NewWebsocketMiddleware(ctx context.Context, opts WebsocketMiddlewareOptions handler.forwardQueryParamsConfig.withRegexAllowList = len(handler.forwardQueryParamsConfig.regexAllowList) > 0 } if opts.EnableNetPoll { - if err := netpoll.Supported(); err != nil { - if errors.Is(err, netpoll.ErrUnsupported) { - opts.Logger.Warn( - "Net poller is only available on Linux and MacOS. Falling back to less efficient connection handling method.", - zap.Error(err), - ) - } else { - opts.Logger.Warn( - "Net poller is not functional by the environment. Ensure that the system supports epoll/kqueue and that necessary syscall permissions are granted. Falling back to less efficient connection handling method.", - zap.Error(err), - ) - } - } else { - poller, err := netpoll.NewPoller(opts.NetPollConnBufferSize, opts.NetPollTimeout) - if err == nil { - opts.Logger.Debug("Net poller is available") + poller, err := netpoll.NewPoller(opts.NetPollConnBufferSize, opts.NetPollTimeout) + if err == nil { + opts.Logger.Debug("Net poller is available") - handler.netPoll = poller - handler.connections = make(map[int]*WebSocketConnectionHandler) - go handler.runPoller() - } + handler.netPoll = poller + handler.connections = make(map[int]*WebSocketConnectionHandler) + go handler.runPoller() } - } else { - opts.Logger.Warn("Net poller is disabled by configuration. Falling back to less efficient connection handling method.") + } return func(next http.Handler) http.Handler {