/
proxypool.go
49 lines (41 loc) · 883 Bytes
/
proxypool.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
package pylon
import (
"net/http/httputil"
"net/http"
)
type ProxyPool struct {
pool chan *httputil.ReverseProxy
}
func NewProxyPool(capacity int) *ProxyPool {
return &ProxyPool{
make(chan *httputil.ReverseProxy, capacity),
}
}
func (p *ProxyPool) Get() *httputil.ReverseProxy {
var proxy *httputil.ReverseProxy
select {
case proxy = <-p.pool:
default:
proxy = NewProxy()
}
return proxy
}
func (p *ProxyPool) Put(rp *httputil.ReverseProxy) {
select {
case p.pool <- rp:
default:
}
}
func NewProxy() *httputil.ReverseProxy {
return &httputil.ReverseProxy{
Transport: NewPylonTransport(),
FlushInterval: flushInterval,
}
}
func setUpProxy(proxy *httputil.ReverseProxy, m *MicroService, host string) {
proxy.Director = func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = host
}
proxy.Transport.(*PylonTransport).setMicroService(m)
}