-
Notifications
You must be signed in to change notification settings - Fork 0
/
microservice_cfg.go
55 lines (49 loc) · 1.25 KB
/
microservice_cfg.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
package restlib
import (
"errors"
"fmt"
"github.com/prakash-p-3121/restlib/model"
"github.com/prakash-p-3121/tomllib"
"io/ioutil"
"sync"
"sync/atomic"
"unsafe"
)
func CreateMsConnectionCfg(cfgPath string) (*sync.Map, error) {
data, err := ioutil.ReadFile(cfgPath)
if err != nil {
fmt.Println("Error reading file:", err)
return nil, err
}
var cfgMap map[string]interface{}
err = tomllib.Serialize(data, &cfgMap)
if err != nil {
return nil, err
}
var concurrentMap sync.Map
for msName, value := range cfgMap {
valueMap, ok := value.(map[string]interface{})
if !ok {
return nil, err
}
host := valueMap["host"].(string)
port := valueMap["port"].(int64)
cfg := &model.MsConnectionCfg{Host: host, Port: port}
cfgPtr := unsafe.Pointer(cfg)
newCfgPtr := atomic.SwapPointer(&cfgPtr, unsafe.Pointer(&model.MsConnectionCfg{
Host: cfg.Host,
Port: cfg.Port,
}))
concurrentMap.Store(msName, newCfgPtr)
}
return &concurrentMap, nil
}
func RetrieveMsConnectionCfg(cfgMap *sync.Map, msName string) (model.MsConnectionCfg, error) {
value, ok := cfgMap.Load(msName)
if !ok {
return model.MsConnectionCfg{}, errors.New("not-found")
}
valuePtr := value.(unsafe.Pointer)
cfgPtr := (*model.MsConnectionCfg)(valuePtr)
return *cfgPtr, nil
}