Skip to content

Add CustomDialer option for LeafNodes #6907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion server/leafnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,11 @@ func (s *Server) connectToRemoteLeafNode(remote *leafNodeCfg, firstConnect bool)
err = ErrLeafNodeDisabled
} else {
s.Debugf("Trying to connect as leafnode to remote server on %q%s", rURL.Host, ipStr)
conn, err = natsDialTimeout("tcp", url, dialTimeout)
if opts.LeafNode.CustomDialer != nil {
conn, err = opts.LeafNode.CustomDialer.Dial("tcp", url)
} else {
conn, err = natsDialTimeout("tcp", url, dialTimeout)
}
}
}
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ type RemoteGatewayOpts struct {
tlsConfigOpts *TLSConfigOpts
}

type CustomDialer interface {
Dial(network, address string) (net.Conn, error)
}

// LeafNodeOpts are options for a given server to accept leaf node connections and/or connect to a remote cluster.
type LeafNodeOpts struct {
Host string `json:"addr,omitempty"`
Expand Down Expand Up @@ -193,6 +197,9 @@ type LeafNodeOpts struct {

// Snapshot of configured TLS options.
tlsConfigOpts *TLSConfigOpts

// CustomDialer is used to override the default dialer.
CustomDialer CustomDialer
}

// SignatureHandler is used to sign a nonce from the server while
Expand Down
40 changes: 40 additions & 0 deletions test/leafnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@
return RunServer(&o), &o
}

func runSolicitLeafServerCustomDialer(lso *server.Options, dialer server.CustomDialer) (*server.Server, *server.Options) {
surl := fmt.Sprintf("nats-leaf://%s:%d", lso.LeafNode.Host, lso.LeafNode.Port)
o := DefaultTestOptions
o.Port = -1
o.NoSystemAccount = true
rurl, _ := url.Parse(surl)
o.LeafNode.Remotes = []*server.RemoteLeafOpts{{URLs: []*url.URL{rurl}}}
o.LeafNode.ReconnectInterval = 100 * time.Millisecond
o.LeafNode.CustomDialer = dialer
return RunServer(&o), &o
}

func TestLeafNodeInfo(t *testing.T) {
s, opts := runLeafServer()
defer s.Shutdown()
Expand Down Expand Up @@ -526,6 +538,34 @@
checkLeafNodeConnected(t, s)
}

type TestDialer struct{

Check failure on line 541 in test/leafnode_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (goimports)
}

func (td TestDialer) Dial(network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: 1 * time.Second,
KeepAlive: -1,
}
return d.Dial(network, address)
}

func TestLeafNodeSolicitCustomDialer(t *testing.T) {
s, opts := runLeafServer()
defer s.Shutdown()

sl, _ := runSolicitLeafServerCustomDialer(opts,&TestDialer{})
defer sl.Shutdown()

checkLeafNodeConnected(t, s)

// Now test reconnect.
s.Shutdown()
// Need to restart it on the same port.
s, _ = runLeafServerOnPort(opts.LeafNode.Port)
defer s.Shutdown()
checkLeafNodeConnected(t, s)
}

func TestLeafNodeNoEcho(t *testing.T) {
s, opts := runLeafServer()
defer s.Shutdown()
Expand Down
Loading