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

AttachContext overrides previous AttachContext #104

Open
ayeletdeRoos opened this issue Sep 19, 2023 · 2 comments
Open

AttachContext overrides previous AttachContext #104

ayeletdeRoos opened this issue Sep 19, 2023 · 2 comments
Labels
question Further information is requested

Comments

@ayeletdeRoos
Copy link

Hi,

I have a scenario where my REST api service calls 2 middlewares. both of them use the AttachContext before calling ctx.Next().

After a long debug I realized that the second AttachContext is overriding the first one since the AttachContext key is the same one generated here .

Is this intentional?

Thanks in advance

@savsgio
Copy link
Owner

savsgio commented Sep 20, 2023

Hi @ayeletdeRoos,

Yes, it is. The point of AttachContext is to save a second context in the RequestCtx, but only one! If you need to save more than one, you can create a new one from the first context, and reattach it to the request!

Something like this:

// NOTE: ctx => atreugo.RequestCtx

myCtx := ctx.AttachedContext()
myBothCtx := context.WithValue(myCtx, <key>, mySecondCtx)

ctx.AttachContext(myBothCtx)

When you attach the last context, all values are accesible via ctx.Value(...)

@ayeletdeRoos
Copy link
Author

Hi @savsgio ,

Right, I did a similar workaround - thanks!

I do think a check for this "edge" case can be helpful (if a key is overridden in the ctx)

Thanks again for your reply :)

@savsgio savsgio added the question Further information is requested label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants