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

panic() variant of Set() for config #64

Closed
tj opened this issue Jul 15, 2014 · 8 comments
Closed

panic() variant of Set() for config #64

tj opened this issue Jul 15, 2014 · 8 comments

Comments

@tj
Copy link
Contributor

tj commented Jul 15, 2014

I'm getting IO timeouts with nsqd v0.2.28 (built w/go1.2.1) for anything blocking for a second, which I'll need to fix anyway, but more importantly I didn't realize that Set() returns instead of a panic. I probably just don't have master with the msg_timeout support, but having a MustSet would be great for simple bootstrapping, dev error in my case. yay/nay?

@tj
Copy link
Contributor Author

tj commented Jul 15, 2014

also realized the client doesn't reconnect for some reason, nothing to report log-wise

@mreiferson
Copy link
Member

Are you using lookupd? If not, it uses a simple 15 second timeout after disconnection.

I'd be fine adding MustSet, go for it.

@jehiah
Copy link
Member

jehiah commented Jul 15, 2014

If you want to panic on errors you can just do the following. I normally think of Must* methods as not panicing, but being guaranteed to return. Since we are in library mode i think it's generally Go style to prefer errors over panic's (though there are a few cases where we use them).

TL;DR; i'm not sure there is a strong enough reason for a second function to Set config values.

if err := cfg.Set(...); err != nil {
   panic(err.Error())
}

@tj
Copy link
Contributor Author

tj commented Jul 15, 2014

Ah I thought the pattern was must return or panic, still a Go noob. I'm not using lookupd in this case, and I just remembered this morning that I've had errors from my local dev nsqd saying msg_timeout was too high so it can't be that, I'll sift through with --verbose

@tj tj closed this as completed Jul 15, 2014
@mreiferson
Copy link
Member

@visionmedia you're right, that is the pattern (return or panic). @jehiah we kinda screwed this up in our go-simplejson API, which isn't actually consistent with the Go standard library's use of Must.

Anyway, it is debatable whether having another set method would do the trick. I am sympathetic to the idea that the library should try to help you do the right thing as easily as possible, but having two methods might not be the path forward.

I feel like, at one point, I had written Set to panic, but there are other issues with a library that panics unexpectedly.

Really, the golden rule is to always always always check returned errors when present.

@chris-baynes
Copy link

I just came across a similar issue trying to set lookupd_poll_interval to less than the minimum allowed. Not realising Set returned an error, my value was silently ignored.

I see there is also a public Validate method that's used by the producer and consumer to check config validity. Wouldn't it then be better to just set the potentially invalid values, then just return an error from Validate?

The reason I prefer this approach is that wrapping each Set in an if err != nil is quite verbose when a few values need to be set, this would also mean the values are only validated once.

What do you guys think?

@jehiah
Copy link
Member

jehiah commented Jul 16, 2014

In #52 we re-exported Config values for backwards compatability, and to maintain a type safe way to set config values (or at least one that is caught at compile time). That leaves two ways to set config values; one is using the config struct directly which is ideal for Go code, and the second, through Set() is ideal for parsing config files, or text command line arguments.

Because we allow the former it's possible to set out-of-range values on some items so Validate() asserts that everything is within the allowable range for some config values.

I agree that error handling can be verbose, but it's the Go way, and I find the more I accept that up front I avoid unexpected states in the future. (I'll also say that was a particular style i was personally slow to adopt switching to Go from Python).

To take direction from the stdlib, If os.Hostname() returns an error to indicate there are obscure cases where it can fail (instead of panicing), that pattern that applies to Config.Set().

@tj
Copy link
Contributor Author

tj commented Jul 16, 2014

I'm cool with the behaviour, just missed the error. I'll just check(sub.Set()) in my case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants