This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
72 lines (59 loc) · 1.5 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
68
69
70
71
72
package plugin
import (
"google.golang.org/grpc"
"time"
)
type Option interface {
apply(*Options)
}
type Options struct {
DialOpts []grpc.DialOption
CallOpts []grpc.CallOption
Heartbeat time.Duration
LogLevel LogLevel
OnPanic func(plugin *Plugin, execID uint64, funcName string, err error)
}
type option struct {
f func(*Options)
}
func (o *option) apply(do *Options) {
o.f(do)
}
func newOption(f func(options *Options)) *option { return &option{f: f} }
func defaultOpts() Options {
return Options{
DialOpts: make([]grpc.DialOption, 0),
CallOpts: make([]grpc.CallOption, 0),
Heartbeat: time.Second * 10,
LogLevel: LogLevelInfo,
OnPanic: func(plugin *Plugin, execID uint64, funcName string, err error) {},
}
}
//WithHeartbeat set heartbeat, default is 10s
func WithHeartbeat(dur time.Duration) Option {
return newOption(func(options *Options) {
options.Heartbeat = dur
})
}
//WithLogLevel set log level, default is Info
func WithLogLevel(level LogLevel) Option {
return newOption(func(options *Options) {
options.LogLevel = level
})
}
func WithDialOpts(opts ...grpc.DialOption) Option {
return newOption(func(options *Options) {
options.DialOpts = opts
})
}
func WithCallOpts(opts ...grpc.CallOption) Option {
return newOption(func(options *Options) {
options.CallOpts = opts
})
}
//WithOnPanic default is Log.Errorf
func WithOnPanic(f func(plugin *Plugin, execID uint64, funcName string, err error)) Option {
return newOption(func(options *Options) {
options.OnPanic = f
})
}