-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_driver.go
113 lines (103 loc) · 2.12 KB
/
remote_driver.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package goconfig_center
import (
"fmt"
"github.com/spf13/viper"
"sync"
"time"
)
type remoteConfig struct {
ConfigDriver `mapstructure:",squash"`
Endpoint string `mapstructure:"endpoint"`
Path string `mapstructure:"path"`
Prefix string `mapstructure:"prefix"`
Type string `mapstructure:"type"`
}
type remoteDriver struct {
cfg *remoteConfig
viper *viper.Viper
close bool
onUpdate chan struct{}
lock *sync.Mutex
once *sync.Once
}
func (r *remoteDriver) Name() string {
return r.cfg.Driver
}
func (r *remoteDriver) GetViper() (*viper.Viper, error) {
return r.viper, nil
}
func (r *remoteDriver) OnUpdate() <-chan struct{} {
if r.close {
return nil
}
r.lock.Lock()
defer r.lock.Unlock()
if r.onUpdate == nil {
r.onUpdate = make(chan struct{})
go func() {
ticker := time.Tick(time.Second)
for !r.close {
select {
case <-ticker:
if r.viper == nil {
break
}
e := r.viper.WatchRemoteConfig()
if e != nil {
fmt.Printf("%s viper remote listen failure! err=%v\n", time.Now().Format("2006-01-02 15:04:05"), e)
continue
}
r.lock.Lock()
if r.onUpdate != nil {
r.onUpdate <- struct{}{}
}
r.lock.Unlock()
}
}
}()
}
return r.onUpdate
}
func (r *remoteDriver) Close() error {
r.lock.Lock()
defer r.lock.Unlock()
if r.close {
return nil
}
r.close = true
if r.onUpdate != nil {
close(r.onUpdate)
r.onUpdate = nil
}
return nil
}
func (r *remoteDriver) Prefix() string {
return r.cfg.Prefix
}
func remoteFactory(cfg *viper.Viper) (Driver, error) {
var c remoteConfig
if err := cfg.Unmarshal(&c); err != nil {
return nil, err
}
v := viper.New()
if err := v.AddRemoteProvider(c.Driver, c.Endpoint, c.Path); err != nil {
return nil, err
}
v.SetConfigType(c.Type)
err := v.ReadRemoteConfig()
if err != nil {
return nil, err
}
return &remoteDriver{
cfg: &c,
viper: v,
lock: &sync.Mutex{},
once: &sync.Once{},
}, nil
}
func init() {
Register("etcd", remoteFactory)
Register("etcd3", remoteFactory)
Register("consul", remoteFactory)
Register("firestore", remoteFactory)
}