-
Notifications
You must be signed in to change notification settings - Fork 711
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
Changes from 6 commits
2231281
c96aceb
74eb51c
1b30ec7
14704f1
c0af51d
bb67532
1eabd60
775b395
d9bf42b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,58 @@ func (nc *Conn) RequestWithContext(ctx context.Context, subj string, data []byte | |
if ctx == nil { | ||
return nil, ErrInvalidContext | ||
} | ||
if nc == nil { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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%. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
There was a problem hiding this comment.
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.