|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package reqctx |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "io" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/modules/process" |
| 12 | +) |
| 13 | + |
| 14 | +type ContextDataProvider interface { |
| 15 | + GetData() ContextData |
| 16 | +} |
| 17 | + |
| 18 | +type ContextData map[string]any |
| 19 | + |
| 20 | +func (ds ContextData) GetData() ContextData { |
| 21 | + return ds |
| 22 | +} |
| 23 | + |
| 24 | +func (ds ContextData) MergeFrom(other ContextData) ContextData { |
| 25 | + for k, v := range other { |
| 26 | + ds[k] = v |
| 27 | + } |
| 28 | + return ds |
| 29 | +} |
| 30 | + |
| 31 | +// RequestDataStore is a short-lived context-related object that is used to store request-specific data. |
| 32 | +type RequestDataStore interface { |
| 33 | + GetData() ContextData |
| 34 | + SetContextValue(k, v any) |
| 35 | + GetContextValue(key any) any |
| 36 | + AddCleanUp(f func()) |
| 37 | + AddCloser(c io.Closer) |
| 38 | +} |
| 39 | + |
| 40 | +type requestDataStoreKeyType struct{} |
| 41 | + |
| 42 | +var RequestDataStoreKey requestDataStoreKeyType |
| 43 | + |
| 44 | +type requestDataStore struct { |
| 45 | + data ContextData |
| 46 | + |
| 47 | + mu sync.RWMutex |
| 48 | + values map[any]any |
| 49 | + |
| 50 | + cleanUpFuncs []func() |
| 51 | +} |
| 52 | + |
| 53 | +func (r *requestDataStore) GetContextValue(key any) any { |
| 54 | + if key == RequestDataStoreKey { |
| 55 | + return r |
| 56 | + } |
| 57 | + r.mu.RLock() |
| 58 | + if v, ok := r.values[key]; ok { |
| 59 | + r.mu.RUnlock() |
| 60 | + return v |
| 61 | + } |
| 62 | + r.mu.RUnlock() |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (r *requestDataStore) SetContextValue(k, v any) { |
| 67 | + r.mu.Lock() |
| 68 | + r.values[k] = v |
| 69 | + r.mu.Unlock() |
| 70 | +} |
| 71 | + |
| 72 | +// GetData and the underlying ContextData are not thread-safe, callers should ensure thread-safety. |
| 73 | +func (r *requestDataStore) GetData() ContextData { |
| 74 | + if r.data == nil { |
| 75 | + r.data = make(ContextData) |
| 76 | + } |
| 77 | + return r.data |
| 78 | +} |
| 79 | + |
| 80 | +func (r *requestDataStore) AddCleanUp(f func()) { |
| 81 | + r.mu.Lock() |
| 82 | + r.cleanUpFuncs = append(r.cleanUpFuncs, f) |
| 83 | + r.mu.Unlock() |
| 84 | +} |
| 85 | + |
| 86 | +func (r *requestDataStore) AddCloser(c io.Closer) { |
| 87 | + r.AddCleanUp(func() { _ = c.Close() }) |
| 88 | +} |
| 89 | + |
| 90 | +func (r *requestDataStore) cleanUp() { |
| 91 | + for _, f := range r.cleanUpFuncs { |
| 92 | + f() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func GetRequestDataStore(ctx context.Context) RequestDataStore { |
| 97 | + if req, ok := ctx.Value(RequestDataStoreKey).(*requestDataStore); ok { |
| 98 | + return req |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +type requestContext struct { |
| 104 | + context.Context |
| 105 | + dataStore *requestDataStore |
| 106 | +} |
| 107 | + |
| 108 | +func (c *requestContext) Value(key any) any { |
| 109 | + if key == RequestDataStoreKey { |
| 110 | + return c.dataStore |
| 111 | + } |
| 112 | + if v := c.dataStore.GetContextValue(key); v != nil { |
| 113 | + return v |
| 114 | + } |
| 115 | + return c.Context.Value(key) |
| 116 | +} |
| 117 | + |
| 118 | +func NewRequestContext(parentCtx context.Context, profDesc string) (_ context.Context, finished func()) { |
| 119 | + ctx, _, processFinished := process.GetManager().AddTypedContext(parentCtx, profDesc, process.RequestProcessType, true) |
| 120 | + reqCtx := &requestContext{Context: ctx, dataStore: &requestDataStore{values: make(map[any]any)}} |
| 121 | + return reqCtx, func() { |
| 122 | + reqCtx.dataStore.cleanUp() |
| 123 | + processFinished() |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// NewRequestContextForTest creates a new RequestContext for testing purposes |
| 128 | +// It doesn't add the context to the process manager, nor do cleanup |
| 129 | +func NewRequestContextForTest(parentCtx context.Context) context.Context { |
| 130 | + return &requestContext{Context: parentCtx, dataStore: &requestDataStore{values: make(map[any]any)}} |
| 131 | +} |
0 commit comments