-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_opts.go
93 lines (73 loc) · 1.79 KB
/
rpc_opts.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
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
package brpc
import (
"io"
"google.golang.org/grpc"
)
type rpcOptions struct {
Attachment []byte
LogID *int64
TraceID *int64
SpanID *int64
ParentSpanID *int64
AttachmentWriter io.Writer
}
type rpcOption interface {
apply(rpcOpts *rpcOptions)
}
var _ rpcOption = &attachmentOption{}
type attachmentOption struct {
grpc.EmptyCallOption
Attachment []byte
}
func (a *attachmentOption) apply(rpcOpts *rpcOptions) {
rpcOpts.Attachment = a.Attachment
}
func WithAttachment(attachment []byte) grpc.CallOption {
return &attachmentOption{Attachment: attachment}
}
var _ rpcOption = &logIDOption{}
type logIDOption struct {
grpc.EmptyCallOption
LogID int64
}
func (l *logIDOption) apply(rpcOpts *rpcOptions) {
rpcOpts.LogID = &l.LogID
}
func WithLogID(logID int64) grpc.CallOption {
return &logIDOption{LogID: logID}
}
var _ rpcOption = &traceOption{}
type traceOption struct {
grpc.EmptyCallOption
TraceID int64
SpanID int64
ParentSpanID int64
}
func (t *traceOption) apply(rpcOpts *rpcOptions) {
rpcOpts.TraceID = &t.TraceID
rpcOpts.SpanID = &t.SpanID
rpcOpts.ParentSpanID = &t.ParentSpanID
}
func WithTrace(traceID, spanID, parentSpanID int64) grpc.CallOption {
return &traceOption{TraceID: traceID, SpanID: spanID, ParentSpanID: parentSpanID}
}
func newRPCOptions(opts ...grpc.CallOption) *rpcOptions {
rpcOpts := new(rpcOptions)
for _, opt := range opts {
rpcOpt, ok := opt.(rpcOption)
if ok {
rpcOpt.apply(rpcOpts)
}
}
return rpcOpts
}
type receiveAttachment struct {
grpc.EmptyCallOption
AttachmentWriter io.Writer
}
func (t *receiveAttachment) apply(rpcOpts *rpcOptions) {
rpcOpts.AttachmentWriter = t.AttachmentWriter
}
func ReceiveAttachmentTo(w io.Writer) grpc.CallOption {
return &receiveAttachment{AttachmentWriter: w}
}