Skip to content

Commit

Permalink
refactor: move listener and enable port re-use (#289)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
agaffney authored Dec 17, 2024
1 parent 4aadb1c commit 8eb565e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
15 changes: 15 additions & 0 deletions connmanager/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
45 changes: 7 additions & 38 deletions internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package node
import (
"fmt"
"log/slog"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 8eb565e

Please sign in to comment.