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] context.Context support #275

Merged
merged 6 commits into from
May 11, 2017

Conversation

wallyqs
Copy link
Member

@wallyqs wallyqs commented Apr 10, 2017

Adds context.Context support to the +Go1.7 clients implementing the APIs suggested at https://github.com/nats-io/go-nats/issues/221#issuecomment-288193653

func (c *EncodedConn) RequestWithContext(ctx context.Context, subject string, v interface{}, vPtr interface{}) error
func (nc *Conn) RequestWithContext(ctx context.Context, subj string, data []byte) (*Msg, error)
func (s *Subscription) NextMsgWithContext(ctx context.Context) (*Msg, error)

Fixes #221

context.go Outdated
// in bytes and request expecting a single response.
func (nc *Conn) RequestWithContext(ctx context.Context, subj string, data []byte) (*Msg, error) {
if ctx == nil {
panic("nil context")
Copy link
Member

Choose a reason for hiding this comment

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

In general I don't think we have panics in the client code. We should either error or ignore a nil context.

Copy link
Member Author

@wallyqs wallyqs Apr 10, 2017

Choose a reason for hiding this comment

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

I agree on that, so for now I have removed the checks to not introduce calling panic in the nats client.

Here I was borrowing from the http, net and other libraries where panic is being called...

...though in other packages like sql package those checks aren't introduced either so switched to follow that style instead:

Copy link
Member

Choose a reason for hiding this comment

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

@wallyqs You have removed the panic on nil context, but is it not going to panic any way when dereferencing it in the code? (unless context's method themselves do nothing if context is nil).

Copy link
Member Author

Choose a reason for hiding this comment

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

yes if nil it would panic when calling ctx.Done() anyway as you mention

context.go Outdated
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
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 statement be under default?

context.go Outdated
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

context.go Outdated
// context gets canceled.
func (s *Subscription) NextMsgWithContext(ctx context.Context) (*Msg, error) {
if ctx == nil {
panic("nil context")
Copy link
Member

Choose a reason for hiding this comment

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

Same here for panic in client code.

context.go Outdated
subject string,
v interface{},
vPtr interface{},
) error {
Copy link
Member

Choose a reason for hiding this comment

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

This proper formatting?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed formatting here...

context.go Outdated
vPtr interface{},
) error {
if ctx == nil {
panic("nil context")
Copy link
Member

Choose a reason for hiding this comment

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

Again on panics.

context.go Outdated
select {
case <-ctx.Done():
return ctx.Err()
default:
Copy link
Member

Choose a reason for hiding this comment

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

Return err as part of default.

t.Fatalf("Expected request with timeout context to fail: %s", err)
}

// Reported error is "context canceled" from Context package,
Copy link
Member

Choose a reason for hiding this comment

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

Cancelled vs canceled, I think both are valid, but we should be consistent in spelling.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks Derek, I changed throughout to use canceled which is the one used in the go repo:

https://github.com/golang/go/blob/dc6af19ff8b44e56abc1217af27fe098c78c932b/src/context/context.go#L155

if _, ok := err.(timeoutError); ok {
t.Errorf("Expected to not have a timeout error")
}
expected = `context canceled`
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

@wallyqs wallyqs force-pushed the add-context-support branch 2 times, most recently from 1533a2c to a23e4a4 Compare April 10, 2017 18:21
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.7%) to 94.391% when pulling a23e4a4 on wallyqs:add-context-support into 6949c8e on nats-io:master.

@derekcollison
Copy link
Member

LGTM

Waldemar Quevedo added 4 commits May 11, 2017 09:57
Add context aware APIs based on original ones

Rename prefix from context tests

Add context integration to encoded requests
Add processNextMsgDelivered and reuse in NextMsg
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.7%) to 94.428% when pulling 855d5bf on wallyqs:add-context-support into 5de724c on nats-io:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.5%) to 94.655% when pulling 608bbb9 on wallyqs:add-context-support into 5de724c on nats-io:master.

@kozlovic
Copy link
Member

LGTM

@kozlovic kozlovic changed the title Add context.Context support [ADDED] context.Context support May 11, 2017
@derekcollison derekcollison merged commit 7764638 into nats-io:master May 11, 2017
@wallyqs wallyqs deleted the add-context-support branch May 11, 2017 22:41
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.

5 participants