Add a new protocol
Co-authored-by: Gérald Croës <gerald@containo.us>
This commit is contained in:
parent
0ca2149408
commit
4a68d29ce2
231 changed files with 6895 additions and 4395 deletions
44
tcp/rr_load_balancer.go
Normal file
44
tcp/rr_load_balancer.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package tcp
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/containous/traefik/log"
|
||||
)
|
||||
|
||||
// RRLoadBalancer is a naive RoundRobin load balancer for TCP services
|
||||
type RRLoadBalancer struct {
|
||||
servers []Handler
|
||||
lock sync.RWMutex
|
||||
current int
|
||||
}
|
||||
|
||||
// NewRRLoadBalancer creates a new RRLoadBalancer
|
||||
func NewRRLoadBalancer() *RRLoadBalancer {
|
||||
return &RRLoadBalancer{}
|
||||
}
|
||||
|
||||
// ServeTCP forwards the connection to the right service
|
||||
func (r *RRLoadBalancer) ServeTCP(conn net.Conn) {
|
||||
r.next().ServeTCP(conn)
|
||||
}
|
||||
|
||||
// AddServer appends a server to the existing list
|
||||
func (r *RRLoadBalancer) AddServer(server Handler) {
|
||||
r.servers = append(r.servers, server)
|
||||
}
|
||||
|
||||
func (r *RRLoadBalancer) next() Handler {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
// FIXME handle weight
|
||||
if r.current >= len(r.servers) {
|
||||
r.current = 0
|
||||
log.Debugf("Load balancer: going back to the first available server")
|
||||
}
|
||||
handler := r.servers[r.current]
|
||||
r.current++
|
||||
return handler
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue