From 1ee5dc062d5f2481123e9cd5c24afc2295fd2724 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 17 Aug 2022 15:43:50 +0200 Subject: [PATCH] upstream: add timeout for pending commands References: https://todo.sr.ht/~emersion/soju/194 --- upstream.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/upstream.go b/upstream.go index dd3afce..d731b3d 100644 --- a/upstream.go +++ b/upstream.go @@ -113,6 +113,7 @@ type upstreamBatch struct { type pendingUpstreamCommand struct { downstreamID uint64 msg *irc.Message + sentAt time.Time } type upstreamConn struct { @@ -367,8 +368,9 @@ func (uc *upstreamConn) sendNextPendingCommand(cmd string) { if len(uc.pendingCmds[cmd]) == 0 { return } - pendingCmd := uc.pendingCmds[cmd][0] + pendingCmd := &uc.pendingCmds[cmd][0] uc.SendMessageLabeled(context.TODO(), pendingCmd.downstreamID, pendingCmd.msg) + pendingCmd.sentAt = time.Now() } func (uc *upstreamConn) enqueueCommand(dc *downstreamConn, msg *irc.Message) { @@ -384,6 +386,12 @@ func (uc *upstreamConn) enqueueCommand(dc *downstreamConn, msg *irc.Message) { msg: msg, }) + // If we didn't get a reply after a while, just give up + // TODO: consider sending an abort reply to downstream + if t := uc.pendingCmds[msg.Command][0].sentAt; !t.IsZero() && time.Since(t) > 30*time.Second { + copy(uc.pendingCmds[msg.Command], uc.pendingCmds[msg.Command][1:]) + } + if len(uc.pendingCmds[msg.Command]) == 1 { uc.sendNextPendingCommand(msg.Command) }