-
Notifications
You must be signed in to change notification settings - Fork 8
/
context.go
32 lines (25 loc) · 946 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
package amqprpc
import "context"
type ctxKey int
const (
queueNameKey ctxKey = iota
shutdownChanKey
)
// ContextWithQueueName adds the given queueName to the provided context.
func ContextWithQueueName(ctx context.Context, queueName string) context.Context {
return context.WithValue(ctx, queueNameKey, queueName)
}
// QueueNameFromContext returns the queue name for the current request.
func QueueNameFromContext(ctx context.Context) (string, bool) {
queueName, ok := ctx.Value(queueNameKey).(string)
return queueName, ok
}
// ContextWithShutdownChan adds a shutdown chan to the given context.
func ContextWithShutdownChan(ctx context.Context, ch chan struct{}) context.Context {
return context.WithValue(ctx, shutdownChanKey, ch)
}
// ShutdownChanFromContext returns the shutdown chan.
func ShutdownChanFromContext(ctx context.Context) (chan struct{}, bool) {
ch, ok := ctx.Value(shutdownChanKey).(chan struct{})
return ch, ok
}