Skip to content

Commit

Permalink
fix: do netPoll detection once and set it globally (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech authored Nov 18, 2024
1 parent fe3f532 commit 2cba138
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
21 changes: 21 additions & 0 deletions router/core/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"github.com/wundergraph/graphql-go-tools/v2/pkg/netpoll"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
}

Expand Down
29 changes: 7 additions & 22 deletions router/core/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2cba138

Please sign in to comment.