Skip to content

Commit

Permalink
refactor: pass in listener to the log receiver
Browse files Browse the repository at this point in the history
This allows to easily bind to a random part and learn that port.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
smira committed Nov 29, 2021
1 parent d86cdd5 commit d0612a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 7 additions & 1 deletion cmd/siderolink-agent/log_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"context"
"net"

"go.uber.org/zap"
"golang.org/x/sync/errgroup"
Expand All @@ -25,7 +26,12 @@ func logHandler(logger *zap.Logger) logreceiver.Handler {
}

func logReceiver(ctx context.Context, eg *errgroup.Group, logger *zap.Logger) error {
srv, err := logreceiver.NewServer(logger, logReceiverFlags.endpoint, logHandler(logger))
lis, err := net.Listen("tcp", logReceiverFlags.endpoint)
if err != nil {
return err
}

srv, err := logreceiver.NewServer(logger, lis, logHandler(logger))
if err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/logreceiver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@ type Server struct {
// Handler is called for each received message.
type Handler func(srcAddress netaddr.IP, msg map[string]interface{})

// NewServer initializes new Server and starts listening.
func NewServer(logger *zap.Logger, listenAddress string, handler Handler) (*Server, error) {
lis, err := net.Listen("tcp", listenAddress)
if err != nil {
return nil, fmt.Errorf("error listening: %w", err)
}

// NewServer initializes new Server.
func NewServer(logger *zap.Logger, listener net.Listener, handler Handler) (*Server, error) {
return &Server{
listener: lis,
listener: listener,
logger: logger,
handler: handler,
}, nil
Expand Down

0 comments on commit d0612a7

Please sign in to comment.