From b5c82adb70204fd735a1620f7713b52ce9b45df2 Mon Sep 17 00:00:00 2001 From: Volodymyr Kozieiev Date: Wed, 8 Nov 2023 13:06:40 +0000 Subject: [PATCH] Increased timeouts when fetching curated communities (#4244) * Timeouts increased --- protocol/messenger_communities.go | 70 +++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 4aafca232..edd76237d 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -562,33 +562,65 @@ func (m *Messenger) SpectatedCommunities() ([]*communities.Community, error) { return m.communitiesManager.Spectated() } +const ( + fetchError int = 0 + fetchSuccess int = 1 + fetchHasUnknowns int = 2 +) + +func calcTimeTillNextUpdate(fetchResultsHistory []int) time.Duration { + // TODO lower this back again once the real curated community contract is up + // The current contract contains communities that are no longer accessible on waku + const shortTimeout = 30 * time.Second + const averageTimeout = 60 * time.Second + const longTimeout = 300 * time.Second + + twoConsecutiveErrors := (len(fetchResultsHistory) == 2 && + fetchResultsHistory[0] == fetchError && + fetchResultsHistory[1] == fetchError) + + twoConsecutiveHasUnknowns := (len(fetchResultsHistory) == 2 && + fetchResultsHistory[0] == fetchHasUnknowns && + fetchResultsHistory[1] == fetchHasUnknowns) + + var timeTillNextUpdate time.Duration + + if twoConsecutiveErrors || twoConsecutiveHasUnknowns { + timeTillNextUpdate = longTimeout + } else { + switch fetchResultsHistory[len(fetchResultsHistory)-1] { + case fetchError: + timeTillNextUpdate = shortTimeout + case fetchSuccess: + timeTillNextUpdate = longTimeout + case fetchHasUnknowns: + timeTillNextUpdate = averageTimeout + } + } + return timeTillNextUpdate +} + // Regularly gets list of curated communities and signals them to client func (m *Messenger) startCuratedCommunitiesUpdateLoop() { logger := m.logger.Named("startCuratedCommunitiesUpdateLoop") - const errorTimeout = 10 * time.Second - const successTimeout = 120 * time.Second - // TODO lower this back again once the real curated community contract is up - // The current contract contains communities that are no longer accessible on waku - const unknownCommunitiesFoundTimeout = 60 * time.Second - type curatedCommunities struct { ContractCommunities []string ContractFeaturedCommunities []string UnknownCommunities []string } - var mu = sync.RWMutex{} - var c = curatedCommunities{} - go func() { - for { - var timeTillNextUpdate time.Duration + var fetchResultsHistory = make([]int, 0) + var mu = sync.RWMutex{} + var c = curatedCommunities{} + + for { response, err := m.CuratedCommunities() if err != nil { - timeTillNextUpdate = errorTimeout + fetchResultsHistory = append(fetchResultsHistory, fetchError) } else { mu.Lock() // Check if it's the same values we had @@ -606,15 +638,19 @@ func (m *Messenger) startCuratedCommunitiesUpdateLoop() { mu.Unlock() if len(response.UnknownCommunities) == 0 { - //next update shouldn't happen soon - timeTillNextUpdate = successTimeout + fetchResultsHistory = append(fetchResultsHistory, fetchSuccess) + } else { - //unknown communities already requested from mailserver, so we wait just a bit before - //next attempt to get their info - timeTillNextUpdate = unknownCommunitiesFoundTimeout + fetchResultsHistory = append(fetchResultsHistory, fetchHasUnknowns) } } + //keep only 2 last fetch results + if len(fetchResultsHistory) > 2 { + fetchResultsHistory = fetchResultsHistory[1:] + } + + timeTillNextUpdate := calcTimeTillNextUpdate(fetchResultsHistory) logger.Debug("Next curated communities update will happen in", zap.Duration("timeTillNextUpdate", timeTillNextUpdate)) select {