diff options
author | Duco van Amstel <duco.vanamstel@gmail.com> | 2018-11-01 20:28:22 +0000 |
---|---|---|
committer | Wim <wim@42.be> | 2018-11-01 21:28:22 +0100 |
commit | 1269be1d0472430d51b821639fb7d35e23f90887 (patch) | |
tree | c7cadb5107a13f5169be9145f4d8df2ba3cad571 /bridge/slack/helpers.go | |
parent | 3b8837a16bdfca5085f37867d08571a03a66f8e9 (diff) | |
download | matterbridge-msglm-1269be1d0472430d51b821639fb7d35e23f90887.tar.gz matterbridge-msglm-1269be1d0472430d51b821639fb7d35e23f90887.tar.bz2 matterbridge-msglm-1269be1d0472430d51b821639fb7d35e23f90887.zip |
Prevent Slack API rate-limit overflow (#539)
Diffstat (limited to 'bridge/slack/helpers.go')
-rw-r--r-- | bridge/slack/helpers.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index 8508d152..4abe79cd 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -4,6 +4,8 @@ import ( "fmt" "regexp" "strings" + "sync" + "time" "github.com/nlopes/slack" ) @@ -57,7 +59,25 @@ 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) @@ -74,9 +94,21 @@ 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{} @@ -106,6 +138,9 @@ func (b *Bslack) populateChannels() { defer b.channelsMutex.Unlock() b.channelsByID = newChannelsByID b.channelsByName = newChannelsByName + + earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) + refreshInProgress = false } var ( |