Skip to content

Commit

Permalink
chore: resume archives import with delay
Browse files Browse the repository at this point in the history
  • Loading branch information
osmaczko committed Jun 12, 2023
1 parent 1a2ca21 commit 334546f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
return nil, err
}
}
m.enableHistoryArchivesImportAfterDelay()

if m.httpServer != nil {
err = m.httpServer.Start()
Expand Down
16 changes: 16 additions & 0 deletions protocol/messenger_communities.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ var updateActiveMembersInterval = 24 * time.Hour
const discordTimestampLayout = "2006-01-02T15:04:05+00:00"

var importRateLimiter = rate.NewLimiter(rate.Every(importSlowRate), 1)
var importDelayer = make(chan struct{})

const (
importSlowRate = time.Second / 1
importFastRate = time.Second / 100
importMessagesChunkSize = 10
importInitialDelay = time.Minute * 5
)

const (
Expand Down Expand Up @@ -2334,6 +2336,13 @@ func (m *Messenger) InitHistoryArchiveTasks(communities []*communities.Community
}
}

func (m *Messenger) enableHistoryArchivesImportAfterDelay() {
go func() {
time.Sleep(importInitialDelay)
close(importDelayer)
}()
}

func (m *Messenger) resumeHistoryArchivesImport(communityID types.HexBytes) error {
archiveIDsToImport, err := m.communitiesManager.GetMessageArchiveIDsToImport(communityID)
if err != nil {
Expand Down Expand Up @@ -2391,6 +2400,13 @@ func (m *Messenger) importHistoryArchives(communityID types.HexBytes, cancel cha
cancelFunc()
}()

// don't proceed until initial import delay has passed
select {
case <-importDelayer:
case <-ctx.Done():
return nil
}

importMessageArchivesLoop:
for {
select {
Expand Down

0 comments on commit 334546f

Please sign in to comment.