-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
134 lines (126 loc) · 3.08 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
package main
import (
"encoding/json"
"errors"
"os"
"strconv"
"strings"
"sync"
"time"
)
type config struct {
configConnDial
AllowService map[string]struct{} // 允许哪些服务访问本服务
sync.RWMutex
}
var CONFIG *config
func init() {
cc := &config{
configConnDial: configConnDial{
KeepAlive: time.Second / 2,
KeepAliveTimeout: time.Second,
DialTimeOut: time.Second,
LeaseTimeOut: 1,
},
AllowService: map[string]struct{}{
"*": struct{}{},
"DEBUG": struct{}{},
},
}
dat, err := os.ReadFile("./sidecar.json")
if err == nil {
var c = map[string]string{}
err = json.Unmarshal(dat, &c)
if err == nil {
if field, ok := c["KeepAlive"]; ok {
i, err := strconv.Atoi(field)
if err == nil {
cc.KeepAlive = time.Millisecond * time.Duration(i)
}
}
if field, ok := c["KeepAliveTimeout"]; ok {
i, err := strconv.Atoi(field)
if err == nil {
cc.KeepAliveTimeout = time.Millisecond * time.Duration(i)
}
}
if field, ok := c["DialTimeOut"]; ok {
i, err := strconv.Atoi(field)
if err == nil {
cc.DialTimeOut = time.Millisecond * time.Duration(i)
}
}
if field, ok := c["LeaseTimeOut"]; ok {
i, err := strconv.Atoi(field)
if err == nil {
cc.LeaseTimeOut = int64(i)
}
}
if field, ok := c["AllowService"]; ok {
spans := strings.Split(field, ";")
cc.AllowService = map[string]struct{}{}
for _, span := range spans {
cc.AllowService[span] = struct{}{}
}
}
}
}
CONFIG = cc
}
func ReadConfig() string {
var as []string
for k, _ := range CONFIG.AllowService {
as = append(as, k)
}
var c = map[string]string{
"KeepAlive": strconv.Itoa(int(CONFIG.KeepAlive / time.Millisecond)),
"KeepAliveTimeout": strconv.Itoa(int(CONFIG.KeepAliveTimeout / time.Millisecond)),
"DialTimeOut": strconv.Itoa(int(CONFIG.DialTimeOut / time.Millisecond)),
"LeaseTimeOut": strconv.Itoa(int(CONFIG.LeaseTimeOut)),
"AllowService": strings.Join(as, ";"),
"node_id": strconv.Itoa(int(node_id)),
}
s, _ := json.Marshal(c)
return string(s)
}
func UpdateConfig(dat []byte) error {
CONFIG.Lock()
defer CONFIG.Unlock()
var c = map[string]string{}
err := json.Unmarshal(dat, &c)
if err != nil {
return err
}
if field, ok := c["KeepAlive"]; ok {
i, err := strconv.Atoi(field)
if err != nil {
return errors.New("bad KeepAlive")
} else {
CONFIG.KeepAlive = time.Millisecond * time.Duration(i)
}
}
if field, ok := c["KeepAliveTimeout"]; ok {
i, err := strconv.Atoi(field)
if err != nil {
return errors.New("bad KeepAliveTimeout")
} else {
CONFIG.KeepAliveTimeout = time.Millisecond * time.Duration(i)
}
}
if field, ok := c["DialTimeOut"]; ok {
i, err := strconv.Atoi(field)
if err != nil {
return errors.New("bad DialTimeOut")
} else {
CONFIG.DialTimeOut = time.Millisecond * time.Duration(i)
}
}
if field, ok := c["AllowService"]; ok {
spans := strings.Split(field, ";")
CONFIG.AllowService = map[string]struct{}{}
for _, span := range spans {
CONFIG.AllowService[span] = struct{}{}
}
}
return nil
}