Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #586 hubot-slack calling bots.info a suspicious number of times #588

Merged
merged 3 commits into from
Apr 3, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class SlackClient
return Promise.resolve(@botUserIdMap[botId]) if @botUserIdMap[botId]?

# Bot user is not in mapping - call bots.info
@robot.logger.debug "SlackClient#fetchBotUser() Calling bots.info API for bot_id: #{botId}"
@web.bots.info(bot: botId).then((r) => r.bot)

###*
Expand Down Expand Up @@ -331,9 +332,13 @@ class SlackClient
if @eventHandler
# fetch full representations of the user, bot, and potentially the item_user.
fetches = {}
fetches.user = @fetchUser event.user if event.user
fetches.bot = @fetchBotUser event.bot_id if event.bot_id
fetches.item_user = @fetchUser event.item_user if event.item_user
if event.bot_id
fetches.bot = @fetchBotUser event.bot_id
else if event.user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this change, when the event has a bot_id we will not make a call to Web API method users.info, and therefore there won't be a value for fetched.user later.

this seems safe because there's no place in the branch of code where fetched.bot is assigned, that the value of fetched.user is used. 👍

but i am wondering, how will this reduce the number of calls to bots.info?

Copy link
Member Author

@seratch seratch Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aoberoi

but i am wondering, how will this reduce the number of calls to bots.info?

The root cause of the increase of bots.info calls is lack of the timing to run the following lines with the latest payloads.

The combination of the latest payloads and current SlackClient implementation keeps the @botUserIdMap empty in any case. So, fetchBotUser method calls bots.info API every time receiving an event with both bot_id and user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh i see! So now that event.user is in every bot message, fetched.user always has a value, and then botUserIdMap is never updated. This change makes it so fetched.user will not have a value when both event.bot and event.user are present. 👍

fetches.user = @fetchUser event.user

if event.item_user
fetches.item_user = @fetchUser event.item_user

# after fetches complete...
Promise.props(fetches)
Expand Down