-
Notifications
You must be signed in to change notification settings - Fork 13
/
config_reader.go
184 lines (158 loc) · 3.92 KB
/
config_reader.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"runtime"
"time"
"github.com/bitmark-inc/logger"
)
// ConfigReader - methods supported by the configuration system
type ConfigReader interface {
OptimalThreadCount() uint32
SetCalendar(JobCalendar)
FirstRefresh(string) error
Refresh() error
GetConfig() (*Configuration, error)
SetLog(*logger.L) error
SetWatcher(watcher FileWatcher)
Start()
FirstTimeRun()
SetProofer(Proofer)
}
const (
oneMinute = time.Duration(1) * time.Minute
minThreadCount = 1
ReaderLoggerPrefix = "config-reader"
)
var (
totalCPUCount = uint32(runtime.NumCPU())
)
// ConfigReaderData - result from reading configuration
type ConfigReaderData struct {
refreshByMinute time.Duration
log *logger.L
currentConfiguration *Configuration
initialized bool
threadCount uint32
calendar JobCalendar
proofer Proofer
watcher FileWatcher
}
func newConfigReader() ConfigReader {
return &ConfigReaderData{
log: nil,
currentConfiguration: nil,
threadCount: 1,
initialized: false,
refreshByMinute: oneMinute,
}
}
func (c *ConfigReaderData) SetWatcher(watcher FileWatcher) {
c.watcher = watcher
}
func (c *ConfigReaderData) SetCalendar(calendar JobCalendar) {
c.calendar = calendar
}
func (c *ConfigReaderData) SetProofer(proofer Proofer) {
c.proofer = proofer
}
func (c *ConfigReaderData) FirstTimeRun() {
err := c.Refresh()
if err != nil {
return
}
c.notify()
}
func (c *ConfigReaderData) Start() {
go func() {
for {
select {
case <-c.watcher.ChangeChannel():
fileName := c.watcher.FileName()
c.log.Info("receive file change event, wait for 1 minute to adapt")
time.Sleep(c.refreshByMinute)
err := c.Refresh()
if err != nil {
c.log.Errorf("failed to read configuration from %s error: %s",
fileName, err)
}
c.notify()
case <-c.watcher.RemoveChannel():
c.log.Warn("config file removed")
}
}
}()
}
func (c *ConfigReaderData) FirstRefresh(fileName string) error {
configuration, err := getConfiguration(fileName)
if err != nil {
return err
}
c.update(configuration)
return nil
}
func (c *ConfigReaderData) Refresh() error {
configuration, err := c.parse()
if err != nil {
return err
}
c.update(configuration)
return nil
}
func (c *ConfigReaderData) notify() {
c.calendar.Refresh(c.currentConfiguration.Calendar)
c.proofer.Refresh()
}
func (c *ConfigReaderData) parse() (*Configuration, error) {
configuration, err := getConfiguration(c.watcher.FilePath())
if err != nil {
return nil, err
}
return configuration, nil
}
func (c *ConfigReaderData) GetConfig() (*Configuration, error) {
if c.currentConfiguration == nil {
return nil, fmt.Errorf("configuration is empty")
}
return c.currentConfiguration, nil
}
func (c *ConfigReaderData) SetLog(log *logger.L) error {
if log == nil {
return fmt.Errorf("logger %v is nil", log)
}
c.log = log
c.initialized = true
return nil
}
func (c *ConfigReaderData) update(newConfiguration *Configuration) {
c.currentConfiguration = newConfiguration
c.threadCount = c.OptimalThreadCount()
if c.initialized {
c.log.Debugf("Updating configuration, target thread count %d, working: %t",
c.threadCount,
c.proofer.IsWorking(),
)
}
}
func (c *ConfigReaderData) updateCpuCount(count uint32) {
if count > 0 {
totalCPUCount = count
}
}
func (c *ConfigReaderData) OptimalThreadCount() uint32 {
if !c.initialized {
return uint32(minThreadCount)
}
percentage := float32(c.currentConfiguration.maxCPUUsage()) / 100
threadCount := uint32(float32(totalCPUCount) * percentage)
if threadCount <= minThreadCount {
return minThreadCount
}
if threadCount > totalCPUCount {
return totalCPUCount
}
return threadCount
}