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 { func (dc *downstreamConn) writeMessages() error {
// TODO: any SendMessage call after the connection is closed will
// either block or drop
for { for {
var err error var err error
var closed bool var closed bool
@ -252,6 +254,7 @@ func (dc *downstreamConn) writeMessages() error {
if dc.srv.Debug { if dc.srv.Debug {
dc.logger.Printf("sent: %v", msg) dc.logger.Printf("sent: %v", msg)
} }
dc.net.SetWriteDeadline(time.Now().Add(writeTimeout))
err = dc.irc.WriteMessage(msg) err = dc.irc.WriteMessage(msg)
case <-dc.closed: case <-dc.closed:
closed = true closed = true

View File

@ -13,6 +13,7 @@ import (
// TODO: make configurable // TODO: make configurable
var keepAlivePeriod = time.Minute var keepAlivePeriod = time.Minute
var retryConnectMinDelay = time.Minute var retryConnectMinDelay = time.Minute
var writeTimeout = 10 * time.Second
func setKeepAlive(c net.Conn) error { func setKeepAlive(c net.Conn) error {
tcpConn, ok := c.(*net.TCPConn) tcpConn, ok := c.(*net.TCPConn)

View File

@ -111,6 +111,8 @@ func connectToUpstream(network *network) (*upstreamConn, error) {
} }
go func() { go func() {
// TODO: any SendMessage call after the connection is closed will
// either block or drop
for { for {
var closed bool var closed bool
select { select {
@ -118,8 +120,10 @@ func connectToUpstream(network *network) (*upstreamConn, error) {
if uc.srv.Debug { if uc.srv.Debug {
uc.logger.Printf("sent: %v", msg) uc.logger.Printf("sent: %v", msg)
} }
uc.net.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := uc.irc.WriteMessage(msg); err != nil { if err := uc.irc.WriteMessage(msg); err != nil {
uc.logger.Printf("failed to write message: %v", err) uc.logger.Printf("failed to write message: %v", err)
closed = true
} }
case <-uc.closed: case <-uc.closed:
closed = true closed = true