From c35ceff06e5cfb3b07e743614e0d87c188a5c193 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 27 Jul 2021 13:52:10 +0200 Subject: [PATCH] Set timeout on mailserver requests --- protocol/messenger_mailserver.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/protocol/messenger_mailserver.go b/protocol/messenger_mailserver.go index aced9ed3d..8b4897de3 100644 --- a/protocol/messenger_mailserver.go +++ b/protocol/messenger_mailserver.go @@ -3,6 +3,7 @@ package protocol import ( "context" "fmt" + "time" "github.com/pkg/errors" "go.uber.org/zap" @@ -18,6 +19,7 @@ import ( // tolerance is how many seconds of potentially out-of-order messages we want to fetch var tolerance uint32 = 60 +var mailserverRequestTimeout = 45 * time.Second func (m *Messenger) shouldSync() (bool, error) { if m.mailserver == nil || !m.online() { @@ -349,14 +351,19 @@ func (m *Messenger) calculateGapForChat(chat *Chat, from uint32) (*common.Messag func (m *Messenger) processMailserverBatch(batch MailserverBatch) error { m.logger.Info("syncing topic", zap.Any("topic", batch.Topics), zap.Int64("from", int64(batch.From)), zap.Int64("to", int64(batch.To))) - cursor, storeCursor, err := m.transport.SendMessagesRequestForTopics(context.Background(), m.mailserver, batch.From, batch.To, nil, nil, batch.Topics, true) + ctx, cancel := context.WithTimeout(context.Background(), mailserverRequestTimeout) + defer cancel() + + cursor, storeCursor, err := m.transport.SendMessagesRequestForTopics(ctx, m.mailserver, batch.From, batch.To, nil, nil, batch.Topics, true) if err != nil { return err } for len(cursor) != 0 || storeCursor != nil { m.logger.Info("retrieved cursor", zap.Any("cursor", cursor)) + ctx, cancel := context.WithTimeout(context.Background(), mailserverRequestTimeout) + defer cancel() - cursor, storeCursor, err = m.transport.SendMessagesRequest(context.Background(), m.mailserver, batch.From, batch.To, cursor, storeCursor, true) + cursor, storeCursor, err = m.transport.SendMessagesRequest(ctx, m.mailserver, batch.From, batch.To, cursor, storeCursor, true) if err != nil { return err }