GithubHelp home page GithubHelp logo

Comments (5)

savsgio avatar savsgio commented on June 20, 2024

Hi @rantav,

If you create a second context with value, you must use ctx.AttachedCtx() not the RequestCtx again, because fasthttp doesn't support overwrite the original RequestCtx. Atreugo only adds an extra layer to support attached context.

So must do:

// First time
ctx2 := context.WithValue(ctx, "k2", "v2")
ctx.AttachContext(ctx2)

// Second time or more
ctx3 :=  context.WithValue(ctx.AttachedContext(), "k3", "v3")
ctx.AttachContext(ctx3)

from atreugo.

rantav avatar rantav commented on June 20, 2024

Thanks @savsgio I understand your point. Let me try to add another angle if I may.
Two points I think needs to be considered.

  1. It's easy to miss the "use ctx.AttachedContext() instead of ctx" instruction. There's no compiler help and by convention everyone using a context.Context are already wrapping one context inside another so that would be their first instinct, to simply use ctx to attach a new value. So in terms of API ergonomics this could be a potential pitfall.
  2. What if I attach a value to ctx and I also attach another value to actx. Now I want to add a third value, preserving the last two? a) I can't do that and b) there's a surprise element. Example:
ctx := acquireRequestCtx(new(fasthttp.RequestCtx))
ctx.SetUserValue("k1", "v1") // ctx contains k1
ctx2 := context.WithValue(ctx, "k2", "v2")
ctx.AttachContext(ctx2) // ctx contains k1, k2

// I want ctx3 to wrap both k1 and k2, but I can't. 
// Option 1
ctx3 := context.WithValue(ctx, "k3", "v3") // ctx3 contains k3, k1 and k2
// but if we do that: surprise 
ctx.AttachContext(ctx3) // now ctx contains k1, k3 (not k2) 

// Option 2 (instead of 1) 
ctx3 := context.WithValue(ctx2, "k3", "v3") // ctx3 contains k3, k2
// And now, surprise
ctx.AttachContext(ctx3) // now ctx contains k1, k3 (not k2) 

from atreugo.

savsgio avatar savsgio commented on June 20, 2024

I'm sorry, but your example is good, the ctx has the 3 keys:

fmt.Println(ctx.Value("k1"), ctx.Value("k2"), ctx.Value("k3"))
# Print: v1 v2 v3

Keep in mind, that you could only attach one context, if you attach another, you override the previous, that's why to use the ctx.AttachedContext() to keep the previously.
About use always the ctx and not the ctx.AttachedContext(), fasthttp and atreugo only implement the interface of context.Context to use as a simple context but not as a context with values, because it's generate some garbage and reduce the performance significally.

Maybe in a future, @erikdubbelboer or @kirillDanshin will implement it, but now, it's not possible.

from atreugo.

rantav avatar rantav commented on June 20, 2024

ok, I get it, it's a limitation or "by design" of fasthttp.

from atreugo.

savsgio avatar savsgio commented on June 20, 2024

Sorry, use attached context carefully.
I could't say you anything more!

from atreugo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.