Send 'Retry-After' to comply with RFC6585.
This commit is contained in:
parent
027093a5a5
commit
8d75aba7eb
29 changed files with 435 additions and 172 deletions
1
vendor/github.com/vulcand/oxy/roundrobin/RequestRewriteListener.go
generated
vendored
1
vendor/github.com/vulcand/oxy/roundrobin/RequestRewriteListener.go
generated
vendored
|
@ -2,4 +2,5 @@ package roundrobin
|
|||
|
||||
import "net/http"
|
||||
|
||||
// RequestRewriteListener function to rewrite request
|
||||
type RequestRewriteListener func(oldReq *http.Request, newReq *http.Request)
|
||||
|
|
62
vendor/github.com/vulcand/oxy/roundrobin/rebalancer.go
generated
vendored
62
vendor/github.com/vulcand/oxy/roundrobin/rebalancer.go
generated
vendored
|
@ -16,13 +16,14 @@ import (
|
|||
// RebalancerOption - functional option setter for rebalancer
|
||||
type RebalancerOption func(*Rebalancer) error
|
||||
|
||||
// Meter measures server peformance and returns it's relative value via rating
|
||||
// Meter measures server performance and returns it's relative value via rating
|
||||
type Meter interface {
|
||||
Rating() float64
|
||||
Record(int, time.Duration)
|
||||
IsReady() bool
|
||||
}
|
||||
|
||||
// NewMeterFn type of functions to create new Meter
|
||||
type NewMeterFn func() (Meter, error)
|
||||
|
||||
// Rebalancer increases weights on servers that perform better than others. It also rolls back to original weights
|
||||
|
@ -52,8 +53,11 @@ type Rebalancer struct {
|
|||
stickySession *StickySession
|
||||
|
||||
requestRewriteListener RequestRewriteListener
|
||||
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
// RebalancerClock sets a clock
|
||||
func RebalancerClock(clock timetools.TimeProvider) RebalancerOption {
|
||||
return func(r *Rebalancer) error {
|
||||
r.clock = clock
|
||||
|
@ -61,6 +65,7 @@ func RebalancerClock(clock timetools.TimeProvider) RebalancerOption {
|
|||
}
|
||||
}
|
||||
|
||||
// RebalancerBackoff sets a beck off duration
|
||||
func RebalancerBackoff(d time.Duration) RebalancerOption {
|
||||
return func(r *Rebalancer) error {
|
||||
r.backoffDuration = d
|
||||
|
@ -68,6 +73,7 @@ func RebalancerBackoff(d time.Duration) RebalancerOption {
|
|||
}
|
||||
}
|
||||
|
||||
// RebalancerMeter sets a Meter builder function
|
||||
func RebalancerMeter(newMeter NewMeterFn) RebalancerOption {
|
||||
return func(r *Rebalancer) error {
|
||||
r.newMeter = newMeter
|
||||
|
@ -83,6 +89,7 @@ func RebalancerErrorHandler(h utils.ErrorHandler) RebalancerOption {
|
|||
}
|
||||
}
|
||||
|
||||
// RebalancerStickySession sets a sticky session
|
||||
func RebalancerStickySession(stickySession *StickySession) RebalancerOption {
|
||||
return func(r *Rebalancer) error {
|
||||
r.stickySession = stickySession
|
||||
|
@ -90,7 +97,7 @@ func RebalancerStickySession(stickySession *StickySession) RebalancerOption {
|
|||
}
|
||||
}
|
||||
|
||||
// RebalancerErrorHandler is a functional argument that sets error handler of the server
|
||||
// RebalancerRequestRewriteListener is a functional argument that sets error handler of the server
|
||||
func RebalancerRequestRewriteListener(rrl RequestRewriteListener) RebalancerOption {
|
||||
return func(r *Rebalancer) error {
|
||||
r.requestRewriteListener = rrl
|
||||
|
@ -98,11 +105,14 @@ func RebalancerRequestRewriteListener(rrl RequestRewriteListener) RebalancerOpti
|
|||
}
|
||||
}
|
||||
|
||||
// NewRebalancer creates a new Rebalancer
|
||||
func NewRebalancer(handler balancerHandler, opts ...RebalancerOption) (*Rebalancer, error) {
|
||||
rb := &Rebalancer{
|
||||
mtx: &sync.Mutex{},
|
||||
next: handler,
|
||||
stickySession: nil,
|
||||
|
||||
log: log.StandardLogger(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
if err := o(rb); err != nil {
|
||||
|
@ -134,6 +144,17 @@ func NewRebalancer(handler balancerHandler, opts ...RebalancerOption) (*Rebalanc
|
|||
return rb, nil
|
||||
}
|
||||
|
||||
// RebalancerLogger defines the logger the rebalancer will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func RebalancerLogger(l *log.Logger) RebalancerOption {
|
||||
return func(rb *Rebalancer) error {
|
||||
rb.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Servers gets all servers
|
||||
func (rb *Rebalancer) Servers() []*url.URL {
|
||||
rb.mtx.Lock()
|
||||
defer rb.mtx.Unlock()
|
||||
|
@ -142,8 +163,8 @@ func (rb *Rebalancer) Servers() []*url.URL {
|
|||
}
|
||||
|
||||
func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if log.GetLevel() >= log.DebugLevel {
|
||||
logEntry := log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
if rb.log.Level >= log.DebugLevel {
|
||||
logEntry := rb.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/roundrobin/rebalancer: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/roundrobin/rebalancer: completed ServeHttp on request")
|
||||
}
|
||||
|
@ -169,25 +190,25 @@ func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
|
||||
if !stuck {
|
||||
url, err := rb.next.NextServer()
|
||||
fwdURL, err := rb.next.NextServer()
|
||||
if err != nil {
|
||||
rb.errHandler.ServeHTTP(w, req, err)
|
||||
return
|
||||
}
|
||||
|
||||
if log.GetLevel() >= log.DebugLevel {
|
||||
//log which backend URL we're sending this request to
|
||||
log.WithFields(log.Fields{"Request": utils.DumpHttpRequest(req), "ForwardURL": url}).Debugf("vulcand/oxy/roundrobin/rebalancer: Forwarding this request to URL")
|
||||
// log which backend URL we're sending this request to
|
||||
log.WithFields(log.Fields{"Request": utils.DumpHttpRequest(req), "ForwardURL": fwdURL}).Debugf("vulcand/oxy/roundrobin/rebalancer: Forwarding this request to URL")
|
||||
}
|
||||
|
||||
if rb.stickySession != nil {
|
||||
rb.stickySession.StickBackend(url, &w)
|
||||
rb.stickySession.StickBackend(fwdURL, &w)
|
||||
}
|
||||
|
||||
newReq.URL = url
|
||||
newReq.URL = fwdURL
|
||||
}
|
||||
|
||||
//Emit event to a listener if one exists
|
||||
// Emit event to a listener if one exists
|
||||
if rb.requestRewriteListener != nil {
|
||||
rb.requestRewriteListener(req, &newReq)
|
||||
}
|
||||
|
@ -215,6 +236,7 @@ func (rb *Rebalancer) reset() {
|
|||
rb.ratings = make([]float64, len(rb.servers))
|
||||
}
|
||||
|
||||
// Wrap sets the next handler to be called by rebalancer handler.
|
||||
func (rb *Rebalancer) Wrap(next balancerHandler) error {
|
||||
if rb.next != nil {
|
||||
return fmt.Errorf("already bound to %T", rb.next)
|
||||
|
@ -223,6 +245,7 @@ func (rb *Rebalancer) Wrap(next balancerHandler) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UpsertServer upsert a server
|
||||
func (rb *Rebalancer) UpsertServer(u *url.URL, options ...ServerOption) error {
|
||||
rb.mtx.Lock()
|
||||
defer rb.mtx.Unlock()
|
||||
|
@ -239,6 +262,7 @@ func (rb *Rebalancer) UpsertServer(u *url.URL, options ...ServerOption) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// RemoveServer remove a server
|
||||
func (rb *Rebalancer) RemoveServer(u *url.URL) error {
|
||||
rb.mtx.Lock()
|
||||
defer rb.mtx.Unlock()
|
||||
|
@ -289,7 +313,7 @@ func (rb *Rebalancer) findServer(u *url.URL) (*rbServer, int) {
|
|||
return nil, -1
|
||||
}
|
||||
|
||||
// Called on every load balancer ServeHTTP call, returns the suggested weights
|
||||
// adjustWeights Called on every load balancer ServeHTTP call, returns the suggested weights
|
||||
// on every call, can adjust weights if needed.
|
||||
func (rb *Rebalancer) adjustWeights() {
|
||||
rb.mtx.Lock()
|
||||
|
@ -319,7 +343,7 @@ func (rb *Rebalancer) adjustWeights() {
|
|||
|
||||
func (rb *Rebalancer) applyWeights() {
|
||||
for _, srv := range rb.servers {
|
||||
log.Debugf("upsert server %v, weight %v", srv.url, srv.curWeight)
|
||||
rb.log.Debugf("upsert server %v, weight %v", srv.url, srv.curWeight)
|
||||
rb.next.UpsertServer(srv.url, Weight(srv.curWeight))
|
||||
}
|
||||
}
|
||||
|
@ -331,7 +355,7 @@ func (rb *Rebalancer) setMarkedWeights() bool {
|
|||
if srv.good {
|
||||
weight := increase(srv.curWeight)
|
||||
if weight <= FSMMaxWeight {
|
||||
log.Debugf("increasing weight of %v from %v to %v", srv.url, srv.curWeight, weight)
|
||||
rb.log.Debugf("increasing weight of %v from %v to %v", srv.url, srv.curWeight, weight)
|
||||
srv.curWeight = weight
|
||||
changed = true
|
||||
}
|
||||
|
@ -378,7 +402,7 @@ func (rb *Rebalancer) markServers() bool {
|
|||
}
|
||||
}
|
||||
if len(g) != 0 && len(b) != 0 {
|
||||
log.Debugf("bad: %v good: %v, ratings: %v", b, g, rb.ratings)
|
||||
rb.log.Debugf("bad: %v good: %v, ratings: %v", b, g, rb.ratings)
|
||||
}
|
||||
return len(g) != 0 && len(b) != 0
|
||||
}
|
||||
|
@ -433,9 +457,8 @@ func decrease(target, current int) int {
|
|||
adjusted := current / FSMGrowFactor
|
||||
if adjusted < target {
|
||||
return target
|
||||
} else {
|
||||
return adjusted
|
||||
}
|
||||
return adjusted
|
||||
}
|
||||
|
||||
// rebalancer server record that keeps track of the original weight supplied by user
|
||||
|
@ -448,9 +471,9 @@ type rbServer struct {
|
|||
}
|
||||
|
||||
const (
|
||||
// This is the maximum weight that handler will set for the server
|
||||
// FSMMaxWeight is the maximum weight that handler will set for the server
|
||||
FSMMaxWeight = 4096
|
||||
// Multiplier for the server weight
|
||||
// FSMGrowFactor Multiplier for the server weight
|
||||
FSMGrowFactor = 4
|
||||
)
|
||||
|
||||
|
@ -460,10 +483,12 @@ type codeMeter struct {
|
|||
codeE int
|
||||
}
|
||||
|
||||
// Rating gets ratio
|
||||
func (n *codeMeter) Rating() float64 {
|
||||
return n.r.Ratio()
|
||||
}
|
||||
|
||||
// Record records a meter
|
||||
func (n *codeMeter) Record(code int, d time.Duration) {
|
||||
if code >= n.codeS && code < n.codeE {
|
||||
n.r.IncA(1)
|
||||
|
@ -472,6 +497,7 @@ func (n *codeMeter) Record(code int, d time.Duration) {
|
|||
}
|
||||
}
|
||||
|
||||
// IsReady returns true if the counter is ready
|
||||
func (n *codeMeter) IsReady() bool {
|
||||
return n.r.IsReady()
|
||||
}
|
||||
|
|
41
vendor/github.com/vulcand/oxy/roundrobin/rr.go
generated
vendored
41
vendor/github.com/vulcand/oxy/roundrobin/rr.go
generated
vendored
|
@ -1,4 +1,4 @@
|
|||
// package roundrobin implements dynamic weighted round robin load balancer http handler
|
||||
// Package roundrobin implements dynamic weighted round robin load balancer http handler
|
||||
package roundrobin
|
||||
|
||||
import (
|
||||
|
@ -30,6 +30,7 @@ func ErrorHandler(h utils.ErrorHandler) LBOption {
|
|||
}
|
||||
}
|
||||
|
||||
// EnableStickySession enable sticky session
|
||||
func EnableStickySession(stickySession *StickySession) LBOption {
|
||||
return func(s *RoundRobin) error {
|
||||
s.stickySession = stickySession
|
||||
|
@ -37,7 +38,7 @@ func EnableStickySession(stickySession *StickySession) LBOption {
|
|||
}
|
||||
}
|
||||
|
||||
// ErrorHandler is a functional argument that sets error handler of the server
|
||||
// RoundRobinRequestRewriteListener is a functional argument that sets error handler of the server
|
||||
func RoundRobinRequestRewriteListener(rrl RequestRewriteListener) LBOption {
|
||||
return func(s *RoundRobin) error {
|
||||
s.requestRewriteListener = rrl
|
||||
|
@ -45,6 +46,7 @@ func RoundRobinRequestRewriteListener(rrl RequestRewriteListener) LBOption {
|
|||
}
|
||||
}
|
||||
|
||||
// RoundRobin implements dynamic weighted round robin load balancer http handler
|
||||
type RoundRobin struct {
|
||||
mutex *sync.Mutex
|
||||
next http.Handler
|
||||
|
@ -55,8 +57,11 @@ type RoundRobin struct {
|
|||
currentWeight int
|
||||
stickySession *StickySession
|
||||
requestRewriteListener RequestRewriteListener
|
||||
|
||||
log *log.Logger
|
||||
}
|
||||
|
||||
// New created a new RoundRobin
|
||||
func New(next http.Handler, opts ...LBOption) (*RoundRobin, error) {
|
||||
rr := &RoundRobin{
|
||||
next: next,
|
||||
|
@ -64,6 +69,8 @@ func New(next http.Handler, opts ...LBOption) (*RoundRobin, error) {
|
|||
mutex: &sync.Mutex{},
|
||||
servers: []*server{},
|
||||
stickySession: nil,
|
||||
|
||||
log: log.StandardLogger(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
if err := o(rr); err != nil {
|
||||
|
@ -76,13 +83,24 @@ func New(next http.Handler, opts ...LBOption) (*RoundRobin, error) {
|
|||
return rr, nil
|
||||
}
|
||||
|
||||
// RoundRobinLogger defines the logger the round robin load balancer will use.
|
||||
//
|
||||
// It defaults to logrus.StandardLogger(), the global logger used by logrus.
|
||||
func RoundRobinLogger(l *log.Logger) LBOption {
|
||||
return func(r *RoundRobin) error {
|
||||
r.log = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Next returns the next handler
|
||||
func (r *RoundRobin) Next() http.Handler {
|
||||
return r.next
|
||||
}
|
||||
|
||||
func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if log.GetLevel() >= log.DebugLevel {
|
||||
logEntry := log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
if r.log.Level >= log.DebugLevel {
|
||||
logEntry := r.log.WithField("Request", utils.DumpHttpRequest(req))
|
||||
logEntry.Debug("vulcand/oxy/roundrobin/rr: begin ServeHttp on request")
|
||||
defer logEntry.Debug("vulcand/oxy/roundrobin/rr: completed ServeHttp on request")
|
||||
}
|
||||
|
@ -116,12 +134,12 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||
newReq.URL = url
|
||||
}
|
||||
|
||||
if log.GetLevel() >= log.DebugLevel {
|
||||
//log which backend URL we're sending this request to
|
||||
log.WithFields(log.Fields{"Request": utils.DumpHttpRequest(req), "ForwardURL": newReq.URL}).Debugf("vulcand/oxy/roundrobin/rr: Forwarding this request to URL")
|
||||
if r.log.Level >= log.DebugLevel {
|
||||
// log which backend URL we're sending this request to
|
||||
r.log.WithFields(log.Fields{"Request": utils.DumpHttpRequest(req), "ForwardURL": newReq.URL}).Debugf("vulcand/oxy/roundrobin/rr: Forwarding this request to URL")
|
||||
}
|
||||
|
||||
//Emit event to a listener if one exists
|
||||
// Emit event to a listener if one exists
|
||||
if r.requestRewriteListener != nil {
|
||||
r.requestRewriteListener(req, &newReq)
|
||||
}
|
||||
|
@ -129,6 +147,7 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||
r.next.ServeHTTP(w, &newReq)
|
||||
}
|
||||
|
||||
// NextServer gets the next server
|
||||
func (r *RoundRobin) NextServer() (*url.URL, error) {
|
||||
srv, err := r.nextServer()
|
||||
if err != nil {
|
||||
|
@ -172,6 +191,7 @@ func (r *RoundRobin) nextServer() (*server, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// RemoveServer remove a server
|
||||
func (r *RoundRobin) RemoveServer(u *url.URL) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
@ -185,6 +205,7 @@ func (r *RoundRobin) RemoveServer(u *url.URL) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Servers gets servers URL
|
||||
func (r *RoundRobin) Servers() []*url.URL {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
@ -196,6 +217,7 @@ func (r *RoundRobin) Servers() []*url.URL {
|
|||
return out
|
||||
}
|
||||
|
||||
// ServerWeight gets the server weight
|
||||
func (r *RoundRobin) ServerWeight(u *url.URL) (int, bool) {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
@ -206,7 +228,7 @@ func (r *RoundRobin) ServerWeight(u *url.URL) (int, bool) {
|
|||
return -1, false
|
||||
}
|
||||
|
||||
// In case if server is already present in the load balancer, returns error
|
||||
// UpsertServer In case if server is already present in the load balancer, returns error
|
||||
func (r *RoundRobin) UpsertServer(u *url.URL, options ...ServerOption) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
@ -306,6 +328,7 @@ type server struct {
|
|||
|
||||
var defaultWeight = 1
|
||||
|
||||
// SetDefaultWeight sets the default server weight
|
||||
func SetDefaultWeight(weight int) error {
|
||||
if weight < 0 {
|
||||
return fmt.Errorf("default weight should be >= 0")
|
||||
|
|
9
vendor/github.com/vulcand/oxy/roundrobin/stickysessions.go
generated
vendored
9
vendor/github.com/vulcand/oxy/roundrobin/stickysessions.go
generated
vendored
|
@ -1,4 +1,3 @@
|
|||
// package stickysession is a mixin for load balancers that implements layer 7 (http cookie) session affinity
|
||||
package roundrobin
|
||||
|
||||
import (
|
||||
|
@ -6,12 +5,14 @@ import (
|
|||
"net/url"
|
||||
)
|
||||
|
||||
// StickySession is a mixin for load balancers that implements layer 7 (http cookie) session affinity
|
||||
type StickySession struct {
|
||||
cookieName string
|
||||
}
|
||||
|
||||
// NewStickySession creates a new StickySession
|
||||
func NewStickySession(cookieName string) *StickySession {
|
||||
return &StickySession{cookieName}
|
||||
return &StickySession{cookieName: cookieName}
|
||||
}
|
||||
|
||||
// GetBackend returns the backend URL stored in the sticky cookie, iff the backend is still in the valid list of servers.
|
||||
|
@ -32,11 +33,11 @@ func (s *StickySession) GetBackend(req *http.Request, servers []*url.URL) (*url.
|
|||
|
||||
if s.isBackendAlive(serverURL, servers) {
|
||||
return serverURL, true, nil
|
||||
} else {
|
||||
return nil, false, nil
|
||||
}
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
// StickBackend creates and sets the cookie
|
||||
func (s *StickySession) StickBackend(backend *url.URL, w *http.ResponseWriter) {
|
||||
cookie := &http.Cookie{Name: s.cookieName, Value: backend.String(), Path: "/"}
|
||||
http.SetCookie(*w, cookie)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue