-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
130 lines (109 loc) · 2.22 KB
/
options.go
File metadata and controls
130 lines (109 loc) · 2.22 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package service
import (
"context"
"time"
registrystore "github.com/rai-project/libkv/store"
"github.com/rai-project/registry"
"github.com/rai-project/tracer"
"github.com/rai-project/tracer/zipkin"
"google.golang.org/grpc"
)
type Options struct {
Name string
Description grpc.ServiceDesc
// Register loop interval
RegisterInterval time.Duration
// Tracer
Tracer tracer.Tracer
// Registry
Registry registrystore.Store
// Before and After funcs
BeforeStart []func() error
BeforeStop []func() error
AfterStart []func() error
AfterStop []func() error
Context context.Context
}
type Option func(*Options)
var (
DefaultName = "rai-project/service[default]"
)
func NewOptions(opts ...Option) *Options {
rgs, err := registry.New()
if err != nil {
rgs = nil
}
trcr, err := zipkin.New(DefaultName)
if err != nil {
trcr = nil
}
options := &Options{
Name: DefaultName,
Registry: rgs,
Tracer: trcr,
Context: context.Background(),
}
for _, o := range opts {
o(options)
}
return options
}
// overrides the default option being used
// Must be at the beginning of the options
func Using(e *Options) Option {
return func(o *Options) {
*o = *e
}
}
func Name(s string) Option {
return func(o *Options) {
o.Name = s
}
}
func ServiceDescription(s grpc.ServiceDesc) Option {
return func(o *Options) {
o.Description = s
o.Name = o.Description.ServiceName
}
}
func Registry(s registrystore.Store) Option {
return func(o *Options) {
if o.Registry != nil {
o.Registry.Close()
}
o.Registry = s
}
}
func Tracer(s tracer.Tracer) Option {
return func(o *Options) {
if o.Tracer != nil {
o.Tracer.Close()
}
o.Tracer = s
}
}
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
func BeforeStart(fn func() error) Option {
return func(o *Options) {
o.BeforeStart = append(o.BeforeStart, fn)
}
}
func BeforeStop(fn func() error) Option {
return func(o *Options) {
o.BeforeStop = append(o.BeforeStop, fn)
}
}
func AfterStart(fn func() error) Option {
return func(o *Options) {
o.AfterStart = append(o.AfterStart, fn)
}
}
func AfterStop(fn func() error) Option {
return func(o *Options) {
o.AfterStop = append(o.AfterStop, fn)
}
}