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 slack pagination #306

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions backend/danswer/connectors/slack/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

def _make_paginated_slack_api_call(
call: Callable[..., SlackResponse], **kwargs: Any
) -> list[dict[str, Any]]:
) -> Generator[dict[str, Any], None, None]:
return make_slack_api_call_paginated(
make_slack_api_rate_limited(make_slack_api_call_logged(call))
)(**kwargs)
Expand All @@ -50,9 +50,9 @@ def _make_slack_api_call(

def get_channel_info(client: WebClient, channel_id: str) -> ChannelType:
"""Get information about a channel. Needed to convert channel ID to channel name"""
return _make_paginated_slack_api_call(
client.conversations_info, channel=channel_id
)[0]["channel"]
return _make_slack_api_call(client.conversations_info, channel=channel_id)[0][
"channel"
]


def get_channels(
Expand Down
20 changes: 12 additions & 8 deletions backend/danswer/connectors/slack/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import time
from collections.abc import Callable
from collections.abc import Generator
from functools import wraps
from typing import Any
from typing import cast
Expand Down Expand Up @@ -45,20 +46,20 @@ def logged_call(**kwargs: Any) -> SlackResponse:

def make_slack_api_call_paginated(
call: Callable[..., SlackResponse],
) -> Callable[..., list[dict[str, Any]]]:
) -> Callable[..., Generator[dict[str, Any], None, None]]:
"""Wraps calls to slack API so that they automatically handle pagination"""

@wraps(call)
def paginated_call(**kwargs: Any) -> list[dict[str, Any]]:
results: list[dict[str, Any]] = []
def paginated_call(**kwargs: Any) -> Generator[dict[str, Any], None, None]:
cursor: str | None = None
has_more = True
while has_more:
for result in call(cursor=cursor, limit=_SLACK_LIMIT, **kwargs):
has_more = result.get("has_more", False)
cursor = result.get("response_metadata", {}).get("next_cursor", "")
results.append(cast(dict[str, Any], result))
return results
response = call(cursor=cursor, limit=_SLACK_LIMIT, **kwargs)
yield cast(dict[str, Any], response.validate())
cursor = cast(dict[str, Any], response.get("response_metadata", {})).get(
"next_cursor", ""
)
has_more = bool(cursor)

return paginated_call

Expand All @@ -84,6 +85,9 @@ def rate_limited_call(**kwargs: Any) -> SlackResponse:
if e.response["error"] == "ratelimited":
# Handle rate limiting: get the 'Retry-After' header value and sleep for that duration
retry_after = int(e.response.headers.get("Retry-After", 1))
logger.info(
f"Slack call rate limited, retrying after {retry_after} seconds. Exception: {e}"
)
time.sleep(retry_after)
else:
# Raise the error for non-transient errors
Expand Down
Loading