Retry connecting to upstream servers

Rate-limit retries in case connecting immediately fails.
This commit is contained in:
Simon Ser 2020-03-03 15:26:19 +01:00
parent 6e12347f2b
commit c366b5320c
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -12,6 +12,7 @@ import (
// TODO: make configurable // TODO: make configurable
var keepAlivePeriod = time.Minute var keepAlivePeriod = time.Minute
var retryConnectMinDelay = time.Minute
func setKeepAlive(c net.Conn) error { func setKeepAlive(c net.Conn) error {
tcpConn, ok := c.(*net.TCPConn) tcpConn, ok := c.(*net.TCPConn)
@ -137,22 +138,20 @@ func (s *Server) prefix() *irc.Prefix {
return &irc.Prefix{Name: s.Hostname} return &irc.Prefix{Name: s.Hostname}
} }
func (s *Server) Run() { func (s *Server) runUpstream(u *user, upstream *Upstream) {
// TODO: multi-user var lastTry time.Time
u := newUser(s, "jounce") for {
if dur := time.Now().Sub(lastTry); dur < retryConnectMinDelay {
delay := retryConnectMinDelay - dur
s.Logger.Printf("waiting %v before trying to reconnect to %q", delay.Truncate(time.Second), upstream.Addr)
time.Sleep(delay)
}
lastTry = time.Now()
s.lock.Lock()
s.users[u.username] = u
s.lock.Unlock()
for i := range s.Upstreams {
upstream := &s.Upstreams[i]
// TODO: retry connecting
go func() {
uc, err := connectToUpstream(u, upstream) uc, err := connectToUpstream(u, upstream)
if err != nil { if err != nil {
s.Logger.Printf("failed to connect to upstream server %q: %v", upstream.Addr, err) s.Logger.Printf("failed to connect to upstream server %q: %v", upstream.Addr, err)
return continue
} }
uc.register() uc.register()
@ -174,7 +173,19 @@ func (s *Server) Run() {
} }
} }
u.lock.Unlock() u.lock.Unlock()
}() }
}
func (s *Server) Run() {
// TODO: multi-user
u := newUser(s, "jounce")
s.lock.Lock()
s.users[u.username] = u
s.lock.Unlock()
for i := range s.Upstreams {
go s.runUpstream(u, &s.Upstreams[i])
} }
} }