From e6ccdb83b79168a1e762f6ae1bbb34993a5b2b0f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 18 Nov 2020 13:29:47 +0200 Subject: [PATCH] Revert portal backfill lock changes and update mautrix-go --- go.mod | 2 +- go.sum | 22 ++-------------------- portal.go | 56 ++++++++++++++++++++----------------------------------- 3 files changed, 23 insertions(+), 57 deletions(-) diff --git a/go.mod b/go.mod index 709990b..7025548 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( gopkg.in/yaml.v2 v2.3.0 maunium.net/go/mauflag v1.0.0 maunium.net/go/maulogger/v2 v2.1.1 - maunium.net/go/mautrix v0.8.0-rc.2 + maunium.net/go/mautrix v0.8.0-rc.3 ) replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.3.13 diff --git a/go.sum b/go.sum index e51ae93..a1cc0c7 100644 --- a/go.sum +++ b/go.sum @@ -127,19 +127,12 @@ github.com/tidwall/pretty v1.0.1 h1:WE4RBSZ1x6McVVC8S/Md+Qse8YUv6HRObAx6ke00NY8= github.com/tidwall/pretty v1.0.1/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/sjson v1.1.1 h1:7h1vk049Jnd5EH9NyzNiEuwYW4b5qgreBbqRC19AS3U= github.com/tidwall/sjson v1.1.1/go.mod h1:yvVuSnpEQv5cYIrO+AT6kw4QVfd5SDZoGIS7/5+fZFs= -github.com/tulir/go-whatsapp v0.3.10 h1:LfzRj6V3dshKemad3HZsgYgKAZtaT0wNITmobJYAbro= -github.com/tulir/go-whatsapp v0.3.10/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o= -github.com/tulir/go-whatsapp v0.3.11 h1:z9AvTb8YXYP8t6Y0jium3reshgDpFCg//WNiCGo9esQ= -github.com/tulir/go-whatsapp v0.3.11/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o= -github.com/tulir/go-whatsapp v0.3.12 h1:4ovBn1GNMUsCrFR7KdAcByQnkh0o+VoA7Q/wY2UD36A= -github.com/tulir/go-whatsapp v0.3.12/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o= github.com/tulir/go-whatsapp v0.3.13 h1:RPc/GdZ7KlhlGiZp2Zk7B/OP9v0l7ywOt5I2kKAZ+xU= github.com/tulir/go-whatsapp v0.3.13/go.mod h1:U5+sm33vrv3wz62YyRM/VS7q2ObXkxU4Xqj/3KOmN9o= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM= golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E= @@ -159,8 +152,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM= -golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201026091529-146b70c837a4 h1:awiuzyrRjJDb+OXi9ceHO3SDxVoN3JER57mhtqkdQBs= golang.org/x/net v0.0.0-20201026091529-146b70c837a4/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -178,7 +169,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -226,13 +216,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= maunium.net/go/maulogger/v2 v2.1.1 h1:NAZNc6XUFJzgzfewCzVoGkxNAsblLCSSEdtDuIjP0XA= maunium.net/go/maulogger/v2 v2.1.1/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= -maunium.net/go/mautrix v0.7.11 h1:3MdKRs8Dt1H8PvKH/6ES1AdooJngBVEGAwLLeVTnouk= -maunium.net/go/mautrix v0.7.11/go.mod h1:FpsAvwNdG3Zeup7Y2Nlv81Lk0h6iVRPoIy6D7g/7YCE= -maunium.net/go/mautrix v0.7.12 h1:kJN5ErlzGAQdNMv58Rv4GATkLlb4OJ3l0IOwxFovlVc= -maunium.net/go/mautrix v0.7.12/go.mod h1:Jn0ijwXwMFvJFIN9IljirIVKpZQbZP/Dk7pdX2qDmXk= -maunium.net/go/mautrix v0.7.13 h1:qfnvLxvQafvLgHbdZF/+9qs9gyArYf8fUnzfQbjgQaU= -maunium.net/go/mautrix v0.7.13/go.mod h1:Jn0ijwXwMFvJFIN9IljirIVKpZQbZP/Dk7pdX2qDmXk= -maunium.net/go/mautrix v0.8.0-rc.1 h1:tVrs5GWCToJhHbIQ3PvSFpL/u1aeTABD2ulHyifiH70= -maunium.net/go/mautrix v0.8.0-rc.1/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY= -maunium.net/go/mautrix v0.8.0-rc.2 h1:H1OieKdfTr4+i3lGgvydl+3z21CM4XgHzTrW8ozmsb8= -maunium.net/go/mautrix v0.8.0-rc.2/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY= +maunium.net/go/mautrix v0.8.0-rc.3 h1:bb18oNxHUmeiJ0V63YTRVGMjgoeLwu+G40l4n42Z5GI= +maunium.net/go/mautrix v0.8.0-rc.3/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY= diff --git a/portal.go b/portal.go index 3b225f9..f092212 100644 --- a/portal.go +++ b/portal.go @@ -133,8 +133,7 @@ func (bridge *Bridge) NewManualPortal(key database.PortalKey) *Portal { recentlyHandled: [recentlyHandledLength]types.WhatsAppMessageID{}, - messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer), - backfillStart: make(chan struct{}, 1), + messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer), } portal.Key = key go portal.handleMessageLoop() @@ -149,8 +148,7 @@ func (bridge *Bridge) NewPortal(dbPortal *database.Portal) *Portal { recentlyHandled: [recentlyHandledLength]types.WhatsAppMessageID{}, - messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer), - backfillStart: make(chan struct{}, 1), + messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer), } go portal.handleMessageLoop() return portal @@ -177,8 +175,7 @@ type Portal struct { recentlyHandledLock sync.Mutex recentlyHandledIndex uint8 - backfillStart chan struct{} - backfillWait sync.WaitGroup + backfillLock sync.Mutex backfilling bool lastMessageTs uint64 @@ -193,34 +190,25 @@ type Portal struct { const MaxMessageAgeToCreatePortal = 5 * 60 // 5 minutes func (portal *Portal) handleMessageLoop() { - for { - select { - case msg := <-portal.messages: - portal.handleNewMessage(msg) - case <-portal.backfillStart: - portal.log.Debugln("Processing of incoming messages is locked for backfilling") - portal.backfillWait.Wait() - portal.log.Debugln("Processing of incoming messages unlocked after backfilling") + for msg := range portal.messages { + if len(portal.MXID) == 0 { + if msg.timestamp+MaxMessageAgeToCreatePortal < uint64(time.Now().Unix()) { + portal.log.Debugln("Not creating portal room for incoming message as the message is too old.") + continue + } + portal.log.Debugln("Creating Matrix room from incoming message") + err := portal.CreateMatrixRoom(msg.source) + if err != nil { + portal.log.Errorln("Failed to create portal room:", err) + return + } } + portal.backfillLock.Lock() + portal.handleMessage(msg) + portal.backfillLock.Unlock() } } -func (portal *Portal) handleNewMessage(msg PortalMessage) { - if len(portal.MXID) == 0 { - if msg.timestamp+MaxMessageAgeToCreatePortal < uint64(time.Now().Unix()) { - portal.log.Debugln("Not creating portal room for incoming message as the message is too old.") - return - } - portal.log.Debugln("Creating Matrix room from incoming message") - err := portal.CreateMatrixRoom(msg.source) - if err != nil { - portal.log.Errorln("Failed to create portal room:", err) - return - } - } - portal.handleMessage(msg) -} - func (portal *Portal) handleMessage(msg PortalMessage) { if len(portal.MXID) == 0 { portal.log.Warnln("handleMessage called even though portal.MXID is empty") @@ -746,11 +734,7 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error } func (portal *Portal) beginBackfill() func() { - portal.backfillWait.Add(1) - select { - case portal.backfillStart <- struct{}{}: - default: - } + portal.backfillLock.Lock() portal.backfilling = true var privateChatPuppetInvited bool var privateChatPuppet *Puppet @@ -768,7 +752,7 @@ func (portal *Portal) beginBackfill() func() { return func() { portal.backfilling = false portal.privateChatBackfillInvitePuppet = nil - portal.backfillWait.Done() + portal.backfillLock.Unlock() if privateChatPuppet != nil && privateChatPuppetInvited { _, _ = privateChatPuppet.DefaultIntent().LeaveRoom(portal.MXID) }