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] Ability to retrieve client ID from server currently connected to #395

Merged
merged 3 commits into from
Oct 15, 2018

Conversation

kozlovic
Copy link
Member

Server 1.2.0 returns the client's ID (cid) through the INFO protocol.
An API is added to retrieve this value. This can be useful for
debugging and monitoring.

Resolves #394

Signed-off-by: Ivan Kozlovic [email protected]

Server 1.2.0 returns the client's ID (cid) through the INFO protocol.
An API is added to retrieve this value. This can be useful for
debugging and monitoring.

Resolves #394

Signed-off-by: Ivan Kozlovic <[email protected]>
@kozlovic
Copy link
Member Author

@derekcollison Not sure of the naming GetCID() and also if we should return error or not. That is, 0 would be if connected to older server but also if not currently connected. Right now, the cid and error are returned.

@coveralls
Copy link

coveralls commented Sep 26, 2018

Coverage Status

Coverage increased (+0.02%) to 94.303% when pulling 1dbc51c on add_get_cid into 0856137 on master.

nats.go Outdated
// client reconnects. Also, returned value may be 0 if connecting
// to a server pre 1.2.0, which did not send the CID back to the
// client.
func (nc *Conn) GetCID() (uint64, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetClientID?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

nats.go Outdated

// GetCID returns the client ID assigned by the server the client
// is currently connected to. Note that the value would change if
// client reconnects. Also, returned value may be 0 if connecting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return an error if not provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please comment on error variable name and content

func (nc *Conn) GetCID() (uint64, error) {
nc.mu.Lock()
defer nc.mu.Unlock()
if nc.isClosed() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 0 return an error that it was not provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
defer nc2.Close()

// Stop server A, nc1 will reconnect to B, and should have different CID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be the same since CID is scoped to the server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No since I made sure that there was another client connected to server B prior to killing A and having nc1 reconnect to server B.

@kozlovic kozlovic changed the title [ADDED] Ability to retrieve CID from server currently connected to [ADDED] Ability to retrieve client ID from server currently connected to Sep 27, 2018
Signed-off-by: Ivan Kozlovic <[email protected]>
@kozlovic
Copy link
Member Author

@derekcollison Made some changes and also added a test with fake server returning no CID and making sure GetClientID() returns proper error.

Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment on a comment and about 0, otherwise LGTM.

nats.go Outdated
@@ -93,6 +93,7 @@ var (
ErrInvalidArg = errors.New("nats: invalid argument")
ErrInvalidContext = errors.New("nats: invalid context")
ErrNoEchoNotSupported = errors.New("nats: no echo option not supported by this server")
ErrNoClientIDReturned = errors.New("nats: client ID not returned by this server")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say not supported, similar to the no echo option above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok will change.

nc.mu.Lock()
defer nc.mu.Unlock()
if nc.isClosed() {
return 0, ErrConnectionClosed
}
if nc.info.CID == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any better way to make sure this was not included in the INFO json? I doubt we would wrap an uint64 but if we did I think it would show up as a zero.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then the server should make sure that if it wraps, then it start again to 1.
I guess we could search "client_id" in the info string we get before doing unmarshal and if not present have a flag in connection that says cid not set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe post an issue against the server.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, is it really worth it? I mean at 10M new connections a second, it would take 58,494 years without restarting the server before it wraps (if my math is correct).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so will change the error name/content and update

Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kozlovic kozlovic merged commit 208e287 into master Oct 15, 2018
@kozlovic kozlovic deleted the add_get_cid branch October 15, 2018 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants