-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
executable file
·96 lines (88 loc) · 2.1 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
package fasthttputil
import (
"fmt"
"net/http"
"net/url"
"sync/atomic"
"time"
"github.com/valyala/fasthttp"
)
var (
XForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
XForwardedHost = http.CanonicalHeaderKey("X-Forwarded-Host")
XRealIP = http.CanonicalHeaderKey("X-Real-IP")
)
type Target struct {
Name string `json:"name,omitempty"`
URL string `json:"url"`
MaxConns int `json:"max_conns"`
Status string `json:"status"`
Host string `json:"host"`
}
type Proxy struct {
client *fasthttp.HostClient
status string
MaxConns int32
Conns int32
Name string
Host []byte
Fails int32
}
func NewProxyClient(to *Target) (*Proxy, error) {
var status string
status = to.Status
url, err := url.Parse(to.Status)
if err != nil {
status = to.URL
}
if status == "" {
status = fmt.Sprintf("http://%s", to.Host)
}
url, err = url.Parse(to.URL)
if err != nil {
return nil, err
}
if to.Name == "" {
to.Name = to.URL
}
return &Proxy{client: &fasthttp.HostClient{Addr: url.Host,
MaxConns: to.MaxConns,
MaxIdleConnDuration: time.Minute * 2,
}, status: status, Name: to.Name, Host: []byte(to.Host)}, err
}
func (p *Proxy) Handler(ctx *fasthttp.RequestCtx) (err error) {
err = p.Do(&ctx.Request, &ctx.Response)
if err != nil {
atomic.AddInt32(&p.Fails, 1)
}
return
}
func (p *Proxy) Do(req *fasthttp.Request, resp *fasthttp.Response) (err error) {
req.Header.Del("Connection")
req.Header.SetBytesV(XForwardedHost, req.Host())
if len(p.Host) > 0 {
req.SetHostBytes(p.Host)
}
err = p.client.Do(req, resp)
resp.Header.Del("Connection")
return
}
// Full checks whether the upstream host has reached its maximum connections
func (p Proxy) Full() bool {
return p.MaxConns > 0 && atomic.LoadInt32(&p.Conns) >= p.MaxConns
}
func (p *Proxy) Down() bool {
return atomic.LoadInt32(&p.Fails) > 0
}
func (p Proxy) Available() bool {
return !p.Down() && !p.Full()
}
func (p Proxy) StatusOK() bool {
a, _, err := p.client.Get(nil, p.status)
return err == nil && a == 200
}
func (p *Proxy) HealthCheck() {
if !p.StatusOK() {
atomic.AddInt32(&p.Fails, 1)
}
}