Skip to content

Commit

Permalink
feat: wire default discard connection logger to protocol configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <wolf31o2@blinklabs.io>
  • Loading branch information
wolf31o2 committed Oct 2, 2024
1 parent a5a62f9 commit 2eee63b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) {
protoErrorChan: make(chan error, 10),
handshakeFinishedChan: make(chan interface{}),
doneChan: make(chan interface{}),
// Create a discard logger to throw away logs. We do this so
// we don't have to add guards around every log operation if
// a logger is not configured by the user.
logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
}
// Apply provided options functions
for _, option := range options {
Expand Down Expand Up @@ -287,6 +291,7 @@ func (c *Connection) setupConnection() error {
protoOptions := protocol.ProtocolOptions{
ConnectionId: c.id,
Muxer: c.muxer,
Logger: c.logger,
ErrorChan: c.protoErrorChan,
}
if c.useNodeToNodeProto {
Expand Down
15 changes: 14 additions & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Blink Labs Software
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ import (
"bytes"
"fmt"
"io"
"log/slog"
"sync"
"time"

Expand Down Expand Up @@ -53,6 +54,7 @@ type ProtocolConfig struct {
ProtocolId uint16
ErrorChan chan error
Muxer *muxer.Muxer
Logger *slog.Logger
Mode ProtocolMode
Role ProtocolRole
MessageHandlerFunc MessageHandlerFunc
Expand Down Expand Up @@ -85,6 +87,7 @@ const (
type ProtocolOptions struct {
ConnectionId connection.ConnectionId
Muxer *muxer.Muxer
Logger *slog.Logger
ErrorChan chan error
Mode ProtocolMode
// TODO: remove me
Expand Down Expand Up @@ -118,6 +121,7 @@ func New(config ProtocolConfig) *Protocol {
func (p *Protocol) Start() {
p.onceStart.Do(func() {
// Register protocol with muxer
p.Logger().Debug("registering protocol with muxer")
muxerProtocolRole := muxer.ProtocolRoleInitiator
if p.config.Role == ProtocolRoleServer {
muxerProtocolRole = muxer.ProtocolRoleResponder
Expand Down Expand Up @@ -156,6 +160,7 @@ func (p *Protocol) Start() {
func (p *Protocol) Stop() {
p.onceStop.Do(func() {
// Unregister protocol from muxer
p.Logger().Debug("unregistering protocol with muxer")
muxerProtocolRole := muxer.ProtocolRoleInitiator
if p.config.Role == ProtocolRoleServer {
muxerProtocolRole = muxer.ProtocolRoleResponder
Expand All @@ -167,6 +172,14 @@ func (p *Protocol) Stop() {
})
}

// Logger returns the protocol logger
func (p *Protocol) Logger() *slog.Logger {
if p.config.Logger == nil {
return slog.New(slog.NewJSONHandler(io.Discard, nil))
}
return p.config.Logger
}

// Mode returns the protocol mode
func (p *Protocol) Mode() ProtocolMode {
return p.config.Mode
Expand Down

0 comments on commit 2eee63b

Please sign in to comment.