diff options
author | David Hill <dhill@mindcry.org> | 2018-11-07 14:36:50 -0500 |
---|---|---|
committer | Wim <wim@42.be> | 2018-11-07 20:36:50 +0100 |
commit | 0e2522279e8671a345c1be6b8b07c0f1a505e23e (patch) | |
tree | 968ac4b1acd0352ae9962d6f71bfd7d9c668596f /bridge/slack/helpers.go | |
parent | 141a42a75b47b0a05f5b360ff5711819d3727a75 (diff) | |
download | matterbridge-msglm-0e2522279e8671a345c1be6b8b07c0f1a505e23e.tar.gz matterbridge-msglm-0e2522279e8671a345c1be6b8b07c0f1a505e23e.tar.bz2 matterbridge-msglm-0e2522279e8671a345c1be6b8b07c0f1a505e23e.zip |
Clean up various stuff (#508)
* various cleanups
Diffstat (limited to 'bridge/slack/helpers.go')
-rw-r--r-- | bridge/slack/helpers.go | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index 4abe79cd..23d5816a 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -4,7 +4,6 @@ import ( "fmt" "regexp" "strings" - "sync" "time" "github.com/nlopes/slack" @@ -61,22 +60,17 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) { 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() + b.refreshMutex.Lock() + if time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress { + b.Log.Debugf("Not refreshing user list as it was done less than %v ago.", + minimumRefreshInterval) + b.refreshMutex.Unlock() + return } - refreshInProgress = true - refreshMutex.Unlock() + b.refreshInProgress = true + b.refreshMutex.Unlock() users, err := b.sc.GetUsers() if err != nil { @@ -95,19 +89,22 @@ func (b *Bslack) populateUsers() { defer b.usersMutex.Unlock() b.users = newUsers - earliestUserRefresh = time.Now().Add(minimumRefreshInterval) - refreshInProgress = false + b.refreshMutex.Lock() + defer b.refreshMutex.Unlock() + b.earliestUserRefresh = time.Now().Add(minimumRefreshInterval) + b.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() + b.refreshMutex.Lock() + if time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress { + b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.", + minimumRefreshInterval) + b.refreshMutex.Unlock() return } - refreshInProgress = true - refreshMutex.Unlock() + b.refreshInProgress = true + b.refreshMutex.Unlock() newChannelsByID := map[string]*slack.Channel{} newChannelsByName := map[string]*slack.Channel{} @@ -139,8 +136,10 @@ func (b *Bslack) populateChannels() { b.channelsByID = newChannelsByID b.channelsByName = newChannelsByName - earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) - refreshInProgress = false + b.refreshMutex.Lock() + defer b.refreshMutex.Unlock() + b.earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) + b.refreshInProgress = false } var ( |