mirror of
https://github.com/simon987/Architeuthis.git
synced 2025-04-19 15:36:42 +00:00
load-balancer algorithm tweaks
This commit is contained in:
parent
98c7f12cf1
commit
806bd9cdb4
40
config.json
40
config.json
@ -8,28 +8,52 @@
|
|||||||
{
|
{
|
||||||
"name": "p0",
|
"name": "p0",
|
||||||
"url": ""
|
"url": ""
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "p1",
|
|
||||||
"url": ""
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"hosts": [
|
"hosts": [
|
||||||
{
|
{
|
||||||
"host": "*",
|
"host": "*",
|
||||||
"every": "500s",
|
"every": "500ms",
|
||||||
"burst": 25,
|
"burst": 25,
|
||||||
"headers": {
|
"headers": {
|
||||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||||
"Cache-Control": "max-age=0",
|
"Cache-Control": "max-age=0",
|
||||||
"Connection": "keep-alive",
|
"Connection": "keep-alive",
|
||||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0",
|
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0"
|
||||||
"X-Test": "default"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"host": "*.reddit.com",
|
"host": "*.reddit.com",
|
||||||
"every": "2s"
|
"every": "2s",
|
||||||
|
"burst": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"host": ".twitter.com",
|
||||||
|
"every": "24s",
|
||||||
|
"burst": 20
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"host": ".pbs.twimg.com",
|
||||||
|
"every": "125ms"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"host": "*.cdninstagram",
|
||||||
|
"every": "250ms"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"host": ".www.instagram.com",
|
||||||
|
"every": "30s",
|
||||||
|
"burst": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"host": ".deviantart.com",
|
||||||
|
"every": "2s",
|
||||||
|
"burst": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"host": ".s3.amazonaws.com",
|
||||||
|
"every": "10s",
|
||||||
|
"burst": 3
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
36
main.go
36
main.go
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/ryanuber/go-glob"
|
"github.com/ryanuber/go-glob"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
@ -47,14 +48,15 @@ func (a ByConnectionCount) Less(i, j int) bool {
|
|||||||
|
|
||||||
func (p *Proxy) getLimiter(host string) *rate.Limiter {
|
func (p *Proxy) getLimiter(host string) *rate.Limiter {
|
||||||
|
|
||||||
expLimit, ok := p.Limiters[host]
|
for hostGlob, limiter := range p.Limiters {
|
||||||
if !ok {
|
if glob.Glob(hostGlob, host) {
|
||||||
newExpiringLimiter := p.makeNewLimiter(host)
|
limiter.LastRead = time.Now()
|
||||||
return newExpiringLimiter.Limiter
|
return limiter.Limiter
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expLimit.LastRead = time.Now()
|
newExpiringLimiter := p.makeNewLimiter(host)
|
||||||
return expLimit.Limiter
|
return newExpiringLimiter.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) makeNewLimiter(host string) *ExpiringLimiter {
|
func (p *Proxy) makeNewLimiter(host string) *ExpiringLimiter {
|
||||||
@ -85,8 +87,26 @@ func simplifyHost(host string) string {
|
|||||||
|
|
||||||
func (b *Balancer) chooseProxy() *Proxy {
|
func (b *Balancer) chooseProxy() *Proxy {
|
||||||
|
|
||||||
sort.Sort(ByConnectionCount(b.proxies))
|
if len(b.proxies) == 0 {
|
||||||
return b.proxies[0]
|
return b.proxies[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(ByConnectionCount(b.proxies))
|
||||||
|
|
||||||
|
p0 := b.proxies[0]
|
||||||
|
proxiesWithSameConnCount := make([]*Proxy, 0)
|
||||||
|
for _, p := range b.proxies {
|
||||||
|
if p.Connections != p0.Connections {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxiesWithSameConnCount = append(proxiesWithSameConnCount, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(proxiesWithSameConnCount) > 1 {
|
||||||
|
return proxiesWithSameConnCount[rand.Intn(len(proxiesWithSameConnCount))]
|
||||||
|
} else {
|
||||||
|
return p0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Balancer {
|
func New() *Balancer {
|
||||||
@ -105,7 +125,7 @@ func New() *Balancer {
|
|||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"proxy": p.Name,
|
"proxy": p.Name,
|
||||||
"connexions": p.Connections,
|
"connexions": p.Connections,
|
||||||
"host": r.Host,
|
"url": r.URL,
|
||||||
}).Trace("Routing request")
|
}).Trace("Routing request")
|
||||||
|
|
||||||
resp, err := p.processRequest(r)
|
resp, err := p.processRequest(r)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user