You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apologies if this has already been answered, I tried looking around the issues for a bit but wasn't able to find an answer as to 'why ctx and evalCtx are different from the API POV?'
The function signature is pretty explicit about 'don't get confused'
, but I was wondering if the evalCtx just be stuffed inside of the regular context using context.WithValue and then extracted accordingly? Consider the following hypothetical web api psuedo code which authenticates the user and adds the authenticated user's information into the context (excuse the technical inaccuracies as I am not a gin user, but I see it used in the documentation)
package main
import (
"github.com/gin-gonic/gin""net/http"
)
constdefaultMessage="Hello!"funcAuthenticationMiddleware() gin.HandlerFunc {
// authenticate the userreturnfunc(c*gin.Context) {
// authenticate the userauthInfo:=GetAuthInfo()
// set the current user's information in the contextc.Ctx=context.WithValue(userKey{}, authInfo)
c.Next()
}
}
funcmain() {
// Initialize Go Ginengine:=gin.Default()
engine.Use(AuthenticationMiddleware())
// Setup a simple endpointengine.GET("/hello", func(c*gin.Context) {
// we can now tell who is using this endpointauthInfo:=context.Value(userKey{})
c.JSON(http.StatusOK, defaultMessage)
})
engine.GET("/hello2", func(c*gin.Context) {
// we can now tell who is using this endpointauthInfo:=context.Value(userKey{})
c.JSON(http.StatusOK, defaultMessage)
})
engine.Run()
}
For every route that wants to take advantage of the evalCtx they then have to extra the information in their individual routes like so
where ConstructEvalContext is some magical function that pulls the authenticate information and creates the appropriate evalCtx.
The API I was hoping to use was create another middleware which does this on behalf each of the routes
package main
import (
"github.com/gin-gonic/gin""net/http"
)
constdefaultMessage="Hello!"funcAuthenticationMiddleware() gin.HandlerFunc {
// authenticate the userreturnfunc(c*gin.Context) {
c.Ctx=context.WithValue(userKey{}, authenticationInformation)
c.Next()
}
}
funcEvaluationContextMiddleware() gin.HandlerFunc {
returnfunc(c*gin.Context) {
// grab the authentication information from the userauthInfo=context.Value(userKey{})
// set the context that is passed to the handlersc.Ctx=context.WithValue(evaluationCtxKey{}, ConstructEvalContext(authInfo))
// or even betterc.Ctx=openFeature.NewEvaluationContext(c.Ctx(), ConstructEvalContext(authInfo))
c.Next()
}
}
funcmain() {
// Initialize Go Ginengine:=gin.Default()
engine.Use(AuthenticationMiddleware())
engine.Use(EvaluationContextMiddleware())
// Setup a simple endpointengine.GET("/hello", func(c*gin.Context) {
client.Boolean(c.Ctx(), "flag1", false)
c.JSON(http.StatusOK, defaultMessage)
})
engine.GET("/hello2", func(c*gin.Context) {
client.Boolean(c.Ctx(), "flag1", false)
c.JSON(http.StatusOK, defaultMessage)
})
engine.Run()
}
; however, these two concepts being separate make the existing API have to be handled individually in every place we want to reference the evaluation context. This would also allow this to be passed transparently through to internal libraries that want to take advantage of evaluation contexts. I can write a helper function to do this for my application, but this seems like a common pattern APIs will want to use.
The concerns I see with this approach are:
what if multiple people set this key in the context - but the existing openFeature.NewEvaluationContext api can hide these concerns from the user.
why can't we use the existing openfeature.SetEvaluationContext or client.SetEvaluationContext - this value would change per request so for openfeature it wouldn't be thread safe. For client, I would have to create a new client for every request and pass it through the context making it the same behavior that is shown above
The text was updated successfully, but these errors were encountered:
Requirements
Apologies if this has already been answered, I tried looking around the issues for a bit but wasn't able to find an answer as to 'why ctx and evalCtx are different from the API POV?'
The function signature is pretty explicit about 'don't get confused'
, but I was wondering if the evalCtx just be stuffed inside of the regular context using context.WithValue and then extracted accordingly? Consider the following hypothetical web api psuedo code which authenticates the user and adds the authenticated user's information into the context (excuse the technical inaccuracies as I am not a gin user, but I see it used in the documentation)
For every route that wants to take advantage of the evalCtx they then have to extra the information in their individual routes like so
where
ConstructEvalContext
is some magical function that pulls the authenticate information and creates the appropriate evalCtx.The API I was hoping to use was create another middleware which does this on behalf each of the routes
; however, these two concepts being separate make the existing API have to be handled individually in every place we want to reference the evaluation context. This would also allow this to be passed transparently through to internal libraries that want to take advantage of evaluation contexts. I can write a helper function to do this for my application, but this seems like a common pattern APIs will want to use.
The concerns I see with this approach are:
openfeature.SetEvaluationContext
orclient.SetEvaluationContext
- this value would change per request so foropenfeature
it wouldn't be thread safe. Forclient
, I would have to create a new client for every request and pass it through the context making it the same behavior that is shown aboveThe text was updated successfully, but these errors were encountered: