Skip to content

Commit

Permalink
Merge pull request #603 from ripienaar/expose_additional_info
Browse files Browse the repository at this point in the history
expose additional info line information to the client
  • Loading branch information
ripienaar committed Nov 11, 2020
2 parents 9958a9d + 1044b72 commit 9e1f4a0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
51 changes: 41 additions & 10 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,19 +590,20 @@ type srv struct {
// The INFO block received from the server.
type serverInfo struct {
ID string `json:"server_id"`
Name string `json:"server_name"`
Proto int `json:"proto"`
Host string `json:"host"`
Port uint `json:"port"`
Version string `json:"version"`
AuthRequired bool `json:"auth_required"`
TLSRequired bool `json:"tls_required"`
TLSAvailable bool `json:"tls_available"`
Port int `json:"port"`
Headers bool `json:"headers"`
AuthRequired bool `json:"auth_required,omitempty"`
TLSRequired bool `json:"tls_required,omitempty"`
TLSAvailable bool `json:"tls_available,omitempty"`
MaxPayload int64 `json:"max_payload"`
ConnectURLs []string `json:"connect_urls,omitempty"`
Proto int `json:"proto,omitempty"`
CID uint64 `json:"client_id,omitempty"`
ClientIP string `json:"client_ip,omitempty"`
Nonce string `json:"nonce,omitempty"`
Cluster string `json:"cluster,omitempty"`
ConnectURLs []string `json:"connect_urls,omitempty"`
LameDuckMode bool `json:"ldm,omitempty"`
}

Expand Down Expand Up @@ -1489,7 +1490,7 @@ func (nc *Conn) waitForExits() {
nc.wg.Wait()
}

// Report the connected server's Url
// ConnectedUrl reports the connected server's URL
func (nc *Conn) ConnectedUrl() string {
if nc == nil {
return _EMPTY_
Expand Down Expand Up @@ -1519,7 +1520,7 @@ func (nc *Conn) ConnectedAddr() string {
return nc.conn.RemoteAddr().String()
}

// Report the connected server's Id
// ConnectedServerId reports the connected server's Id
func (nc *Conn) ConnectedServerId() string {
if nc == nil {
return _EMPTY_
Expand All @@ -1534,6 +1535,36 @@ func (nc *Conn) ConnectedServerId() string {
return nc.info.ID
}

// ConnectedServerName reports the connected server's name
func (nc *Conn) ConnectedServerName() string {
if nc == nil {
return _EMPTY_
}

nc.mu.RLock()
defer nc.mu.RUnlock()

if nc.status != CONNECTED {
return _EMPTY_
}
return nc.info.Name
}

// ConnectedClusterName reports the connected server's cluster name if any
func (nc *Conn) ConnectedClusterName() string {
if nc == nil {
return _EMPTY_
}

nc.mu.RLock()
defer nc.mu.RUnlock()

if nc.status != CONNECTED {
return _EMPTY_
}
return nc.info.Cluster
}

// Low level setup for structs, etc
func (nc *Conn) setup() {
nc.subs = make(map[int64]*Subscription)
Expand Down Expand Up @@ -2599,7 +2630,7 @@ func (nc *Conn) processInfo(info string) error {
// if advertise is disabled on that server, or servers that
// did not include themselves in the async INFO protocol.
// If empty, do not remove the implicit servers from the pool.
if len(ncInfo.ConnectURLs) == 0 {
if len(nc.info.ConnectURLs) == 0 {
if !nc.initc && ncInfo.LameDuckMode && nc.Opts.LameDuckModeHandler != nil {
nc.ach.push(func() { nc.Opts.LameDuckModeHandler(nc) })
}
Expand Down
1 change: 0 additions & 1 deletion nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,6 @@ func TestAsyncINFO(t *testing.T) {
ID: "test",
Host: "localhost",
Port: 4222,
Version: "1.2.3",
AuthRequired: true,
TLSRequired: true,
MaxPayload: 2 * 1024 * 1024,
Expand Down
29 changes: 23 additions & 6 deletions test/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,35 @@ func TestConnectedServer(t *testing.T) {
if u == "" || u != nats.DefaultURL {
t.Fatalf("Unexpected connected URL of %s\n", u)
}
srv := nc.ConnectedServerId()
if srv == "" {
t.Fatal("Expected a connected server id")
id := nc.ConnectedServerId()
if id == "" {
t.Fatalf("Expected a connected server id, got %s", id)
}
name := nc.ConnectedServerName()
if name == "" {
t.Fatalf("Expected a connected server name, got %s", name)
}
cname := nc.ConnectedClusterName()
if cname == "" {
t.Fatalf("Expected a connected server cluster name, got %s", cname)
}

nc.Close()
u = nc.ConnectedUrl()
if u != "" {
t.Fatalf("Expected a nil connected URL, got %s\n", u)
}
srv = nc.ConnectedServerId()
if srv != "" {
t.Fatalf("Expected a nil connect server, got %s\n", srv)
id = nc.ConnectedServerId()
if id != "" {
t.Fatalf("Expected a nil connect server, got %s", id)
}
name = nc.ConnectedServerName()
if name != "" {
t.Fatalf("Expected a nil connect server name, got %s", name)
}
cname = nc.ConnectedClusterName()
if cname != "" {
t.Fatalf("Expected a nil connect server cluster, got %s", cname)
}
}

Expand Down
1 change: 1 addition & 0 deletions test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func RunDefaultServer() *server.Server {
func RunServerOnPort(port int) *server.Server {
opts := natsserver.DefaultTestOptions
opts.Port = port
opts.Cluster.Name = "testing"
return RunServerWithOptions(opts)
}

Expand Down

0 comments on commit 9e1f4a0

Please sign in to comment.