Comments (5)
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.
Thanks @savsgio I understand your point. Let me try to add another angle if I may.
Two points I think needs to be considered.
- It's easy to miss the "use
ctx.AttachedContext()
instead of ctx" instruction. There's no compiler help and by convention everyone using acontext.Context
are already wrapping one context inside another so that would be their first instinct, to simply usectx
to attach a new value. So in terms of API ergonomics this could be a potential pitfall. - 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.
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.
ok, I get it, it's a limitation or "by design" of fasthttp.
from atreugo.
Sorry, use attached context carefully.
I could't say you anything more!
from atreugo.
Related Issues (20)
- Possible breaking change in patch version? "path must begin with '/' in path ''" HOT 2
- AttachContext overrides previous AttachContext HOT 2
- Hi, Savsgio, how to add CORS middleware? HOT 30
- Is it possible to run "after" middleware even when "before" middleware exits error? HOT 5
- Unable to match URL with "/" ending HOT 1
- Unsupported path parameters with root / prefix HOT 1
- Problem with trailing "/" when optional url param is empty (11 version) - 308 redirect HOT 3
- I can not install HOT 1
- Error! installing with go get github.com/savsgio/atreugo HOT 5
- Cannot update/download deps HOT 1
- any sort of html template kind of real life example code to go with? router with html hello world at least HOT 1
- Instrumenting servers / paths to add metrics doesn't work when view returns an error HOT 1
- Use atreugo and autocert HOT 2
- Support custom logger HOT 4
- Possible race condition in context propagation HOT 3
- rate limit middleware request HOT 3
- Race condition when calling RequestCtx.Value() HOT 4
- Sentry APM Middleware HOT 1
- Occasional panic HOT 1
- Configurable JSON Marshaller HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from atreugo.