diff --git a/downstream.go b/downstream.go index 1496ce2..96ee7b3 100644 --- a/downstream.go +++ b/downstream.go @@ -244,6 +244,8 @@ func (dc *downstreamConn) readMessages(ch chan<- event) error { } func (dc *downstreamConn) writeMessages() error { + // TODO: any SendMessage call after the connection is closed will + // either block or drop for { var err error var closed bool @@ -252,6 +254,7 @@ func (dc *downstreamConn) writeMessages() error { if dc.srv.Debug { dc.logger.Printf("sent: %v", msg) } + dc.net.SetWriteDeadline(time.Now().Add(writeTimeout)) err = dc.irc.WriteMessage(msg) case <-dc.closed: closed = true diff --git a/server.go b/server.go index 0ebd1c8..94af998 100644 --- a/server.go +++ b/server.go @@ -13,6 +13,7 @@ import ( // TODO: make configurable var keepAlivePeriod = time.Minute var retryConnectMinDelay = time.Minute +var writeTimeout = 10 * time.Second func setKeepAlive(c net.Conn) error { tcpConn, ok := c.(*net.TCPConn) diff --git a/upstream.go b/upstream.go index 341dc03..2c430aa 100644 --- a/upstream.go +++ b/upstream.go @@ -111,6 +111,8 @@ func connectToUpstream(network *network) (*upstreamConn, error) { } go func() { + // TODO: any SendMessage call after the connection is closed will + // either block or drop for { var closed bool select { @@ -118,8 +120,10 @@ func connectToUpstream(network *network) (*upstreamConn, error) { if uc.srv.Debug { uc.logger.Printf("sent: %v", msg) } + uc.net.SetWriteDeadline(time.Now().Add(writeTimeout)) if err := uc.irc.WriteMessage(msg); err != nil { uc.logger.Printf("failed to write message: %v", err) + closed = true } case <-uc.closed: closed = true