forked from xgfone/gconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
146 lines (129 loc) · 3.42 KB
/
snapshot.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
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gconf
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"sync"
"time"
)
// LoadBackupFile loads configuration data from the backup file if exists,
// then watches the change of the options and write them into the file.
//
// So you can use it as the local cache.
func (c *Config) LoadBackupFile(filename string) error {
var data []byte
var ms map[string]interface{}
if _, err := os.Stat(filename); err != nil {
if !os.IsNotExist(err) {
return err
}
} else if data, err = ioutil.ReadFile(filename); err != nil {
return err
} else if err = json.Unmarshal(data, &ms); err != nil {
return err
}
c.snap.InitMap(ms)
c.LoadMap(ms, false)
go c.writeSnapshotIntoFile(filename)
return nil
}
func (c *Config) writeSnapshotIntoFile(filename string) {
var lastgen uint64
ticker := time.NewTicker(time.Minute)
for {
select {
case _, ok := <-c.exit:
if !ok {
return
}
case <-ticker.C:
gen, data, err := c.snap.getData()
if err != nil {
c.handleError(fmt.Errorf("[Config] snapshot marshal json: %s", err.Error()))
} else if gen == lastgen {
continue
}
if err = ioutil.WriteFile(filename, data, os.ModePerm); err != nil {
c.handleError(fmt.Errorf("[Config] snapshot write file[%s]: %s", filename, err.Error()))
} else {
lastgen = gen
debugf("[Config] Write snapshot into file '%s'", filename)
}
}
}
}
// Snapshot returns the snapshot of the whole configuration options
// excpet for the their default values.
//
// Notice: the key includes the group name and the option name, for instance,
//
// map[string]interface{} {
// "opt1": "value1",
// "opt2": "value2",
// "group1.opt3": "value3",
// "group1.group2.opt4": "value4",
// // ...
// }
func (c *Config) Snapshot() map[string]interface{} {
return c.snap.ToMap()
}
func newSnapshot(c *Config) *snapshot {
snap := &snapshot{conf: c, maps: make(map[string]interface{}, 64)}
c.Observe(snap.ChangeObserver)
return snap
}
type snapshot struct {
conf *Config
lock sync.RWMutex
maps map[string]interface{}
gen uint64
}
func (s *snapshot) InitMap(ms map[string]interface{}) {
s.lock.Lock()
for k, v := range ms {
s.maps[k] = v
}
s.lock.Unlock()
}
func (s *snapshot) ToMap() map[string]interface{} {
s.lock.RLock()
dst := make(map[string]interface{}, len(s.maps)*2)
for key, value := range s.maps {
dst[key] = value
}
s.lock.RUnlock()
return dst
}
func (s *snapshot) ChangeObserver(group, opt string, old, new interface{}) {
key := opt
if group != "" {
key = fmt.Sprintf("%s%s%s", group, s.conf.gsep, opt)
}
s.lock.Lock()
defer s.lock.Unlock()
if value, ok := s.maps[key]; !ok || !reflect.DeepEqual(value, new) {
s.gen++
s.maps[key] = new
}
}
func (s *snapshot) getData() (uint64, []byte, error) {
s.lock.RLock()
defer s.lock.RUnlock()
data, err := json.Marshal(s.maps)
return s.gen, data, err
}