Thumbnail

rani/matterbridge.git

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

commit 1dbb23169f53410536080b8b8fb49253968ba64a Author: Wim <wim@42.be> Date: Wed Jan 30 23:26:36 2019 +0000 Optimize handling of very large slack teams. Fixes #695 Stop getting users if we reach 2000 users. Slack will rate-limit us even if we follow their limits. This means that we now have to lookup every user that says a message for the first time. This should be less intensive on the API. This also disables partly f31ba861996fa81ee3e9449549d4b92bb1b3778d for now. ChannelMembers will not be filled. diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index 5e601ea..e013b83 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -758 +756 @@ func (b *Bslack) handleSlackClient(messages chan *config.Message) {   // When we join a channel we update the full list of users as   // well as the information for the channel that we joined as this   // should now tell that we are a member of it. - b.populateUsers(false) -   b.channelsMutex.Lock()   b.channelsByID[ev.Channel.ID] = &ev.Channel   b.channelsByName[ev.Channel.Name] = &ev.Channel @@ -2027 +2006 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er  func (b *Bslack) handleStatusEvent(ev *slack.MessageEvent, rmsg *config.Message) bool {   switch ev.SubType {   case sChannelJoined, sMemberJoined: - b.populateUsers(false)   // There's no further processing needed on channel events   // so we return 'true'.   return true diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index d84353f..9959d4f 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -126 +1213 @@ import (  )    func (b *Bslack) getUser(id string) *slack.User { + b.usersMutex.RLock() + user, ok := b.users[id] + b.usersMutex.RUnlock() + if ok { + return user + } + b.populateUser(id)   b.usersMutex.RLock()   defer b.usersMutex.RUnlock()   @@ -1029 +10911 @@ func (b *Bslack) populateUsers(wait bool) {     newUsers := map[string]*slack.User{}   pagination := b.sc.GetUsersPaginated(slack.GetUsersOptionLimit(200)) + count := 0   for {   var err error   pagination, err = pagination.Next(context.Background()) + time.Sleep(time.Second)   if err != nil {   if pagination.Done(err) {   break @@ -1206 +12913 @@ func (b *Bslack) populateUsers(wait bool) {   for i := range pagination.Users {   newUsers[pagination.Users[i].ID] = &pagination.Users[i]   } + b.Log.Debugf("getting %d users", len(pagination.Users)) + count++ + // more > 2000 users, slack will complain and ratelimit. break + if count > 10 { + b.Log.Info("Large slack detected > 2000 users, skipping loading complete userlist.") + break + }   }     b.usersMutex.Lock() @@ -17215 +18818 @@ func (b *Bslack) populateChannels(wait bool) {   newChannelsByID[channels[i].ID] = &channels[i]   newChannelsByName[channels[i].Name] = &channels[i]   // also find all the members in every channel - members, err := b.getUsersInConversation(channels[i].ID) - if err != nil { - if err = b.handleRateLimit(err); err != nil { - b.Log.Errorf("Could not retrieve channel members: %#v", err) - return + // comment for now, issues on big slacks + /* + members, err := b.getUsersInConversation(channels[i].ID) + if err != nil { + if err = b.handleRateLimit(err); err != nil { + b.Log.Errorf("Could not retrieve channel members: %#v", err) + return + } + continue   } - continue - } - newChannelMembers[channels[i].ID] = members + newChannelMembers[channels[i].ID] = members + */   }     if nextCursor == "" {