Skip to content

Commit 1b8531a

Browse files
committed
fix(curated): pause curated communities loop on expensive networks
Part of #7614
1 parent 7d1eb05 commit 1b8531a

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

protocol/messenger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ type Messenger struct {
153153
historicSyncInFlight bool
154154
lastHistoricSyncRequestAt time.Time
155155

156+
connectionStateMutex sync.RWMutex
156157
connectionState connection.State
157158
contractMaker *contracts.ContractMaker
158159
verificationDatabase *verification.Persistence

protocol/messenger_curated_communities.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (m *Messenger) startCuratedCommunitiesUpdateLoop() {
4444
for {
4545
select {
4646
case <-time.After(interval):
47-
if m.isPaused() {
47+
if m.shouldPauseCuratedCommunitiesUpdateLoop() {
4848
interval = curatedCommunitiesUpdateInterval
4949
continue
5050
}
@@ -88,6 +88,13 @@ func (m *Messenger) startCuratedCommunitiesUpdateLoop() {
8888
}()
8989
}
9090

91+
func (m *Messenger) shouldPauseCuratedCommunitiesUpdateLoop() bool {
92+
// TODO when we implement back the setting for the user to select if they want to
93+
// fetch on expensive networks, use canSyncWithStoreNodes()
94+
// https://github.com/status-im/status-app/issues/18388
95+
return m.isPaused() || m.getConnectionState().IsExpensive()
96+
}
97+
9198
func (m *Messenger) getCuratedCommunitiesFromContract() (*communities.CuratedCommunities, error) {
9299
if m.contractMaker == nil {
93100
return nil, errors.New("contract maker not initialized")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package protocol
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/status-im/status-go/internal/connection"
9+
)
10+
11+
func TestShouldPauseCuratedCommunitiesUpdateLoop(t *testing.T) {
12+
m := &Messenger{}
13+
14+
require.False(t, m.shouldPauseCuratedCommunitiesUpdateLoop())
15+
16+
m.setConnectionState(connection.State{Expensive: true})
17+
require.True(t, m.shouldPauseCuratedCommunitiesUpdateLoop())
18+
}

protocol/messenger_mailserver.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,29 @@ func (m *Messenger) calculateGapForChat(chat *Chat, from uint32) (*common.Messag
658658
}
659659

660660
func (m *Messenger) canSyncWithStoreNodes() (bool, error) {
661-
if m.connectionState.IsExpensive() {
661+
if m.getConnectionState().IsExpensive() {
662662
return m.settings.CanSyncOnMobileNetwork()
663663
}
664664
return true, nil
665665
}
666666

667+
func (m *Messenger) getConnectionState() connection.State {
668+
m.connectionStateMutex.RLock()
669+
defer m.connectionStateMutex.RUnlock()
670+
671+
return m.connectionState
672+
}
673+
674+
func (m *Messenger) setConnectionState(state connection.State) {
675+
m.connectionStateMutex.Lock()
676+
defer m.connectionStateMutex.Unlock()
677+
678+
m.connectionState = state
679+
}
680+
667681
func (m *Messenger) ConnectionChanged(state connection.State) {
668682
m.messaging.ConnectionChanged(state)
669-
m.connectionState = state
683+
m.setConnectionState(state)
670684
}
671685

672686
func (m *Messenger) processMailserverBatch(peerInfo peer.AddrInfo, batch types2.StoreNodeBatch) error {

0 commit comments

Comments
 (0)