@@ -88,6 +88,21 @@ func (r *requestDataStore) cleanUp() {
8888 }
8989}
9090
91+ type RequestContext interface {
92+ context.Context
93+ RequestDataStore
94+ }
95+
96+ func FromContext (ctx context.Context ) RequestContext {
97+ // here we must use the current ctx and the underlying store
98+ // the current ctx guarantees that the ctx deadline/cancellation/values are respected
99+ // the underlying store guarantees that the request-specific data is available
100+ if store := GetRequestDataStore (ctx ); store != nil {
101+ return & requestContext {Context : ctx , RequestDataStore : store }
102+ }
103+ return nil
104+ }
105+
91106func GetRequestDataStore (ctx context.Context ) RequestDataStore {
92107 if req , ok := ctx .Value (RequestDataStoreKey ).(* requestDataStore ); ok {
93108 return req
@@ -97,27 +112,28 @@ func GetRequestDataStore(ctx context.Context) RequestDataStore {
97112
98113type requestContext struct {
99114 context.Context
100- dataStore * requestDataStore
115+ RequestDataStore
101116}
102117
103118func (c * requestContext ) Value (key any ) any {
104- if v := c .dataStore . GetContextValue (key ); v != nil {
119+ if v := c .GetContextValue (key ); v != nil {
105120 return v
106121 }
107122 return c .Context .Value (key )
108123}
109124
110125func NewRequestContext (parentCtx context.Context , profDesc string ) (_ context.Context , finished func ()) {
111126 ctx , _ , processFinished := process .GetManager ().AddTypedContext (parentCtx , profDesc , process .RequestProcessType , true )
112- reqCtx := & requestContext {Context : ctx , dataStore : & requestDataStore {values : make (map [any ]any )}}
127+ store := & requestDataStore {values : make (map [any ]any )}
128+ reqCtx := & requestContext {Context : ctx , RequestDataStore : store }
113129 return reqCtx , func () {
114- reqCtx . dataStore .cleanUp ()
130+ store .cleanUp ()
115131 processFinished ()
116132 }
117133}
118134
119135// NewRequestContextForTest creates a new RequestContext for testing purposes
120136// It doesn't add the context to the process manager, nor do cleanup
121137func NewRequestContextForTest (parentCtx context.Context ) context.Context {
122- return & requestContext {Context : parentCtx , dataStore : & requestDataStore {values : make (map [any ]any )}}
138+ return & requestContext {Context : parentCtx , RequestDataStore : & requestDataStore {values : make (map [any ]any )}}
123139}
0 commit comments