-
Notifications
You must be signed in to change notification settings - Fork 449
/
proxy_collection.go
165 lines (133 loc) · 3.51 KB
/
proxy_collection.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package toxiproxy
import (
"encoding/json"
"fmt"
"io"
"sync"
)
// ProxyCollection is a collection of proxies. It's the interface for anything
// to add and remove proxies from the toxiproxy instance. It's responsibility is
// to maintain the integrity of the proxy set, by guarding for things such as
// duplicate names.
type ProxyCollection struct {
sync.RWMutex
proxies map[string]*Proxy
}
func NewProxyCollection() *ProxyCollection {
return &ProxyCollection{
proxies: make(map[string]*Proxy),
}
}
func (collection *ProxyCollection) Add(proxy *Proxy, start bool) error {
collection.Lock()
defer collection.Unlock()
if _, exists := collection.proxies[proxy.Name]; exists {
return ErrProxyAlreadyExists
}
if start {
err := proxy.Start()
if err != nil {
return err
}
}
collection.proxies[proxy.Name] = proxy
return nil
}
func (collection *ProxyCollection) AddOrReplace(proxy *Proxy, start bool) error {
collection.Lock()
defer collection.Unlock()
if existing, exists := collection.proxies[proxy.Name]; exists {
if existing.Listen == proxy.Listen && existing.Upstream == proxy.Upstream {
return nil
}
existing.Stop()
}
if start {
err := proxy.Start()
if err != nil {
return err
}
}
collection.proxies[proxy.Name] = proxy
return nil
}
func (collection *ProxyCollection) PopulateJson(
server *ApiServer,
data io.Reader,
) ([]*Proxy, error) {
input := []struct {
Proxy
Enabled *bool `json:"enabled"` // Overrides Proxy field to make field nullable
}{}
err := json.NewDecoder(data).Decode(&input)
if err != nil {
return nil, joinError(err, ErrBadRequestBody)
}
// Check for valid input before creating any proxies
t := true
for i := range input {
if len(input[i].Name) < 1 {
return nil, joinError(fmt.Errorf("name at proxy %d", i+1), ErrMissingField)
}
if len(input[i].Upstream) < 1 {
return nil, joinError(fmt.Errorf("upstream at proxy %d", i+1), ErrMissingField)
}
if input[i].Enabled == nil {
input[i].Enabled = &t
}
}
proxies := make([]*Proxy, 0, len(input))
for i := range input {
proxy := NewProxy(server, input[i].Name, input[i].Listen, input[i].Upstream)
err = collection.AddOrReplace(proxy, *input[i].Enabled)
if err != nil {
return proxies, err
}
proxies = append(proxies, proxy)
}
return proxies, err
}
func (collection *ProxyCollection) Proxies() map[string]*Proxy {
collection.RLock()
defer collection.RUnlock()
// Copy the map since using the existing one isn't thread-safe
proxies := make(map[string]*Proxy, len(collection.proxies))
for k, v := range collection.proxies {
proxies[k] = v
}
return proxies
}
func (collection *ProxyCollection) Get(name string) (*Proxy, error) {
collection.RLock()
defer collection.RUnlock()
return collection.getByName(name)
}
func (collection *ProxyCollection) Remove(name string) error {
collection.Lock()
defer collection.Unlock()
proxy, err := collection.getByName(name)
if err != nil {
return err
}
proxy.Stop()
delete(collection.proxies, proxy.Name)
return nil
}
func (collection *ProxyCollection) Clear() error {
collection.Lock()
defer collection.Unlock()
for _, proxy := range collection.proxies {
proxy.Stop()
delete(collection.proxies, proxy.Name)
}
return nil
}
// getByName returns a proxy by its name. Its used from #remove and #get.
// It assumes the lock has already been acquired.
func (collection *ProxyCollection) getByName(name string) (*Proxy, error) {
proxy, exists := collection.proxies[name]
if !exists {
return nil, ErrProxyNotFound
}
return proxy, nil
}