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

Timeout middleware: context is not restored #467

Open
turip opened this issue Aug 14, 2024 · 0 comments · May be fixed by #517
Open

Timeout middleware: context is not restored #467

turip opened this issue Aug 14, 2024 · 0 comments · May be fixed by #517

Comments

@turip
Copy link

turip commented Aug 14, 2024

If you take a look at the Timeout middleware:

func Timeout(timeout time.Duration) func(message.HandlerFunc) message.HandlerFunc {
	return func(h message.HandlerFunc) message.HandlerFunc {
		return func(msg *message.Message) ([]*message.Message, error) {
			ctx, cancel := context.WithTimeout(msg.Context(), timeout)
			defer func() {
				cancel()
			}()

			msg.SetContext(ctx)
			return h(msg)
		}
	}
}

It is visible that the original context is never set to the original value if a timeout occurs. However the Retry middleware uses the message's context to ensure that the MaxElapsedTime is supported:

                 if r.MaxElapsedTime > 0 {
			var cancel func()
			ctx, cancel = context.WithTimeout(ctx, r.MaxElapsedTime)
			defer cancel()
		}

This means that if I have this chain Retry(Timeout(handler)), and a timeout occurs then the Retry will not be effective.

I would recommend to save the original context in Timeout and restore that in the defer function(). Or is this the expected behavior?

@zanvd zanvd linked a pull request Nov 21, 2024 that will close this issue
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 a pull request may close this issue.

1 participant