Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 6fc742f0d01cd218b1afb35a96da129d54c2bb0c Author: Duco van Amstel <duco.vanamstel@gmail.com> Date: Thu Nov 01 20:28:22 2018 +0000 Prevent Slack API rate-limit overflow (#539) diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index 8508d15..4abe79c 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -46 +48 @@ import (   "fmt"   "regexp"   "strings" + "sync" + "time"     "github.com/nlopes/slack"  ) @@ -577 +5925 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {   return nil, fmt.Errorf("%s: channel %s not found", b.Account, ID)  }   +const minimumRefreshInterval = 10 * time.Second + +var ( + refreshMutex sync.Mutex + refreshInProgress bool + earliestChannelRefresh = time.Now() + earliestUserRefresh = time.Now() +) +  func (b *Bslack) populateUsers() { + refreshMutex.Lock() + if time.Now().Before(earliestUserRefresh) || refreshInProgress { + b.Log.Debugf("Not refreshing user list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds())) + refreshMutex.Unlock() + return + } + refreshInProgress = true + refreshMutex.Unlock() +   users, err := b.sc.GetUsers()   if err != nil {   b.Log.Errorf("Could not reload users: %#v", err) @@ -749 +9421 @@ func (b *Bslack) populateUsers() {   b.usersMutex.Lock()   defer b.usersMutex.Unlock()   b.users = newUsers + + earliestUserRefresh = time.Now().Add(minimumRefreshInterval) + refreshInProgress = false  }    func (b *Bslack) populateChannels() { + refreshMutex.Lock() + if time.Now().Before(earliestChannelRefresh) || refreshInProgress { + b.Log.Debugf("Not refreshing channel list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds())) + refreshMutex.Unlock() + return + } + refreshInProgress = true + refreshMutex.Unlock() +   newChannelsByID := map[string]*slack.Channel{}   newChannelsByName := map[string]*slack.Channel{}   @@ -1066 +1389 @@ func (b *Bslack) populateChannels() {   defer b.channelsMutex.Unlock()   b.channelsByID = newChannelsByID   b.channelsByName = newChannelsByName + + earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) + refreshInProgress = false  }    var (