-
Notifications
You must be signed in to change notification settings - Fork 7
/
proxy.go
147 lines (123 loc) · 3.06 KB
/
proxy.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
package main
import (
"errors"
log "github.com/sirupsen/logrus"
"net"
"sync"
"time"
)
type ProxyMgr struct {
proxies []*Proxy
}
func NewProxyMgr() *ProxyMgr {
return &ProxyMgr{proxies: make([]*Proxy, 0)}
}
func (p *ProxyMgr) AddProxy(proxy *Proxy) {
p.proxies = append(p.proxies, proxy)
}
func (p *ProxyMgr) RemoveProxy(name string) {
for index, proxy := range p.proxies {
if proxy.name == name {
p.proxies = append(p.proxies[0:index], p.proxies[index+1:]...)
}
}
}
func (p *ProxyMgr) GetProxy(name string) (*Proxy, error) {
for _, proxy := range p.proxies {
if proxy.name == name {
return proxy, nil
}
}
return nil, errors.New("Fail to find proxy")
}
func (p *ProxyMgr) GetAllProxy() []*Proxy {
r := make([]*Proxy, 0)
r = append(r, p.proxies...)
return r
}
// Start start all the proxies
func (p *ProxyMgr) Run() {
var wg sync.WaitGroup
for _, proxy := range p.proxies {
wg.Add(1)
go p.startProxy(proxy, &wg)
}
wg.Wait()
}
func (p *ProxyMgr) startProxy(proxy *Proxy, wg *sync.WaitGroup) {
defer wg.Done()
proxy.Run()
}
type Proxy struct {
name string
addr string
requestTimeout time.Duration
seqIdAllocator *SeqIdAllocator
loadBalancer LoadBalancer
clients []*Client
clientLock sync.Mutex
}
// NewProxy create a thrift proxy listening on the addr
// and all received message will be forward by loadBalancer
// to backend thrift servers
func NewProxy(name string,
addr string,
requestTimeout time.Duration,
loadBalancer LoadBalancer) *Proxy {
proxy := &Proxy{name: name,
addr: addr,
requestTimeout: requestTimeout,
seqIdAllocator: NewSeqIdAllocator(),
loadBalancer: loadBalancer,
clients: make([]*Client, 0)}
return proxy
}
func (p *Proxy) Run() {
log.WithFields(log.Fields{"name": p.name}).Info("Start proxy")
ln, err := net.Listen("tcp", p.addr)
if err != nil {
log.WithFields(log.Fields{"address": p.addr}).Error("Fail to listen on address")
return
}
log.WithFields(log.Fields{"address": p.addr}).Info("Listen on address")
for {
conn, err := ln.Accept()
if err == nil {
client := NewClient(conn,
p.requestTimeout,
p.seqIdAllocator,
p.loadBalancer,
p.removeClient)
log.WithFields(log.Fields{"address": conn.RemoteAddr().String()}).Info("Accept connection")
p.addClient(client)
}
}
}
func (p *Proxy) GetName() string {
return p.name
}
func (p *Proxy) AddBackend(backendInfo *BackendInfo) {
p.loadBalancer.AddBackend(backendInfo)
}
func (p *Proxy) RemoveBackend(addr string) {
p.loadBalancer.RemoveBackend(addr)
}
func (p *Proxy) GetAllBackends() []Backend {
return p.loadBalancer.GetAllBackends()
}
func (p *Proxy) addClient(client *Client) {
p.clientLock.Lock()
defer p.clientLock.Unlock()
p.clients = append(p.clients, client)
}
func (p *Proxy) removeClient(c *Client) {
p.clientLock.Lock()
defer p.clientLock.Unlock()
for i, value := range p.clients {
if value == c {
log.WithFields(log.Fields{"address": c.remoteAddr().String()}).Info("Succeed to remove client")
p.clients = append(p.clients[:i], p.clients[i+1:]...)
break
}
}
}