-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.go
151 lines (132 loc) · 3.32 KB
/
httpserver.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
package main
import (
"context"
"errors"
"fmt"
"sort"
"sync"
"time"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
)
type EdsTarget struct {
Address string `json:"address"`
Port uint32 `json:"port"`
LastPingTimestamp time.Time
}
type EdsTargets []EdsTarget
func (e EdsTargets) Len() int {
return len(e)
}
func (e EdsTargets) Less(i, j int) bool {
return fmt.Sprintf("%s:%d", e[i].Address, e[i].Port) < fmt.Sprintf("%s:%d", e[j].Address, e[j].Port)
}
func (e EdsTargets) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
type KeyedEdsTarget struct {
Key string
Address string `json:"address"`
Port uint32 `json:"port"`
}
type HttpServer struct {
AllTargets map[string]EdsTarget
mutex *sync.Mutex
DataCache *cache.SnapshotCache
Eds *EdsResource
EvictionTimeout int
}
func NewHttpServer() *HttpServer {
result := HttpServer{}
result.Initialize()
return &result
}
func (s *HttpServer) Initialize() {
s.AllTargets = make(map[string]EdsTarget)
s.mutex = &sync.Mutex{}
}
func (s *HttpServer) List() EdsTargets {
rsp := make([]EdsTarget, len(s.AllTargets))
i := 0
for _, val := range s.AllTargets {
rsp[i] = val
i++
}
var r EdsTargets = rsp
sort.Sort(r)
return r
}
func (s *HttpServer) Get(key string) (*EdsTarget, bool) {
if t, ok := s.AllTargets[key]; ok {
return &t, true
}
return nil, false
}
func toEdsTarget(src KeyedEdsTarget) EdsTarget {
return EdsTarget{
Address: src.Address,
Port: src.Port,
LastPingTimestamp: time.Now(),
}
}
func (s *HttpServer) Post(key string, target KeyedEdsTarget) error {
if len(key) == 0 {
return errors.New("key cannot be empty")
}
s.mutex.Lock()
defer s.mutex.Unlock()
s.AllTargets[key] = toEdsTarget(target)
snapshot := s.Eds.GenerateSnapshot()
return (*s.DataCache).SetSnapshot(context.Background(), s.Eds.NodeId, snapshot)
}
func (s *HttpServer) Delete(key string) error {
if len(key) == 0 {
return errors.New("key cannot be empty")
}
s.mutex.Lock()
defer s.mutex.Unlock()
if _, ok := s.AllTargets[key]; ok {
delete(s.AllTargets, key)
snapshot := s.Eds.GenerateSnapshot()
return (*s.DataCache).SetSnapshot(context.Background(), s.Eds.NodeId, snapshot)
}
return errors.New("the key does not exist")
}
func (s *HttpServer) EvictHeartbeatTimeout() {
s.mutex.Lock()
defer s.mutex.Unlock()
refTime := time.Now()
for key, value := range s.AllTargets {
elapsed := refTime.Sub(value.LastPingTimestamp)
if elapsed.Seconds() > float64(s.EvictionTimeout) {
delete(s.AllTargets, key)
}
}
}
func (s *HttpServer) GetEndpoints() []*endpoint.LbEndpoint {
result := make([]*endpoint.LbEndpoint, len(s.AllTargets))
i := 0
for _, val := range s.AllTargets {
ep := endpoint.LbEndpoint{
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.SocketAddress_TCP,
Address: val.Address,
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: val.Port,
},
},
},
},
},
},
}
result[i] = &ep
i++
}
return result
}