-
Notifications
You must be signed in to change notification settings - Fork 5
/
registry.go
74 lines (60 loc) · 1.43 KB
/
registry.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
package main
import (
"fmt"
"net/http/httputil"
"net/url"
"sync/atomic"
)
type Backend struct {
proxy *httputil.ReverseProxy
containerID string
}
type ServiceRegistry struct {
BackendsStore atomic.Value
}
func (s *ServiceRegistry) Init() {
s.BackendsStore.Store([]Backend{})
}
func (s *ServiceRegistry) Add(containerID, addr string) {
URL, _ := url.Parse(addr)
s.BackendsStore.Swap(append(s.GetBackends(), Backend{
proxy: httputil.NewSingleHostReverseProxy(URL),
containerID: containerID,
}))
}
func (s *ServiceRegistry) GetByContainerID(containerID string) (Backend, bool) {
for _, b := range s.GetBackends() {
if b.containerID == containerID {
return b, true
}
}
return Backend{}, false
}
func (s *ServiceRegistry) GetByIndex(index int) Backend {
return s.GetBackends()[index]
}
func (s *ServiceRegistry) RemoveByContainerID(containerID string) {
var backends []Backend
for _, b := range s.GetBackends() {
if b.containerID == containerID {
continue
}
backends = append(backends, b)
}
s.BackendsStore.Store(backends)
}
func (s *ServiceRegistry) RemoveAll() {
s.BackendsStore.Store([]Backend{})
}
func (s *ServiceRegistry) Len() int {
return len(s.GetBackends())
}
func (s *ServiceRegistry) List() {
backends := s.GetBackends()
for i := range backends {
fmt.Println(backends[i].containerID)
}
}
func (s *ServiceRegistry) GetBackends() []Backend {
return s.BackendsStore.Load().([]Backend)
}