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

Return an alternative human readable version of the connection status #807

Closed
josephwoodward opened this issue Aug 27, 2021 · 4 comments
Closed
Labels
feature New feature request

Comments

@josephwoodward
Copy link
Contributor

Feature Request

Currently the NATS connection status is a constant backed by an iota, this means consumers wishing to write out the connection status as a human readable format (such as logs) requires something like this:

func natsStatus(status nats.Status) string {
	switch status {
	case nats.DISCONNECTED:
		return "DISCONNECTED"
	case nats.CONNECTED:
		return "CONNECTED"
         ...
	default:
		return "unknown status"
	}
}

It would be great if the client library could provide such a functionality to get a human readable version of the connection status.

Proposed Change:

My initial thoughts were perhaps a pointer function such as func (nc *Conn) StatusText() string that returns the uppercase string of the connection status.

Alternative Approaches

Whilst a slightly larger, breaking change - another option is to make it a type like so, which could then implement the Stringer interface.

type direction int

const (
	CONNECTED direction = iota
	DISCONNECTED
)

func (d direction) String() string {
	return [...]string{"CONNECTED", "DISCONNECTED"}[d]
}
@josephwoodward josephwoodward added the feature New feature request label Aug 27, 2021
@kozlovic
Copy link
Member

Or simply do this:

func (s Status) String() string {
	switch s {
	case DISCONNECTED:
		return "DISCONNECTED"
	case CONNECTED:
		return "CONNECTED"
	case CLOSED:
		return "CLOSED"
	case RECONNECTING:
		return "RECONNECTING"
	case CONNECTING:
		return "CONNECTING"
	case DRAINING_SUBS:
		return "DRAINING_SUBS"
	case DRAINING_PUBS:
		return "DRAINING_PUBS"
	}
	return "unknown status"
}

which seem to work:

nc, err := nats.Connect(...)
..
st := nc.Status()
fmt.Printf("status=%s\n", st)

this prints:

status=CONNECTED

@josephwoodward
Copy link
Contributor Author

@kozlovic Ah yes, of course - that would be much better. Would you be happy for me to contribute this to the client library?

@kozlovic
Copy link
Member

kozlovic commented Sep 1, 2021

@josephwoodward Sure, if you would like!

@kozlovic
Copy link
Member

kozlovic commented Sep 2, 2021

Close by PR #812

@kozlovic kozlovic closed this as completed Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request
Projects
None yet
Development

No branches or pull requests

2 participants