forked from Yiming1997/agilePool
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
462 lines (411 loc) · 14.9 KB
/
Copy pathcontext.go
File metadata and controls
462 lines (411 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package agilepool
import (
"context"
"fmt"
"runtime"
"sync"
"time"
)
// ---------------------------------------------------------------------------
// Well-known label keys
// ---------------------------------------------------------------------------
const (
// LabelTraceID is the standard key for trace identifiers.
LabelTraceID = "trace_id"
// Internal keys used by TimingHook — prefixed with "_" to avoid
// collisions with user labels.
labelTiming = "_timing" // Empty{} if enabled
labelTimingSubmitted = "_timing_submitted" // time.Time
labelTimingEnqueued = "_timing_enqueued" // time.Time
labelTimingStarted = "_timing_started" // time.Time
labelTimingCompleted = "_timing_completed" // time.Time
)
// ---------------------------------------------------------------------------
// Empty — sentinel value
// ---------------------------------------------------------------------------
// Empty is a zero-allocation sentinel type. Store an Empty{} under a key
// to signal "enabled" without allocating a meaningful value.
//
// ctx.EnableTiming() // stores Empty{} under "_timing"
// _, ok := ctx.Get("_timing") // ok==true means timing is enabled
type Empty struct{}
// ---------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------
// Context wraps a standard context.Context with a thread-safe mutable
// key-value store (labels). It implements context.Context via embedding,
// and overrides Value() to search its own labels before delegating to the
// parent chain — the same design pattern used by Gin's gin.Context.
//
// Context is designed for sync.Pool reuse: obtain one via NewContext and
// call Release() after the task completes to return it to the pool.
type Context struct {
context.Context
mu sync.Mutex
labels map[string]any
}
// ---------------------------------------------------------------------------
// sync.Pool for Context reuse
// ---------------------------------------------------------------------------
var contextPool = sync.Pool{
New: func() any {
return &Context{labels: make(map[string]any, 8)}
},
}
// ---------------------------------------------------------------------------
// contextKey — unexported type to avoid collisions in context.WithValue.
// ---------------------------------------------------------------------------
type contextKey struct{ name string }
var poolContextKey = contextKey{name: "agilepool.Context"}
// ---------------------------------------------------------------------------
// Constructor & lifecycle
// ---------------------------------------------------------------------------
// NewContext obtains a Context from the internal sync.Pool and initialises
// it with parent. If parent is nil, context.Background() is used.
//
// IMPORTANT: NewContext injects *Context itself into the parent's Value
// chain via context.WithValue. This means PoolContextFrom can find it even
// if the caller later wraps the Context with context.WithValue,
// context.WithTimeout, or context.WithCancel — matching Gin's behaviour.
func NewContext(parent context.Context) *Context {
if parent == nil {
parent = context.Background()
}
c := contextPool.Get().(*Context)
// Inject self so PoolContextFrom survives wrapping.
c.Context = context.WithValue(parent, poolContextKey, c)
return c
}
// Release resets the Context and returns it to the internal pool for reuse.
// Call this once per task to avoid allocation churn.
func (c *Context) Release() {
c.Reset()
c.Context = nil // drop reference to parent chain
contextPool.Put(c)
}
// Reset clears all labels and metrics state. It is called automatically
// by Release(); you only need to call it directly if you are reusing a
// Context outside the pool.
func (c *Context) Reset() {
c.mu.Lock()
// Zero each entry so GC can reclaim values, then clear the map.
for k := range c.labels {
delete(c.labels, k)
}
c.mu.Unlock()
}
// ---------------------------------------------------------------------------
// Store / Get — generic key-value access (labels is map[string]any)
// ---------------------------------------------------------------------------
// Store sets a value under key. Thread-safe.
func (c *Context) Store(key string, value any) {
c.mu.Lock()
defer c.mu.Unlock()
c.labels[key] = value
}
// Get retrieves a value by key. The bool reports whether the key was present.
// Thread-safe.
func (c *Context) Get(key string) (any, bool) {
c.mu.Lock()
defer c.mu.Unlock()
v, ok := c.labels[key]
return v, ok
}
// GetString is a convenience helper that returns the string value for key,
// or "" if the key is missing or its value is not a string.
func (c *Context) GetString(key string) string {
c.mu.Lock()
defer c.mu.Unlock()
if v, ok := c.labels[key]; ok {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
// GetTime is a convenience helper that returns the time.Time value for key,
// or the zero time if the key is missing or its value is not a time.Time.
func (c *Context) GetTime(key string) time.Time {
c.mu.Lock()
defer c.mu.Unlock()
if v, ok := c.labels[key]; ok {
if t, ok := v.(time.Time); ok {
return t
}
}
return time.Time{}
}
// ---------------------------------------------------------------------------
// Value — override context.Context.Value for chain lookup (Gin-style)
// ---------------------------------------------------------------------------
// Value searches the Context's labels first (for string keys), then falls
// back to the embedded context.Context chain. This means:
//
// - ctx.Value("trace_id") → found in labels
// - ctx.Value(poolContextKey) → found in parent via WithValue injection
// - ctx.Value(someOtherKey) → delegated to embedded context chain
//
// This design ensures that even after wrapping with context.WithValue /
// context.WithTimeout, standard context.Value lookups can still reach the
// Context's labels.
func (c *Context) Value(key any) any {
// String keys: search our labels map first.
if k, ok := key.(string); ok {
c.mu.Lock()
if v, exists := c.labels[k]; exists {
c.mu.Unlock()
return v
}
c.mu.Unlock()
}
// Fall back to the embedded context chain (which includes the
// poolContextKey injection set up in NewContext).
return c.Context.Value(key)
}
// ---------------------------------------------------------------------------
// PoolContextFrom — extract *Context from any context.Context
// ---------------------------------------------------------------------------
// PoolContextFrom extracts a *Context from ctx.
// Returns nil if ctx is not a *Context and does not contain one in its
// Value chain.
//
// It first tries a type assertion (fast path), then falls back to
// ctx.Value(poolContextKey) (survives context.WithValue / WithTimeout
// wrapping thanks to the injection in NewContext).
func PoolContextFrom(ctx context.Context) *Context {
if c, ok := ctx.(*Context); ok {
return c
}
if c, ok := ctx.Value(poolContextKey).(*Context); ok {
return c
}
return nil
}
// ---------------------------------------------------------------------------
// TraceID — auto-generating trace identifier
// ---------------------------------------------------------------------------
// TraceID returns the trace_id label. If no trace_id has been set,
// a random hex-encoded 16-byte id is generated and stored automatically.
func (c *Context) TraceID() (string, error) {
// Fast path: already set.
c.mu.Lock()
if id, ok := c.labels[LabelTraceID].(string); ok && id != "" {
c.mu.Unlock()
return id, nil
}
c.mu.Unlock()
// Slow path: generate outside the lock (crypto/rand may block).
id, err := newTraceID()
if err != nil {
return "", err
}
// Double-check: another goroutine may have set it while we were generating.
c.mu.Lock()
if existing, ok := c.labels[LabelTraceID].(string); ok && existing != "" {
c.mu.Unlock()
return existing, nil
}
c.labels[LabelTraceID] = id
c.mu.Unlock()
return id, nil
}
// SetTraceID explicitly sets the trace_id label. Use this when you have an
// incoming trace id from an upstream system (e.g. X-Trace-Id header).
func (c *Context) SetTraceID(id string) {
c.Store(LabelTraceID, id)
}
// ---------------------------------------------------------------------------
// Timing support
// ---------------------------------------------------------------------------
// EnableTiming marks this Context for lifecycle timing. After calling this,
// any registered TimingHook will record timestamps at each lifecycle stage
// (submitted / enqueued / started / completed).
//
// Check whether timing is enabled:
//
// _, enabled := ctx.Get(labelTiming)
func (c *Context) EnableTiming() {
c.Store(labelTiming, Empty{})
}
// IsTimingEnabled reports whether EnableTiming() was called on this Context.
func (c *Context) IsTimingEnabled() bool {
_, ok := c.Get(labelTiming)
return ok
}
// Timing returns the four lifecycle timestamps recorded by TimingHook.
// A zero time.Time means that stage was not recorded (e.g. the hook was
// not registered, or timing was not enabled).
func (c *Context) Timing() (submitted, enqueued, started, completed time.Time) {
return c.GetTime(labelTimingSubmitted),
c.GetTime(labelTimingEnqueued),
c.GetTime(labelTimingStarted),
c.GetTime(labelTimingCompleted)
}
// Duration returns the elapsed time since the task started, or 0 if no
// start timestamp has been recorded. TimingHook must be registered and
// EnableTiming() must have been called.
func (c *Context) Duration() time.Duration {
start := c.GetTime(labelTimingStarted)
if start.IsZero() {
return 0
}
return time.Since(start)
}
// TotalLatency returns the full end-to-end latency (submitted → completed),
// or 0 if either timestamp is missing.
func (c *Context) TotalLatency() time.Duration {
submitted, _, _, completed := c.Timing()
if submitted.IsZero() || completed.IsZero() {
return 0
}
return completed.Sub(submitted)
}
// QueueLatency returns the time spent waiting in the queue (submitted → started),
// or 0 if either timestamp is missing.
func (c *Context) QueueLatency() time.Duration {
submitted, _, started, _ := c.Timing()
if submitted.IsZero() || started.IsZero() {
return 0
}
return started.Sub(submitted)
}
// ExecLatency returns the time spent executing (started → completed),
// or 0 if either timestamp is missing.
func (c *Context) ExecLatency() time.Duration {
_, _, started, completed := c.Timing()
if started.IsZero() || completed.IsZero() {
return 0
}
return completed.Sub(started)
}
// HandoffLatency returns the time between submission and enqueueing
// (submitted → enqueued), or 0 if either timestamp is missing.
// This measures how long the pool took to accept the task into its
// internal channel or buffer.
func (c *Context) HandoffLatency() time.Duration {
submitted, enqueued, _, _ := c.Timing()
if submitted.IsZero() || enqueued.IsZero() {
return 0
}
return enqueued.Sub(submitted)
}
// QueueWaitLatency returns the time spent waiting in the queue after
// enqueueing (enqueued → started), or 0 if either timestamp is missing.
// This is the pure queueing delay, excluding the initial handoff.
func (c *Context) QueueWaitLatency() time.Duration {
_, enqueued, started, _ := c.Timing()
if enqueued.IsZero() || started.IsZero() {
return 0
}
return started.Sub(enqueued)
}
// ---------------------------------------------------------------------------
// TimingHook — pre-built four-phase lifecycle timing hooks
// ---------------------------------------------------------------------------
// TimingHook returns a set of four hooks that record timestamps into the
// Context at each lifecycle stage. The Context must have EnableTiming()
// called on it before the hooks will record anything.
//
// Usage:
//
// s, e, st, co := agilepool.TimingHook()
// pool.OnTaskSubmitted(s)
// pool.OnTaskEnqueued(e)
// pool.OnTaskStarted(st)
// pool.OnTaskCompleted(co)
//
// After completion, query timestamps via:
//
// pc.Timing() // all four time.Time values
// pc.Duration() // time since started
// pc.TotalLatency() // submitted → completed
// pc.QueueLatency() // submitted → started
// pc.ExecLatency() // started → completed
func TimingHook() (
submitted TaskHook,
enqueued TaskHook,
started TaskHook,
completed TaskCompleteHook,
) {
submitted = func(ctx context.Context, task Task) {
pc := PoolContextFrom(ctx)
if pc == nil || !pc.IsTimingEnabled() {
return
}
pc.Store(labelTimingSubmitted, time.Now())
}
enqueued = func(ctx context.Context, task Task) {
pc := PoolContextFrom(ctx)
if pc == nil || !pc.IsTimingEnabled() {
return
}
pc.Store(labelTimingEnqueued, time.Now())
}
started = func(ctx context.Context, task Task) {
pc := PoolContextFrom(ctx)
if pc == nil || !pc.IsTimingEnabled() {
return
}
pc.Store(labelTimingStarted, time.Now())
}
completed = func(ctx context.Context, task Task, recovered any) {
pc := PoolContextFrom(ctx)
if pc == nil || !pc.IsTimingEnabled() {
return
}
pc.Store(labelTimingCompleted, time.Now())
}
return
}
// ---------------------------------------------------------------------------
// SlowTaskLogHook — detect and log slow tasks
// ---------------------------------------------------------------------------
// SlowTaskLogHook returns a TaskCompleteHook that logs a warning when a task's
// execution time exceeds maxDuration. TimingHook must also be registered and
// EnableTiming() must have been called on the Context, otherwise the hook is
// a no-op.
//
// The log message includes the TraceID, execution latency, threshold, and
// the caller location (file:line function) captured via runtime.Caller so
// operators can trace back to the hook dispatch site.
//
// Usage:
//
// // Register TimingHook first to capture lifecycle timestamps.
// s, e, st, co := agilepool.TimingHook()
// pool.OnTaskSubmitted(s)
// pool.OnTaskEnqueued(e)
// pool.OnTaskStarted(st)
// pool.OnTaskCompleted(co)
//
// // Then register SlowTaskLogHook as an additional completion hook.
// pool.OnTaskCompleted(agilepool.SlowTaskLogHook(5*time.Second, logger))
//
// // In the task, enable timing on the Context:
// pc := agilepool.PoolContextFrom(ctx)
// pc.EnableTiming()
func SlowTaskLogHook(maxDuration time.Duration, logger Logger) TaskCompleteHook {
return func(ctx context.Context, task Task, recovered any) {
pc := PoolContextFrom(ctx)
if pc == nil || !pc.IsTimingEnabled() {
return
}
d := pc.ExecLatency()
if d <= 0 || d <= maxDuration {
return
}
traceID, _ := pc.TraceID()
// Capture the caller location for diagnostics.
// skip=3 skips: this closure → dispatch wrapper → dispatchTaskCompleted,
// landing on the worker's runTask defer where the hook was triggered.
loc := "unknown"
if callerPC, file, line, ok := runtime.Caller(3); ok {
loc = fmt.Sprintf("%s:%d", file, line)
if fn := runtime.FuncForPC(callerPC); fn != nil {
loc += " " + fn.Name()
}
}
logger.Printf("[slow-task] trace_id=%s exec_latency=%v threshold=%v caller=%s",
traceID, d, maxDuration, loc)
}
}