forked from shima-park/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
146 lines (120 loc) · 3.4 KB
/
options.go
File metadata and controls
146 lines (120 loc) · 3.4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package agollo
import (
"time"
)
type Options struct {
ConfigServerURL string // apollo 服务地址
AppID string // appid
Cluster string // 默认的集群名称,默认:default
DefaultNamespace string // 默认的命名空间,默认:application
PreloadNamespaces []string // 预加载命名空间,默认:application
ApolloClient ApolloClient // apollo HTTP api实现
Logger Logger // 需要日志需要设置实现,或者注入有效的io.Writer,默认: ioutil.Discard
AutoFetchOnCacheMiss bool // 自动获取非预设以外的namespace的配置,默认:false
LongPollerInterval time.Duration // 轮训间隔时间,默认:1s
BackupFile string // 备份文件存放地址,默认:.agollo
FailTolerantOnBackupExists bool // 服务器连接失败时允许读取备份,默认:false
}
func newOptions(opts ...Option) Options {
var options = Options{
AutoFetchOnCacheMiss: defaultAutoFetchOnCacheMiss,
FailTolerantOnBackupExists: defaultFailTolerantOnBackupExists,
}
for _, opt := range opts {
opt(&options)
}
if options.Cluster == "" {
options.Cluster = defaultCluster
}
if options.DefaultNamespace == "" {
options.DefaultNamespace = defaultNamespace
}
if len(options.PreloadNamespaces) == 0 {
options.PreloadNamespaces = []string{defaultNamespace}
} else {
if !stringInSlice(defaultNamespace, options.PreloadNamespaces) {
PreloadNamespaces(defaultNamespace)(&options)
}
}
if options.ApolloClient == nil {
options.ApolloClient = NewApolloClient()
}
if options.Logger == nil {
options.Logger = NewLogger()
}
if options.LongPollerInterval <= time.Duration(0) {
options.LongPollerInterval = defaultLongPollInterval
}
if options.BackupFile == "" {
options.BackupFile = defaultBackupFile
}
return options
}
type Option func(*Options)
func Cluster(cluster string) Option {
return func(o *Options) {
o.Cluster = cluster
}
}
func DefaultNamespace(defaultNamespace string) Option {
return func(o *Options) {
o.DefaultNamespace = defaultNamespace
}
}
func PreloadNamespaces(namespaces ...string) Option {
return func(o *Options) {
o.PreloadNamespaces = append(o.PreloadNamespaces, namespaces...)
}
}
func WithApolloClient(c ApolloClient) Option {
return func(o *Options) {
o.ApolloClient = c
}
}
func WithLogger(l Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
func AutoFetchOnCacheMiss() Option {
return func(o *Options) {
o.AutoFetchOnCacheMiss = true
}
}
func LongPollerInterval(i time.Duration) Option {
return func(o *Options) {
o.LongPollerInterval = i
}
}
func BackupFile(backupFile string) Option {
return func(o *Options) {
o.BackupFile = backupFile
}
}
func FailTolerantOnBackupExists() Option {
return func(o *Options) {
o.FailTolerantOnBackupExists = true
}
}
type GetOptions struct {
DefaultValue string
Namespace string
}
func newGetOptions(opts ...GetOption) GetOptions {
var getOpts GetOptions
for _, opt := range opts {
opt(&getOpts)
}
return getOpts
}
type GetOption func(*GetOptions)
func WithDefault(defVal string) GetOption {
return func(o *GetOptions) {
o.DefaultValue = defVal
}
}
func WithNamespace(namespace string) GetOption {
return func(o *GetOptions) {
o.Namespace = namespace
}
}