1
0
Fork 0

Don't add TCP proxy when error occurs during creation.

This commit is contained in:
Ludovic Fernandez 2019-05-09 14:30:06 +02:00 committed by Traefiker Bot
parent 2617de2cdd
commit c2345c6e9a
6 changed files with 149 additions and 55 deletions

View file

@ -18,15 +18,15 @@ func NewProxy(address string) (*Proxy, error) {
if err != nil {
return nil, err
}
return &Proxy{
target: tcpAddr,
}, nil
return &Proxy{target: tcpAddr}, nil
}
// ServeTCP forwards the connection to a service
func (p *Proxy) ServeTCP(conn net.Conn) {
log.Debugf("Handling connection from %s", conn.RemoteAddr())
defer conn.Close()
connBackend, err := net.DialTCP("tcp", nil, p.target)
if err != nil {
log.Errorf("Error while connection to backend: %v", err)

View file

@ -21,6 +21,11 @@ func NewRRLoadBalancer() *RRLoadBalancer {
// ServeTCP forwards the connection to the right service
func (r *RRLoadBalancer) ServeTCP(conn net.Conn) {
if len(r.servers) == 0 {
log.WithoutContext().Error("no available server")
return
}
r.next().ServeTCP(conn)
}
@ -38,6 +43,7 @@ func (r *RRLoadBalancer) next() Handler {
r.current = 0
log.Debugf("Load balancer: going back to the first available server")
}
handler := r.servers[r.current]
r.current++
return handler