-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy_backend.go
More file actions
53 lines (43 loc) · 1.79 KB
/
Copy pathproxy_backend.go
File metadata and controls
53 lines (43 loc) · 1.79 KB
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
package dewy
import "fmt"
// proxyBackendUpdater adapts *Dewy to the container.BackendUpdater interface
// so the rolling deploy can register/unregister TCP-proxy backends as new
// replicas come up and old replicas are taken down.
//
// proxyBackendUpdater is a defined type whose underlying type is Dewy, not
// an alias (which would be `type proxyBackendUpdater = Dewy`). The defined
// form gives us a separate method set scoped to the BackendUpdater contract
// while sharing memory with *Dewy, so the explicit conversion
// (*proxyBackendUpdater)(d) is zero-cost: no wrapper struct, no captured
// closures, just a pointer reinterpretation.
type proxyBackendUpdater Dewy
func (p *proxyBackendUpdater) AddBackend(host string, mappedPort, proxyPort int) error {
return (*Dewy)(p).addProxyBackend(host, mappedPort, proxyPort)
}
func (p *proxyBackendUpdater) RemoveBackend(host string, mappedPort, proxyPort int) error {
return (*Dewy)(p).removeProxyBackend(host, mappedPort, proxyPort)
}
// addProxyBackend adds a new backend to the appropriate TCP proxy.
func (d *Dewy) addProxyBackend(host string, port int, proxyPort int) error {
d.proxyMutex.RLock()
proxy, exists := d.tcpProxies[proxyPort]
d.proxyMutex.RUnlock()
if !exists {
return fmt.Errorf("no proxy configured for port %d", proxyPort)
}
proxy.addBackend(host, port)
return nil
}
// removeProxyBackend removes a backend from the appropriate TCP proxy.
func (d *Dewy) removeProxyBackend(host string, port int, proxyPort int) error {
d.proxyMutex.RLock()
proxy, exists := d.tcpProxies[proxyPort]
d.proxyMutex.RUnlock()
if !exists {
return fmt.Errorf("no proxy configured for port %d", proxyPort)
}
if !proxy.removeBackend(host, port) {
return fmt.Errorf("backend %s:%d not found for proxy port %d", host, port, proxyPort)
}
return nil
}