From 8eb565e2e56235c33baa20375af4de3063e17dad Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Mon, 16 Dec 2024 22:11:09 -0600 Subject: [PATCH] refactor: move listener and enable port re-use (#289) This migrates the listeners from the internal node entrypoint to the node framework, which enables re-use of the listening port as the outbound source port --- connmanager/listener.go | 15 ++++++++++++++ internal/node/node.go | 45 +++++++---------------------------------- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/connmanager/listener.go b/connmanager/listener.go index ae6b6c7..953c98a 100644 --- a/connmanager/listener.go +++ b/connmanager/listener.go @@ -58,6 +58,21 @@ func (c *ConnectionManager) startListener(l ListenerConfig) error { return fmt.Errorf("failed to open listening socket: %s", err) } l.Listener = listener + if l.UseNtC { + c.config.Logger.Info( + fmt.Sprintf( + "listening for ouroboros node-to-node connections on %s", + l.ListenAddress, + ), + ) + } else { + c.config.Logger.Info( + fmt.Sprintf( + "listening for ouroboros node-to-client connections on %s", + l.ListenAddress, + ), + ) + } } // Build connection options defaultConnOpts := []ouroboros.ConnectionOptionFunc{ diff --git a/internal/node/node.go b/internal/node/node.go index 13bb3fb..0a522c1 100644 --- a/internal/node/node.go +++ b/internal/node/node.go @@ -17,7 +17,6 @@ package node import ( "fmt" "log/slog" - "net" "net/http" _ "net/http/pprof" "os" @@ -40,44 +39,10 @@ func Run(logger *slog.Logger) error { fmt.Sprintf("topology: %+v", config.GetTopologyConfig()), "component", "node", ) - tcpNtN, err := net.Listen( - "tcp", - fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port), - ) - if err != nil { - return err - } - logger.Info( - fmt.Sprintf( - "listening for ouroboros node-to-node connections on %s", - fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port), - ), - "component", "node", - ) // TODO: make this safer, check PID, create parent, etc. if _, err := os.Stat(cfg.SocketPath); err == nil { os.Remove(cfg.SocketPath) } - socketNtC, err := net.Listen("unix", cfg.SocketPath) - if err != nil { - logger.Error( - fmt.Sprintf( - "failed listening on socket: %s: %+v: %+v", - cfg.SocketPath, - socketNtC, - err, - ), - "component", "node", - ) - return err - } - logger.Info( - fmt.Sprintf( - "listening for ouroboros node-to-client connections on socket %s", - cfg.SocketPath, - ), - "component", "node", - ) var nodeCfg *cardano.CardanoNodeConfig if cfg.CardanoConfig != "" { tmpCfg, err := cardano.NewCardanoNodeConfigFromFile(cfg.CardanoConfig) @@ -102,13 +67,17 @@ func Run(logger *slog.Logger) error { dingo.WithCardanoNodeConfig(nodeCfg), dingo.WithListeners( dingo.ListenerConfig{ - Listener: tcpNtN, + ListenNetwork: "tcp", + ListenAddress: fmt.Sprintf("%s:%d", cfg.BindAddr, cfg.Port), + ReuseAddress: true, }, dingo.ListenerConfig{ - Listener: socketNtC, - UseNtC: true, + ListenNetwork: "unix", + ListenAddress: cfg.SocketPath, + UseNtC: true, }, ), + dingo.WithOutboundSourcePort(cfg.Port), // Enable metrics with default prometheus registry dingo.WithPrometheusRegistry(prometheus.DefaultRegisterer), // TODO: make this configurable