-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (145 loc) · 3.78 KB
/
main.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
package cfg
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/beevik/guid"
)
type Settings struct {
Host string
ProjectName string
IntervalTimeInSecond time.Duration
FirstTimeLoadRetryCount int
}
type configuration struct {
Key string `json:"key"`
Value string `json:"value"`
}
type ConfigurationDto struct {
key string
value string
}
func (c ConfigurationDto) String() string {
return c.value
}
func (c ConfigurationDto) Int() int {
n, err := strconv.Atoi(c.value)
if err != nil {
panic(err)
}
return n
}
func (c ConfigurationDto) Bool() bool {
n, err := strconv.ParseBool(c.value)
if err != nil {
panic(err)
}
return n
}
func GetEnvironmentVariable(v string) ConfigurationDto {
return ConfigurationDto{
key: v,
value: os.Getenv(v),
}
}
type Environments struct {
FilePath string
FileName string
}
var client = &http.Client{
Timeout: time.Second * 30,
}
var configurationDtoList = make(map[string]ConfigurationDto)
var interval = make(chan bool, 1)
var Complete = make(chan bool, 1)
var defaultRequestDelayInSecond = time.Second * 5
func Get(configurationKey string) ConfigurationDto {
return configurationDtoList[configurationKey]
}
func (s Settings) getConfigurationsFromService() ([]configuration, error) {
req, err := http.NewRequest("GET", s.Host+"/configurations?projectName="+s.ProjectName, nil)
if err != nil {
log.Println("an error occurred while getting configurations.", err)
}
req.Header.Add("x-correlationid", guid.New().String())
req.Header.Add("x-agentname", "cfg-go-client")
resp, err := client.Do(req)
fmt.Println("requ")
var configurationList []configuration
if resp != nil {
err = json.NewDecoder(resp.Body).Decode(&configurationList)
if err != nil {
log.Println("an error occurred while getting configurations...", err)
return []configuration{}, errors.New("an error occurred while decoding response")
}
defer resp.Body.Close()
return configurationList, nil
}
return configurationList, errors.New("an error occurred")
}
type loader interface {
loadConfigurationsFromService(settings Settings)
}
func (s Settings) loadConfigurationsFromService() {
f := true
init := false
counter := 0
for {
<-interval
configurationList, err := s.getConfigurationsFromService()
if err != nil && f {
log.Printf("configurations didn't updated counter:%v", counter)
if counter < s.FirstTimeLoadRetryCount && !init {
time.Sleep(defaultRequestDelayInSecond)
counter++
interval <- true
continue
}
if init {
time.Sleep(s.IntervalTimeInSecond * time.Second)
interval <- true
continue
}
panic("arrived max retry count before first load")
}
for _, config := range configurationList {
if cDto, ok := configurationDtoList[config.Key]; ok {
if cDto.value != config.Value {
if !f {
log.Printf("changed configuration value. configurationKey: %v -> configuration new & old value: %v --- %v, ", config.Key, config.Value, cDto.value )
}
cDto.value = config.Value
configurationDtoList[config.Key] = cDto
}
} else {
log.Printf("new configuration found. configurationKey: %v configuration value: %v", config.Key, config.Value)
configurationDtoList[config.Key] = ConfigurationDto{
key: config.Key,
value: config.Value,
}
}
}
if f {
Complete <- true
init = true
close(Complete)
f = false
if s.IntervalTimeInSecond < time.Second * 1 {
s.IntervalTimeInSecond = time.Second * 60
log.Printf("wrong intervalTimeInSecond value, will be set as: %s", s.IntervalTimeInSecond)
}
}
time.Sleep(s.IntervalTimeInSecond)
interval <- true
continue
}
}
func Init(settings Settings) {
interval <- true
go settings.loadConfigurationsFromService()
}