-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
66 lines (51 loc) · 1.73 KB
/
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
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
package wkafka
import "context"
type ctxKey string
const (
// KeyRecord is the context key for *Record.
KeyRecord ctxKey = "kafka_record"
// KeyIsDLQProcess is the context key for is DLQ.
KeyIsDLQProcess ctxKey = "kafka_is_dlq"
// KeyIsDLQEnabled is the context key for is DLQ enabled.
KeyIsDLQEnabled ctxKey = "kafka_is_dlq_enabled"
)
// CtxIsDLQProcess usable in the callback function to understand processing a DLQ message.
// - If the context is nil, or the KeyIsDLQProcess is not set, false is returned.
func CtxIsDLQProcess(ctx context.Context) bool {
if ctx == nil {
return false
}
isDLQ, _ := ctx.Value(KeyIsDLQProcess).(bool)
return isDLQ
}
// CtxIsDLQEnabled usable in the callback function to understand DLQ is enabled.
// - If the context is nil, or the KeyIsDLQEnabled is not set, false is returned.
func CtxIsDLQEnabled(ctx context.Context) bool {
if ctx == nil {
return false
}
isDLQEnabled, _ := ctx.Value(KeyIsDLQEnabled).(bool)
return isDLQEnabled
}
// CtxRecord returns the *Record from the context in callback function.
// - If the context is nil, or the Record is not set, nil is returned.
// - This is only used in callback function.
// - Don't edit the returned *Record!
func CtxRecord(ctx context.Context) *Record {
if ctx == nil {
return nil
}
record, _ := ctx.Value(KeyRecord).(*Record)
return record
}
// CtxRecordBatch returns the []*Record from the context in callback function.
// - If the context is nil, or the Record is not set, nil is returned.
// - This is only used in batch callback function.
// - Don't edit the returned []*Record!
func CtxRecordBatch(ctx context.Context) []*Record {
if ctx == nil {
return nil
}
record, _ := ctx.Value(KeyRecord).([]*Record)
return record
}