-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.go
67 lines (57 loc) · 1.71 KB
/
options.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
package nrpc
import (
"google.golang.org/grpc"
"google.golang.org/grpc/stats"
)
// Option defines an option for configuring the server.
type Option func(opt *options)
func getOptions(opts []Option) options {
opt := options{
logger: noopLogger{},
statsHandler: noopStatsHandler{},
}
for _, o := range opts {
o(&opt)
}
return opt
}
type options struct {
logger Logger
unaryInt grpc.UnaryServerInterceptor
streamInt grpc.StreamServerInterceptor
statsHandler stats.Handler
}
// WithLogger sets the logger for the client or server.
func WithLogger(log Logger) Option {
return func(opt *options) {
opt.logger = log
}
}
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the
// server. Only one unary interceptor can be installed. The construction of multiple
// interceptors (e.g., chaining) can be implemented at the caller.
func UnaryInterceptor(i grpc.UnaryServerInterceptor) Option {
return func(opt *options) {
if opt.unaryInt != nil {
panic("nrpc: The unary server interceptor was already set and may not be reset.")
}
opt.unaryInt = i
}
}
// StreamInterceptor returns a ServerOption that sets the StreamServerInterceptor for the
// server. Only one stream interceptor can be installed.
func StreamInterceptor(i grpc.StreamServerInterceptor) Option {
return func(opt *options) {
if opt.streamInt != nil {
panic("nrpc: The stream server interceptor was already set and may not be reset.")
}
opt.streamInt = i
}
}
// StatsHandler returns a ServerOption that sets the StatsHandler for the server.
// It can be used to add tracing, metrics, etc.
func StatsHandler(handler stats.Handler) Option {
return func(opt *options) {
opt.statsHandler = handler
}
}