Skip to content

Commit

Permalink
Session configuration accepts slog handler to set up logging.
Browse files Browse the repository at this point in the history
Discarding slog handler implemented and used instead of setting default slog logger.
Checks on interval values added to Session constructor.
  • Loading branch information
alexeykiselev committed Dec 16, 2024
1 parent 294cf66 commit 57b9ffb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion itests/clients/net_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
9 changes: 4 additions & 5 deletions pkg/networking/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/networking/logging.go
Original file line number Diff line number Diff line change
@@ -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 }
6 changes: 6 additions & 0 deletions pkg/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
22 changes: 14 additions & 8 deletions pkg/networking/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/networking/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 57b9ffb

Please sign in to comment.