From e3340d61ec8d07a33f6391414367563265f9296f Mon Sep 17 00:00:00 2001 From: Joonas Myhrberg Date: Wed, 2 Aug 2023 21:57:54 +0300 Subject: [PATCH] Add global backfill lock Ensures only a single channel is backfilled at once. --- backfill.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backfill.go b/backfill.go index 6e0f0a1..12025fc 100644 --- a/backfill.go +++ b/backfill.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "fmt" "sort" + "sync" "github.com/bwmarrin/discordgo" "github.com/rs/zerolog" @@ -16,6 +17,8 @@ import ( "go.mau.fi/mautrix-discord/database" ) +var globalBackfillLock sync.Mutex + func (portal *Portal) forwardBackfillInitial(source *User, thread *Thread) { log := portal.log defer func() { @@ -27,6 +30,9 @@ func (portal *Portal) forwardBackfillInitial(source *User, thread *Thread) { panic("forwardBackfillInitial() called without locking forwardBackfillLock") } + globalBackfillLock.Lock() + defer globalBackfillLock.Unlock() + limit := portal.bridge.Config.Bridge.Backfill.Limits.Initial.Channel if portal.GuildID == "" { limit = portal.bridge.Config.Bridge.Backfill.Limits.Initial.DM @@ -78,6 +84,9 @@ func (portal *Portal) ForwardBackfillMissed(source *User, serverLastMessageID st portal.forwardBackfillLock.Lock() defer portal.forwardBackfillLock.Unlock() + globalBackfillLock.Lock() + defer globalBackfillLock.Unlock() + var lastMessage *database.Message if thread != nil { lastMessage = portal.bridge.DB.Message.GetLastInThread(portal.Key, thread.ID)