-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
164 lines (141 loc) · 3.38 KB
/
config.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package opt
import (
"errors"
"sync"
)
type configurableItem struct {
section string
conf Configurable
}
// Configurable represent an object/application that is configurable
type Configurable interface {
Configure(op *Options, first bool)
}
// Configurator stores application wide configuration
type Configurator struct {
mu sync.RWMutex
conn Connector
lastCfg *Options
items []configurableItem
}
// NewConfigurator open configuration with given driver and property
func NewConfigurator(driver string, prop *Options) (*Configurator, error) {
// load the driver
drv := DriverFor(driver)
if drv == nil {
return nil, errors.New("can not find configuration driver " + driver + ", forget to import?")
}
// create configurator
cfg := Configurator{}
conn, err := drv.Connect(cfg.sourceChanged, prop)
if err != nil {
return nil, err
}
cfg.lastCfg, err = conn.Load()
if err != nil {
conn.Close()
return nil, err
}
cfg.conn = conn
return &cfg, nil
}
func (cfg *Configurator) sourceChanged(f int) error {
// if new configuration is available, load it.
// if the configuration is changed, reconfigure all registered configurable
cfg.mu.Lock()
defer cfg.mu.Unlock()
newCfg, err := cfg.conn.Load()
if err != nil {
return err
}
if newCfg == nil {
return errors.New("loaded configuration return <nil>")
}
// if configuration for given section is changed,
// broadcast the change to `Configurable` item
changed := false
for _, item := range cfg.items {
newOpt := newCfg.Get(item.section)
oldOpt := cfg.lastCfg.Get(item.section)
if !newOpt.EqualTo(oldOpt) {
changed = true
item.conf.Configure(newOpt, false)
}
}
if changed {
cfg.lastCfg = newCfg
}
return nil
}
// Valid returns true if connector is set and configuration loaded
func (cfg *Configurator) Valid() bool {
return cfg.conn != nil && cfg.lastCfg != nil
}
// Get return configuration for given key
func (cfg *Configurator) Get(key string) *Options {
cfg.mu.RLock()
defer cfg.mu.RUnlock()
if cfg.lastCfg != nil {
return cfg.lastCfg.Get(key)
}
return New()
}
// Store save configuration
func (cfg *Configurator) Store() error {
cfg.mu.RLock()
defer cfg.mu.RUnlock()
if cfg.Valid() {
return cfg.conn.Store(cfg.lastCfg)
}
return nil
}
// Load configuration from underlying source and
// if configure set to true, configure all registered objects
func (cfg *Configurator) Load(configure bool) error {
cfg.mu.Lock()
defer cfg.mu.Unlock()
if cfg.conn != nil {
newCfg, err := cfg.conn.Load()
if err != nil {
return err
}
cfg.lastCfg = newCfg
if configure {
cfg.Configure()
}
}
return nil
}
// Configure all registered configurable
func (cfg *Configurator) Configure() {
if cfg.lastCfg != nil {
for _, item := range cfg.items {
item.conf.Configure(cfg.lastCfg.Get(item.section), false)
}
}
}
// Register configurable to be managed by this configurator
func (cfg *Configurator) Register(section string, c Configurable) {
cfg.mu.Lock()
defer cfg.mu.Unlock()
// duplicate check
for _, v := range cfg.items {
if v.conf == c {
return
}
}
// append new client then reconfigure
cfg.items = append(cfg.items, configurableItem{section: section, conf: c})
if cfg.lastCfg != nil {
c.Configure(cfg.lastCfg, true)
}
}
// Close configurator
func (cfg *Configurator) Close() error {
if cfg.conn != nil {
c := cfg.conn
cfg.conn = nil
return c.Close()
}
return nil
}