-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
34 lines (27 loc) · 905 Bytes
/
context.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
34
package xsqs
import (
"context"
"github.com/yklyahin/xsqs/logging"
)
type contextKey int
const (
workerCtxKey contextKey = iota
)
// WorkerCtx stores the context information for an SQS worker.
type WorkerCtx struct {
Name string // Name of the worker.
Logger logging.Log
}
// GetWorkerCtxFromContext retrieves the WorkerCtx from the provided context.
// It returns the retrieved WorkerCtx and a boolean indicating whether the WorkerCtx was found in the context.
func GetWorkerCtxFromContext(ctx context.Context) (WorkerCtx, bool) {
value := ctx.Value(workerCtxKey)
if value == nil {
return WorkerCtx{}, false
}
return value.(WorkerCtx), true
}
// ContextWithWorkerCtx returns a new context with WorkerCtx derived from the provided parent context.
func ContextWithWorkerCtx(ctx context.Context, worker WorkerCtx) context.Context {
return context.WithValue(ctx, workerCtxKey, worker)
}