4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-07 05:24:03 +00:00

Clean up various stuff (#508)

* various cleanups
This commit is contained in:
David Hill
2018-11-07 14:36:50 -05:00
committed by Wim
parent 141a42a75b
commit 0e2522279e
11 changed files with 77 additions and 54 deletions

View File

@ -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 (