...
1 package entrypoint
2
3 import (
4 "sync"
5
6 "github.com/datawire/ambassador/v2/pkg/consulwatch"
7 )
8
9 type ConsulStore struct {
10 mutex sync.Mutex
11 endpoints map[ConsulKey]consulwatch.Endpoints
12 }
13
14 type ConsulKey struct {
15 datacenter string
16 service string
17 }
18
19 func NewConsulStore() *ConsulStore {
20 return &ConsulStore{endpoints: map[ConsulKey]consulwatch.Endpoints{}}
21 }
22
23 func (c *ConsulStore) ConsulEndpoint(datacenter, service, address string, port int, tags ...string) {
24 c.mutex.Lock()
25 defer c.mutex.Unlock()
26
27 key := ConsulKey{datacenter, service}
28 ep, ok := c.endpoints[key]
29 if !ok {
30 ep = consulwatch.Endpoints{
31 Id: datacenter,
32 Service: service,
33 }
34 }
35 ep.Endpoints = append(ep.Endpoints, consulwatch.Endpoint{
36 ID: datacenter,
37 Service: service,
38 Address: address,
39 Port: port,
40 Tags: tags,
41 })
42 c.endpoints[key] = ep
43 }
44
45 func (c *ConsulStore) Get(datacenter, service string) (consulwatch.Endpoints, bool) {
46 c.mutex.Lock()
47 defer c.mutex.Unlock()
48 ep, ok := c.endpoints[ConsulKey{datacenter, service}]
49 return ep, ok
50 }
51
View as plain text