-
Notifications
You must be signed in to change notification settings - Fork 686
/
fake_consul_store.go
50 lines (42 loc) · 1.03 KB
/
fake_consul_store.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
package entrypoint
import (
"sync"
"github.com/datawire/ambassador/pkg/consulwatch"
)
type ConsulStore struct {
mutex sync.Mutex
endpoints map[ConsulKey]consulwatch.Endpoints
}
type ConsulKey struct {
datacenter string
service string
}
func NewConsulStore() *ConsulStore {
return &ConsulStore{endpoints: map[ConsulKey]consulwatch.Endpoints{}}
}
func (c *ConsulStore) ConsulEndpoint(datacenter, service, address string, port int, tags ...string) {
c.mutex.Lock()
defer c.mutex.Unlock()
key := ConsulKey{datacenter, service}
ep, ok := c.endpoints[key]
if !ok {
ep = consulwatch.Endpoints{
Id: datacenter,
Service: service,
}
}
ep.Endpoints = append(ep.Endpoints, consulwatch.Endpoint{
ID: datacenter,
Service: service,
Address: address,
Port: port,
Tags: tags,
})
c.endpoints[key] = ep
}
func (c *ConsulStore) Get(datacenter, service string) (consulwatch.Endpoints, bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
ep, ok := c.endpoints[ConsulKey{datacenter, service}]
return ep, ok
}