Skip to content
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

[ADDED] Conn.ConnectedAddr() #426

Merged
merged 1 commit into from
Jan 17, 2019
Merged
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
13 changes: 13 additions & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,19 @@ func (nc *Conn) ConnectedUrl() string {
return nc.current.url.String()
}

// ConnectedAddr returns the connected server's IP
func (nc *Conn) ConnectedAddr() string {
if nc == nil {
return _EMPTY_
}
nc.mu.Lock()
defer nc.mu.Unlock()
if nc.status != CONNECTED {
return _EMPTY_
}
return nc.conn.RemoteAddr().String()
}

// Report the connected server's Id
func (nc *Conn) ConnectedServerId() string {
if nc == nil {
Expand Down
22 changes: 22 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,3 +1592,25 @@ func TestLookupHostResultIsRandomized(t *testing.T) {
}
t.Fatalf("Always used first address returned by LookupHost")
}

func TestConnectedAddr(t *testing.T) {
s := RunServerOnPort(TEST_PORT)
defer s.Shutdown()

var nc *Conn
if addr := nc.ConnectedAddr(); addr != _EMPTY_ {
t.Fatalf("Expected empty result for nil connection, got %q", addr)
}
nc, err := Connect(fmt.Sprintf("localhost:%d", TEST_PORT))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}
expected := s.Addr().String()
if addr := nc.ConnectedAddr(); addr != expected {
t.Fatalf("Expected address %q, got %q", expected, addr)
}
nc.Close()
if addr := nc.ConnectedAddr(); addr != _EMPTY_ {
t.Fatalf("Expected empty result for closed connection, got %q", addr)
}
}