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

More protocol friendly request pattern. Fix for #294. #295

Merged
merged 10 commits into from
May 27, 2017
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: go
sudo: false
go:
- 1.8.1
- 1.8.3
- 1.7.6
- 1.6.4
- 1.7.5
install:
- go get -t ./...
- go get github.com/nats-io/gnatsd
Expand Down
51 changes: 51 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,58 @@ func (nc *Conn) RequestWithContext(ctx context.Context, subj string, data []byte
if ctx == nil {
return nil, ErrInvalidContext
}
if nc == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Code coverage wise, this is not tested.

return nil, ErrInvalidConnection
}

// snapshot
var doSetup, useOldRequestStyle bool
nc.mu.Lock()
useOldRequestStyle = nc.Opts.UseOldRequestStyle
doSetup = (nc.respMux == nil)
nc.mu.Unlock()

// If user wants the old style.
if useOldRequestStyle {
Copy link
Member

Choose a reason for hiding this comment

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

Code coverage wise, this is not tested.

return nc.oldRequestWithContext(ctx, subj, data)
}

// Make sure scoped subscription is setup at least once on first
// call to Request(). Will handle duplicates in createRespMux.
if doSetup {
if err := nc.createRespMux(); err != nil {
return nil, err
}
}
// Create literal Inbox and map to a chan msg.
mch := make(chan *Msg, RequestChanLen)
nc.mu.Lock()
respInbox := nc.newRespInbox()
nc.respMap[respToken(respInbox)] = mch
nc.mu.Unlock()

err := nc.PublishRequest(subj, respInbox, data)
if err != nil {
return nil, err
}

var ok bool
var msg *Msg

select {
case msg, ok = <-mch:
if !ok {
return nil, ErrConnectionClosed
}
case <-ctx.Done():
return nil, ctx.Err()
}

return msg, nil
}

// oldRequestWithContext utilizes inbox and subscription per request.
Copy link
Member

Choose a reason for hiding this comment

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

This whole function shows up as not covered, which brings the code coverage of this file down to 71%.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix..

func (nc *Conn) oldRequestWithContext(ctx context.Context, subj string, data []byte) (*Msg, error) {
inbox := NewInbox()
ch := make(chan *Msg, RequestChanLen)

Expand Down
Loading