From 5189f86f81bf4b2e39fe948d20061d6aec0c36b6 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 23 Oct 2024 20:29:53 +0300 Subject: [PATCH] hicli: lower http header timeout after first sync --- cmd/gomuks/media.go | 5 +++++ pkg/hicli/hicli.go | 7 ++++--- pkg/hicli/sync.go | 7 ++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/gomuks/media.go b/cmd/gomuks/media.go index c1fffbd..858d974 100644 --- a/cmd/gomuks/media.go +++ b/cmd/gomuks/media.go @@ -226,6 +226,10 @@ func (gmx *Gomuks) DownloadMedia(w http.ResponseWriter, r *http.Request) { resp, err := gmx.Client.Client.Download(ctx, mxc) if err != nil { + if ctx.Err() != nil { + w.WriteHeader(499) + return + } log.Err(err).Msg("Failed to download media") var httpErr mautrix.HTTPError if cacheEntry == nil { @@ -236,6 +240,7 @@ func (gmx *Gomuks) DownloadMedia(w http.ResponseWriter, r *http.Request) { if cacheEntry.Error == nil { cacheEntry.Error = &database.MediaError{ ReceivedAt: jsontime.UnixMilliNow(), + Attempts: 1, } } else { cacheEntry.Error.Attempts++ diff --git a/pkg/hicli/hicli.go b/pkg/hicli/hicli.go index 293edd7..9549edc 100644 --- a/pkg/hicli/hicli.go +++ b/pkg/hicli/hicli.go @@ -94,11 +94,12 @@ func New(rawDB, cryptoDB *dbutil.Database, log zerolog.Logger, pickleKey []byte, Transport: &http.Transport{ DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext, TLSHandshakeTimeout: 10 * time.Second, - // This needs to be relatively high to allow initial syncs - ResponseHeaderTimeout: 180 * time.Second, + // This needs to be relatively high to allow initial syncs, + // it's lowered after the first sync in postProcessSyncResponse + ResponseHeaderTimeout: 300 * time.Second, ForceAttemptHTTP2: true, }, - Timeout: 180 * time.Second, + Timeout: 300 * time.Second, }, Syncer: (*hiSyncer)(c), Store: (*hiStore)(c), diff --git a/pkg/hicli/sync.go b/pkg/hicli/sync.go index b428c2e..3249f25 100644 --- a/pkg/hicli/sync.go +++ b/pkg/hicli/sync.go @@ -10,6 +10,7 @@ import ( "context" "errors" "fmt" + "net/http" "slices" "strings" "time" @@ -70,7 +71,11 @@ func (h *HiClient) postProcessSyncResponse(ctx context.Context, resp *mautrix.Re if syncCtx.shouldWakeupRequestQueue { h.WakeupRequestQueue() } - h.firstSyncReceived = true + if !h.firstSyncReceived { + h.firstSyncReceived = true + h.Client.Client.Transport.(*http.Transport).ResponseHeaderTimeout = 60 * time.Second + h.Client.Client.Timeout = 180 * time.Second + } if !syncCtx.evt.IsEmpty() { h.EventHandler(syncCtx.evt) }