-
Notifications
You must be signed in to change notification settings - Fork 717
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 1 commit
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 |
---|---|---|
|
@@ -1968,18 +1968,20 @@ func (nc *Conn) respHandler(m *Msg) { | |
rt := respToken(m.Subject) | ||
|
||
nc.mu.Lock() | ||
// Just return if closed, let Request timeout. | ||
if nc.isClosed() { | ||
nc.mu.Unlock() | ||
return | ||
} | ||
|
||
// Grab mch | ||
mch := nc.respMap[rt] | ||
// Delete the key regardless, one response only. | ||
// FIXME(dlc) - should we track responses past 1 | ||
// just statistics wise? | ||
delete(nc.respMap, rt) | ||
|
||
// Just return if closed, kick out Request by | ||
// closing channel. | ||
if nc.isClosed() { | ||
nc.mu.Unlock() | ||
close(mch) | ||
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. Is close of a nil channel valid? (remember, mch could be nil here...) 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. Believe so.. 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. Actually no, but removed it anyway. 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. No, it will panic, so this needs to be fixed. 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. @derekcollison Seems that you missed above comment: closing a nil channel will panic 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. Reverted back to just returning. |
||
return | ||
} | ||
nc.mu.Unlock() | ||
|
||
// Don't block, let Request timeout instead, mch is | ||
|
@@ -2068,7 +2070,7 @@ func (nc *Conn) Request(subj string, data []byte, timeout time.Duration) (*Msg, | |
return nil, ErrConnectionClosed | ||
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. Would like to see a test where we spawn a go routine that will close the connection after a delay, and issue a request in while there is no responder running, and make sure that Request() returns ErrConnectionClosed. 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. Agree, but hard to make timing work correctly I think. I did consider it. 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. What about something like this:
The test for 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. That may work.. 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. I quickly tried that and looks like the Request is not kicked out... 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. Currently this always gives us a timeout error, by design. 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. So we would never get this ErrConnectionClosed for a closed channel, since this channel is never closed? 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. Fixed. 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. Actually, if you look at Close(), we do go through the list of subscriptions and close the mch or signal in case of async. So NextMsg() (in old Request style) would get kicked out and will get ErrConnectionClosed. You are changing the behavior in that the Request will now return ErrConnectionClosed but only after waiting for the whole timeout. 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. Agree, fixed. |
||
} | ||
case <-t.C: | ||
if nc.isClosed() { | ||
if nc.IsClosed() { | ||
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. I would not put that here. It is trued that there is possibly a race that the connection is closed while the request just timed-out, but we don't do that in NextMsg, etc.. 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. ok fixed. |
||
return nil, ErrConnectionClosed | ||
} | ||
return nil, ErrTimeout | ||
|
@@ -2746,6 +2748,20 @@ func (nc *Conn) clearPendingFlushCalls() { | |
nc.pongs = nil | ||
} | ||
|
||
// This will clear any pending Request calls. | ||
// Lock is assumed to be held by the caller. | ||
func (nc *Conn) clearPendingRequestCalls() { | ||
if nc.respMap == nil { | ||
return | ||
} | ||
for _, ch := range nc.respMap { | ||
if ch != nil { | ||
close(ch) | ||
} | ||
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. You should empty the map. In a test, you got this:
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. That's because the channel was closed, but left in the map, then respHandler tried to close again. 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. Fixed. |
||
} | ||
nc.pongs = nil | ||
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. did you meant nc.respMap here? |
||
} | ||
|
||
// Low level close call that will do correct cleanup and set | ||
// desired status. Also controls whether user defined callbacks | ||
// will be triggered. The lock should not be held entering this | ||
|
@@ -2768,6 +2784,9 @@ func (nc *Conn) close(status Status, doCBs bool) { | |
// Clear any queued pongs, e.g. pending flush calls. | ||
nc.clearPendingFlushCalls() | ||
|
||
// Clear any queued and blocking Requests. | ||
nc.clearPendingRequestCalls() | ||
|
||
if nc.ptmr != nil { | ||
nc.ptmr.Stop() | ||
} | ||
|
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.
Should we check for
nil
here? We remove the channel from the map next line, so if the responder was to send 2 responses, the second response would get a nil channel.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.
Sending to a nil channel will block forever, so select below will do the right thing.