-
Notifications
You must be signed in to change notification settings - Fork 139
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
Add Contexts where appropriate #124
Comments
An example from the Go codebase on how to use a context in a function that makes an external call: |
would be great to have this! |
@mrkagelui this project is open-source. We would welcome a contribution that adds this feature. @mgdotson also suggested he would be willing to work on it. |
The Consume method doesn't take context for cancellation. func (ch *Channel) Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args Table) (<-chan Delivery, error) {
...
} I know the Channel has a Cancel method, and a developer can implement any cancel process. However, Go's context is a standard way to cancel a process, so passing context to Consume method can cancel delivering without implementing its own cancel process is useful. What do you think? func (ch *Channel) ConsumeWithContext(ctx context.Context, queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args Table) (<-chan Delivery, error) {
...
} |
I implemented #192 as a basis for discussion. |
I'm not sure I quite follow the implementation (and I haven't followed the calling code all the way through to see if it is a blocking operation waiting for server confirmation). Wouldn't the If example wrapper: finished := make(chan struct{})
go func() {
msgs, err = Channel.Consume(
queue,
consumer,
autoAck,
exclusive,
noLocal,
noWait,
args,
)
close(finished)
}()
select {
case <-finished:
return msgs, err
case <-ctx.Done():
// cleanup
go func() {
<-finished
_ = msg.Cancel(consumer, true)
}()
return nil, fmt.Errorf("consume timed out")
} In the case of If I've misunderstood Consume and it's not a potentially blocking call with |
First of all, I also think In your example, |
I'm not sure about cancelling Line 305 in 1a875e1
Lines 365 to 373 in 1a875e1
Even if If we want to cancel |
@Zerpet Do you mean moving select {
case <-ctx.Done():
return
default:
}
ch.call(...) A default check before the call is only slightly better but still leaves the caller in the state of waiting for the call to return and attempting to clean up state from outside if the context is valid prior to "call" but times out during the "call". If moving context in #192 above "call", could there be a race with "ch.[Cc]ancel(consumer, false)" and a message pending that needs a nack? Feels like the entire state engine needs to be reworked with contexts in mind which is a pretty big lift. |
I meant a default context check as in your snippet.
The more I think about this, the more I'm inclined to keep the state engine as-is, and implement a timeout mechanism that closes the AMQP channel. Anything else simply leaves the library in an unknown state. For example, All in all, I think that closing the AMQP channel is the only reliable solution. It is intrusive, but it leaves the system in a known state, and it is straightforward for the caller what happened and what's the state. |
The context was not honoured in any of the *WithContext functions. This is confusing, and arguably broken. However, we cannot immediately fix the context-support situation due to #124 (comment) This commit undeprecates the non-context variants of publish, and documents that both variants are equivalent. The example now favours the non-context variants. Related to #195 Signed-off-by: Aitor Perez Cedres <[email protected]>
The context was not honoured in any of the *WithContext functions. This is confusing, and arguably broken. However, we cannot immediately fix the context-support situation due to #124 (comment) This commit undeprecates the non-context variants of publish, and documents that both variants are equivalent. The example now favours the non-context variants. Related to #195 Signed-off-by: Aitor Perez Cedres <[email protected]>
The context was not honoured in any of the *WithContext functions. This is confusing, and arguably broken. However, we cannot immediately fix the context-support situation due to #124 (comment) This commit undeprecates the non-context variants of publish, and documents that both variants are equivalent. The example now favours the non-context variants. Related to #195 Signed-off-by: Aitor Perez Cedres <[email protected]>
The context was not honoured in any of the *WithContext functions. This is confusing, and arguably broken. However, we cannot immediately fix the context-support situation due to #124 (comment) This commit undeprecates the non-context variants of publish, and documents that both variants are equivalent. The example now favours the non-context variants. Related to #195 Signed-off-by: Aitor Perez Cedres <[email protected]>
https://www.digitalocean.com/community/tutorials/how-to-use-contexts-in-go
#121 (reply in thread)
See the following:
cc @mgdotson
The text was updated successfully, but these errors were encountered: