Keep track of upstream connections in a list

This commit is contained in:
Simon Ser 2020-02-06 22:25:32 +01:00
parent 89f1944d30
commit 3b0639bacc
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -45,6 +45,7 @@ type Server struct {
lock sync.Mutex lock sync.Mutex
downstreamConns []*downstreamConn downstreamConns []*downstreamConn
upstreamConns []*upstreamConn
} }
func (s *Server) prefix() *irc.Prefix { func (s *Server) prefix() *irc.Prefix {
@ -61,9 +62,23 @@ func (s *Server) Run() {
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 return
} }
s.lock.Lock()
s.upstreamConns = append(s.upstreamConns, conn)
s.lock.Unlock()
if err := conn.readMessages(); err != nil { if err := conn.readMessages(); err != nil {
conn.logger.Printf("failed to handle messages: %v", err) conn.logger.Printf("failed to handle messages: %v", err)
} }
s.lock.Lock()
for i, c := range s.upstreamConns {
if c == conn {
s.upstreamConns = append(s.upstreamConns[:i], s.upstreamConns[i+1:]...)
break
}
}
s.lock.Unlock()
}() }()
} }
} }
@ -80,9 +95,11 @@ func (s *Server) Serve(ln net.Listener) error {
s.lock.Lock() s.lock.Lock()
s.downstreamConns = append(s.downstreamConns, conn) s.downstreamConns = append(s.downstreamConns, conn)
s.lock.Unlock() s.lock.Unlock()
if err := conn.readMessages(); err != nil { if err := conn.readMessages(); err != nil {
conn.logger.Printf("failed to handle messages: %v", err) conn.logger.Printf("failed to handle messages: %v", err)
} }
s.lock.Lock() s.lock.Lock()
for i, c := range s.downstreamConns { for i, c := range s.downstreamConns {
if c == conn { if c == conn {