fix(communities)_: ensure `ScheduleMembersReevaluation` is non-blocking (#5176)

This commit is contained in:
osmaczko 2024-05-17 15:06:33 +02:00 committed by GitHub
parent c3ab8bd658
commit 948e09af03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 9 deletions

View File

@ -1165,6 +1165,23 @@ func (m *Manager) reevaluateMembersLoop(communityID types.HexBytes, reevaluateOn
error error
} }
shouldReevaluate := func(task *membersReevaluationTask, force bool) bool {
task.mutex.Lock()
defer task.mutex.Unlock()
// Ensure reevaluation is performed not more often than once per minute
if !force && task.lastSuccessTime.After(time.Now().Add(-1*time.Minute)) {
return false
}
if !task.lastSuccessTime.Before(time.Now().Add(-memberPermissionsCheckInterval)) &&
!task.lastSuccessTime.Before(task.onDemandRequestTime) {
return false
}
return true
}
reevaluateMembers := func(force bool) (err error) { reevaluateMembers := func(force bool) (err error) {
t, exists := m.membersReevaluationTasks.Load(communityID.String()) t, exists := m.membersReevaluationTasks.Load(communityID.String())
if !exists { if !exists {
@ -1178,16 +1195,8 @@ func (m *Manager) reevaluateMembersLoop(communityID types.HexBytes, reevaluateOn
error: errors.New("invalid task type"), error: errors.New("invalid task type"),
} }
} }
task.mutex.Lock()
defer task.mutex.Unlock()
// Ensure reevaluation is performed not more often than once per minute if !shouldReevaluate(task, force) {
if !force && task.lastSuccessTime.After(time.Now().Add(-1*time.Minute)) {
return nil
}
if !task.lastSuccessTime.Before(time.Now().Add(-memberPermissionsCheckInterval)) &&
!task.lastSuccessTime.Before(task.onDemandRequestTime) {
return nil return nil
} }
@ -1201,7 +1210,10 @@ func (m *Manager) reevaluateMembersLoop(communityID types.HexBytes, reevaluateOn
return err return err
} }
task.mutex.Lock()
defer task.mutex.Unlock()
task.lastSuccessTime = time.Now() task.lastSuccessTime = time.Now()
return nil return nil
} }