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

Conversation

seratch
Copy link
Member

@seratch seratch commented Apr 1, 2020

Summary

This pull request fixes #586 by changing the internals of SlackClient a bit.

The latest RTM event payloads from Slack has both user and bot_id even for events generated by a bot user as below:

{
  "bot_id": "B12345678",
  "suppress_notification": false,
  "type": "message",
  "text": "Badgers? BADGERS? WE DON'T NEED NO STINKIN BADGERS",
  "user": "U12345678",
  "team": "T12345678",
  "bot_profile": {
    "id": "B12345678",
    "deleted": false,
    "name": "hubot",
    "updated": 1568973776,
    "app_id": "A12345678",
    "icons": {
      "image_36": "https://a.slack-edge.com/80588/img/services/hubot_36.png",
      "image_48": "https://a.slack-edge.com/80588/img/services/hubot_48.png",
      "image_72": "https://a.slack-edge.com/80588/img/services/hubot_72.png"
    },
    "team_id": "T12345678"
  },
  "source_team": "T12345678",
  "user_team": "T12345678",
  "channel": "C12345678",
  "event_ts": "1585723843.003300",
  "ts": "1585723843.003300"
}

In such cases, SlackClient's @botUserIdMap (the internal memory cache of bot user information) is not updated. That's the reason why the cache doesn't work as expected and a large number of bots.info calls may occur when receiving bot user events.

Requirements (place an x in each [ ])

@seratch seratch added the bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented label Apr 1, 2020
@seratch seratch self-assigned this Apr 1, 2020
@codecov
Copy link

codecov bot commented Apr 1, 2020

Codecov Report

Merging #588 into master will decrease coverage by 0.10%.
The diff coverage is 71.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #588      +/-   ##
==========================================
- Coverage   85.03%   84.93%   -0.11%     
==========================================
  Files           6        6              
  Lines         381      385       +4     
  Branches       85       85              
==========================================
+ Hits          324      327       +3     
- Misses         33       34       +1     
  Partials       24       24              
Impacted Files Coverage Δ
src/client.coffee 91.15% <71.42%> (-0.60%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 933bafc...f6b3e7f. Read the comment docs.

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. 👍

@mistydemeo
Copy link
Contributor

Let me know when you'd like me to test this!

@seratch seratch merged commit 3767644 into slackapi:master Apr 3, 2020
@seratch seratch deleted the issue-586 branch April 3, 2020 09:29
@seratch seratch mentioned this pull request Apr 3, 2020
2 tasks
@seratch
Copy link
Member Author

seratch commented Apr 6, 2020

@mistydemeo Thanks a lot for your help on this! I just released version 4.7.2. Could you try it out when you have a chance?

@mistydemeo
Copy link
Contributor

I've deployed 4.7.2, and it's behaving normally. We deployed at 11:15 PDT; can you please take a look at our API usage and see if it's reduced since then?

@seratch
Copy link
Member Author

seratch commented Apr 8, 2020

@mistydemeo Thank you! After your deployment, we're seeing the significant decrease in bots.info API calls in data. In the case where you need more help on related matters, I'll be closely working with the support team!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hubot-slack calling bots.info a suspicious number of times
4 participants