Set write deadlines

References: https://todo.sr.ht/~emersion/soju/26
This commit is contained in:
Simon Ser 2020-04-01 16:25:03 +02:00
parent d4de60a869
commit 29f2e93ab7
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 8 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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