-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
ctx.go
33 lines (25 loc) · 767 Bytes
/
ctx.go
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
package gue
import "context"
type ctxKey struct{}
var (
workerIdxKey = ctxKey{}
)
const (
// WorkerIdxUnknown is returned when worker index in the pool is not set for some reason.
WorkerIdxUnknown = -1
)
// setWorkerIdx sets the index of the worker in the pool to the worker context.
func setWorkerIdx(ctx context.Context, idx int) context.Context {
return context.WithValue(ctx, workerIdxKey, idx)
}
// GetWorkerIdx gets the index of the worker in the pool from the worker context.
// Returns WorkerIdxUnknown if the context is not set or the value is not found there.
func GetWorkerIdx(ctx context.Context) int {
if ctx == nil {
return WorkerIdxUnknown
}
if idx, ok := ctx.Value(workerIdxKey).(int); ok {
return idx
}
return WorkerIdxUnknown
}