forked from abh/geodns-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeomap.go
116 lines (86 loc) · 2.01 KB
/
geomap.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
package dnsconfig
import (
"fmt"
"sort"
"strings"
"sync"
)
type geoTargetList []*GeoTarget
type geoTargetMap map[string]geoTargetList
type GeoMap struct {
geomap geoTargetMap
mutex sync.Mutex
}
type GeoTarget struct {
target string
weight int
}
func (a geoTargetList) Less(i, j int) bool {
iLen := len(a[i].target)
jLen := len(a[j].target)
if iLen == jLen {
return a[i].target < a[j].target
}
return iLen < jLen
}
func (s geoTargetList) Len() int { return len(s) }
func (s geoTargetList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s geoTargetList) Sort() { sort.Sort(s) }
func NewGeoMap() GeoMap {
gm := GeoMap{}
gm.Clear()
return gm
}
func (gm *GeoMap) Clear() {
gm.mutex.Lock()
defer gm.mutex.Unlock()
gm.geomap = make(geoTargetMap)
}
func (gm *GeoMap) GetNodeGeos(node string) []*GeoTarget {
gm.mutex.Lock()
defer gm.mutex.Unlock()
if geo, ok := gm.geomap[node]; ok {
return geo
}
for k, geos := range gm.geomap {
if matchWildcard(k, node) {
return geos
}
}
if geo, ok := gm.geomap["default"]; ok {
return geo
}
return nil
}
func (gm *GeoMap) LoadFile(fileName string) error {
objmap := make(map[string]interface{})
return jsonLoader(fileName, objmap, func() error {
gm.mutex.Lock()
defer gm.mutex.Unlock()
geomap := geoTargetMap{}
for name, v := range objmap {
// log.Printf("%s: %#v\n", name, v)
if _, ok := geomap[name]; !ok {
geomap[name] = make([]*GeoTarget, 0)
}
for _, g := range v.([]interface{}) {
gSplit := strings.Split(g.(string), "=")
// log.Printf("gSplit: %#v\n", gSplit)
weight := 100
if len(gSplit) > 1 {
var err error
weight, err = toInt(gSplit[1])
if err != nil {
return fmt.Errorf("Bad weight '%s' for geo '%s'/'%s': %s\n", gSplit[1], name, gSplit[0], err)
}
}
geo := GeoTarget{target: gSplit[0], weight: weight}
geomap[name] = append(geomap[name], &geo)
}
geomap[name].Sort()
// log.Printf("%s: %#v", name, geomap)
}
gm.geomap = geomap
return nil
})
}