-
Notifications
You must be signed in to change notification settings - Fork 640
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
WIP: Bot: support bubbling up rate limit info #573
Open
mistydemeo
wants to merge
3
commits into
slackapi:main
Choose a base branch
from
mistydemeo:bubble_up_rate_limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+16
−2
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
SlackClient = require "./client" | ||
pkg = require "../package" | ||
|
||
Events = require("@slack/client").CLIENT_EVENTS | ||
|
||
class SlackBot extends Adapter | ||
|
||
###* | ||
|
@@ -39,6 +41,7 @@ class SlackBot extends Adapter | |
@client.rtm.on "close", @close | ||
@client.rtm.on "disconnect", @disconnect | ||
@client.rtm.on "error", @error | ||
@client.web.on Events.WEB.RATE_LIMITED, @rate_limited | ||
@client.rtm.on "authenticated", @authenticated | ||
@client.onEvent @eventHandler | ||
|
||
|
@@ -206,9 +209,20 @@ class SlackBot extends Adapter | |
error: (error) => | ||
@robot.logger.error "Slack RTM error: #{JSON.stringify error}" | ||
# Assume that scripts can handle slowing themselves down, all other errors are bubbled up through Hubot | ||
# NOTE: should rate limit errors also bubble up? | ||
if error.code isnt -1 | ||
@robot.emit "error", error | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find any official documentation of the rate-limiting code, but this test indicates that code -1 is reliably rate limiting: https://github.com/slackapi/hubot-slack/blob/master/test/bot.coffee#L168-L171 |
||
# The actual duration of the rate limit isn't exposed. :( | ||
@rate_limited(-1) | ||
|
||
###* | ||
# Bubbles up rate-limit info, allowing the bot to react | ||
# | ||
# @private | ||
# @param {number seconds} | ||
### | ||
rate_limited: (seconds) -> | ||
@robot.emit "slack-rate-limit", seconds | ||
|
||
###* | ||
# Incoming Slack event handler | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mistydemeo Here is a sample I used for verifying this implementation.
Does this really work for you? It doesn't work for me. The following is the error I get every time the
rate_limited
method is called. I haven't investigated details yet but it seems@robot
is undefined in any case.Also, to utilize this, you need to always use
robot.adapter.client.web
. If some of your scripts initializeWebClient
separately (also, that's the way this adapter currently recommends), rate-limited errors those clients get won't be notified here.Correct me if my understanding is wrong.