Skip to content

Commit

Permalink
[Integration][Jira] Ocean Rate limiting handling from Jira (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
lordsarcastic authored Jan 15, 2025
1 parent d7a79d6 commit d3fabb1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions integrations/jira/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- towncrier release notes start -->

## 0.2.22 (2025-01-15)


### Improvements

- Added rate limit support to avoid failures due to 429 errors



## 0.2.21 (2025-01-15)


Expand Down
14 changes: 10 additions & 4 deletions integrations/jira/jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from port_ocean.context.ocean import ocean
from port_ocean.utils import http_async_client


PAGE_SIZE = 50
WEBHOOK_NAME = "Port-Ocean-Events-Webhook"
MAX_CONCURRENT_REQUESTS = 10
Expand Down Expand Up @@ -60,7 +59,14 @@ def __init__(self, jira_url: str, jira_email: str, jira_token: str) -> None:
self.client = http_async_client
self.client.auth = self.jira_api_auth
self.client.timeout = Timeout(30)
self.semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
self._semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)

async def _handle_rate_limit(self, response: Response) -> None:
if response.status_code == 429:
logger.warning(
f"Jira API rate limit reached. Waiting for {response.headers['Retry-After']} seconds."
)
await asyncio.sleep(int(response.headers["Retry-After"]))

async def _send_api_request(
self,
Expand All @@ -71,13 +77,14 @@ async def _send_api_request(
headers: dict[str, str] | None = None,
) -> Any:
try:
async with self.semaphore:
async with self._semaphore:
response = await self.client.request(
method=method, url=url, params=params, json=json, headers=headers
)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
await self._handle_rate_limit(e.response)
logger.error(
f"Jira API request failed with status {e.response.status_code}: {method} {url}"
)
Expand Down Expand Up @@ -117,7 +124,6 @@ async def _get_cursor_paginated_data(
method: str,
extract_key: str,
initial_params: dict[str, Any] | None = None,
page_size: int = PAGE_SIZE,
cursor_param: str = "cursor",
) -> AsyncGenerator[list[dict[str, Any]], None]:
params = initial_params or {}
Expand Down
2 changes: 1 addition & 1 deletion integrations/jira/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jira"
version = "0.2.21"
version = "0.2.22"
description = "Integration to bring information from Jira into Port"
authors = ["Mor Paz <[email protected]>"]

Expand Down

0 comments on commit d3fabb1

Please sign in to comment.