diff --git a/itests/clients/net_client.go b/itests/clients/net_client.go index 74b7c457d..821999517 100644 --- a/itests/clients/net_client.go +++ b/itests/clients/net_client.go @@ -46,7 +46,7 @@ func NewNetClient( h := newHandler(t, peers) log := slogt.New(t) conf := networking.NewConfig(p, h). - WithLogger(log). + WithSlogHandler(log.Handler()). WithWriteTimeout(networkTimeout). WithKeepAliveInterval(pingInterval). WithSlogAttributes(slog.String("suite", t.Name()), slog.String("impl", impl.String())) diff --git a/pkg/networking/configuration.go b/pkg/networking/configuration.go index cda0e377e..151bc8541 100644 --- a/pkg/networking/configuration.go +++ b/pkg/networking/configuration.go @@ -12,7 +12,7 @@ const ( // Config allows to set some parameters of the [Conn] or it's underlying connection. type Config struct { - logger *slog.Logger + slogHandler slog.Handler protocol Protocol handler Handler keepAlive bool @@ -25,7 +25,6 @@ type Config struct { // Other parameters are set to their default values. func NewConfig(p Protocol, h Handler) *Config { return &Config{ - logger: slog.Default(), protocol: p, handler: h, keepAlive: true, @@ -35,9 +34,9 @@ func NewConfig(p Protocol, h Handler) *Config { } } -// WithLogger sets the logger. -func (c *Config) WithLogger(logger *slog.Logger) *Config { - c.logger = logger +// WithSlogHandler sets the slog handler. +func (c *Config) WithSlogHandler(handler slog.Handler) *Config { + c.slogHandler = handler return c } diff --git a/pkg/networking/logging.go b/pkg/networking/logging.go new file mode 100644 index 000000000..94338ab31 --- /dev/null +++ b/pkg/networking/logging.go @@ -0,0 +1,18 @@ +package networking + +import ( + "context" + "log/slog" +) + +// TODO: Remove this file and the handler when the default [slog.DiscardHandler] will be introduced in +// Go version 1.24. See https://go-review.googlesource.com/c/go/+/626486. + +// discardingHandler is a logger that discards all log messages. +// It is used when no slog handler is provided in the [Config]. +type discardingHandler struct{} + +func (h discardingHandler) Enabled(context.Context, slog.Level) bool { return false } +func (h discardingHandler) Handle(context.Context, slog.Record) error { return nil } +func (h discardingHandler) WithAttrs([]slog.Attr) slog.Handler { return h } +func (h discardingHandler) WithGroup(string) slog.Handler { return h } diff --git a/pkg/networking/network.go b/pkg/networking/network.go index df36a64fa..f145b4cb1 100644 --- a/pkg/networking/network.go +++ b/pkg/networking/network.go @@ -16,6 +16,12 @@ var ( // ErrInvalidConfigurationNoHandler is used when the configuration has no handler. ErrInvalidConfigurationNoHandler = errors.New("invalid configuration: empty handler") + // ErrInvalidConfigurationNoKeepAliveInterval is used when the configuration has an invalid keep-alive interval. + ErrInvalidConfigurationNoKeepAliveInterval = errors.New("invalid configuration: invalid keep-alive interval value") + + // ErrInvalidConfigurationNoWriteTimeout is used when the configuration has an invalid write timeout. + ErrInvalidConfigurationNoWriteTimeout = errors.New("invalid configuration: invalid write timeout value") + // ErrUnacceptableHandshake is used when the handshake is not accepted. ErrUnacceptableHandshake = errors.New("handshake is not accepted") diff --git a/pkg/networking/session.go b/pkg/networking/session.go index e7f3eba6c..5723b1913 100644 --- a/pkg/networking/session.go +++ b/pkg/networking/session.go @@ -50,9 +50,16 @@ func newSession(ctx context.Context, config *Config, conn io.ReadWriteCloser, tp if config.handler == nil { return nil, ErrInvalidConfigurationNoHandler } + if config.keepAlive && config.keepAliveInterval <= 0 { + return nil, ErrInvalidConfigurationNoKeepAliveInterval + } + if config.connectionWriteTimeout <= 0 { + return nil, ErrInvalidConfigurationNoWriteTimeout + } if tp == nil { return nil, ErrEmptyTimerPool } + sCtx, cancel := context.WithCancel(ctx) s := &Session{ g: execution.NewTaskGroup(suppressContextCancellationError), @@ -65,17 +72,16 @@ func newSession(ctx context.Context, config *Config, conn io.ReadWriteCloser, tp sendCh: make(chan *sendPacket, 1), // TODO: Make the size of send channel configurable. } - attributes := []any{ - slog.String("namespace", Namespace), - slog.String("remote", s.RemoteAddr().String()), + if config.slogHandler == nil { + config.slogHandler = discardingHandler{} } - attributes = append(attributes, config.attributes...) - if config.logger == nil { - s.logger = slog.Default().With(attributes...) - } else { - s.logger = config.logger.With(attributes...) + sa := [...]any{ + slog.String("namespace", Namespace), + slog.String("remote", s.RemoteAddr().String()), } + attrs := append(sa[:], config.attributes...) + s.logger = slog.New(config.slogHandler).With(attrs...) s.g.Run(s.receiveLoop) s.g.Run(s.sendLoop) diff --git a/pkg/networking/session_test.go b/pkg/networking/session_test.go index 51fbd8369..cbeb9ad8e 100644 --- a/pkg/networking/session_test.go +++ b/pkg/networking/session_test.go @@ -385,7 +385,7 @@ func TestCloseParentContext(t *testing.T) { func testConfig(t testing.TB, p networking.Protocol, h networking.Handler, direction string) *networking.Config { log := slogt.New(t) return networking.NewConfig(p, h). - WithLogger(log). + WithSlogHandler(log.Handler()). WithWriteTimeout(1 * time.Second). WithKeepAliveDisabled(). WithSlogAttribute(slog.String("direction", direction))