Skip to content

Commit

Permalink
[Core] Support the reduction of Port rate limit in the integrations (#…
Browse files Browse the repository at this point in the history
…1155)

# Description

What - I updated the retry mechanism to ensure the total retry time is
capped at 5 minutes, with a maximum backoff time of 5 minutes between
retries.

Why - This change is needed to handle the reduced port rate limit in
integrations more effectively, minimizing the risk of overwhelming the
system while still ensuring retries are reasonable.

How - I adjusted the retry logic to distribute retries over the 5-minute
window, scaling backoff times to align with the reduced rate limit
without exceeding the time limit.

## Type of change

Please leave one option from the following and delete the rest:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] New Integration (non-breaking change which adds a new integration)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [X] Non-breaking change (fix of existing functionality that will not
change current behavior)
- [ ] Documentation (added/updated documentation)

<h4> All tests should be run against the port production
environment(using a testing org). </h4>

### Core testing checklist

- [ ] Integration able to create all default resources from scratch
- [ ] Resync finishes successfully
- [ ] Resync able to create entities
- [ ] Resync able to update entities
- [ ] Resync able to detect and delete entities
- [ ] Scheduled resync able to abort existing resync and start a new one
- [ ] Tested with at least 2 integrations from scratch
- [ ] Tested with Kafka and Polling event listeners
- [ ] Tested deletion of entities that don't pass the selector


### Integration testing checklist

- [ ] Integration able to create all default resources from scratch
- [ ] Resync able to create entities
- [ ] Resync able to update entities
- [ ] Resync able to detect and delete entities
- [ ] Resync finishes successfully
- [ ] If new resource kind is added or updated in the integration, add
example raw data, mapping and expected result to the `examples` folder
in the integration directory.
- [ ] If resource kind is updated, run the integration with the example
data and check if the expected result is achieved
- [ ] If new resource kind is added or updated, validate that
live-events for that resource are working as expected
- [ ] Docs PR link [here](#)

### Preflight checklist

- [ ] Handled rate limiting
- [ ] Handled pagination
- [ ] Implemented the code in async
- [ ] Support Multi account

## Screenshots

Include screenshots from your environment showing how the resources of
the integration will look.

## API Documentation

Provide links to the API documentation used for this integration.

---------

Co-authored-by: Shalev Avhar <[email protected]>
  • Loading branch information
shalev007 and Shalev Avhar authored Nov 25, 2024
1 parent a0ef85e commit ab1b54a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 8 additions & 2 deletions port_ocean/clients/port/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# period of time, before raising an exception.
# The max_connections value can't be too high, as it will cause the application to run out of memory.
# The max_keepalive_connections can't be too high, as it will cause the application to run out of available connections.
PORT_HTTP_MAX_CONNECTIONS_LIMIT = 200
PORT_HTTP_MAX_CONNECTIONS_LIMIT = 100
PORT_HTTP_MAX_KEEP_ALIVE_CONNECTIONS = 50
PORT_HTTP_TIMEOUT = 60.0

Expand All @@ -28,13 +28,19 @@

_http_client: LocalStack[httpx.AsyncClient] = LocalStack()

FIVE_MINUETS = 60 * 5


def _get_http_client_context(port_client: "PortClient") -> httpx.AsyncClient:
client = _http_client.top
if client is None:
client = OceanAsyncClient(
TokenRetryTransport,
transport_kwargs={"port_client": port_client},
transport_kwargs={
"port_client": port_client,
"max_backoff_wait": FIVE_MINUETS,
"base_delay": 0.3,
},
timeout=PORT_HTTPX_TIMEOUT,
limits=PORT_HTTPX_LIMITS,
)
Expand Down
12 changes: 6 additions & 6 deletions port_ocean/helpers/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class RetryTransport(httpx.AsyncBaseTransport, httpx.BaseTransport):
HTTPStatus.GATEWAY_TIMEOUT,
]
)
MAX_BACKOFF_WAIT = 60
MAX_BACKOFF_WAIT_IN_SECONDS = 60

def __init__(
self,
wrapped_transport: Union[httpx.BaseTransport, httpx.AsyncBaseTransport],
max_attempts: int = 10,
max_backoff_wait: float = MAX_BACKOFF_WAIT,
backoff_factor: float = 0.1,
max_backoff_wait: float = MAX_BACKOFF_WAIT_IN_SECONDS,
base_delay: float = 0.1,
jitter_ratio: float = 0.1,
respect_retry_after_header: bool = True,
retryable_methods: Iterable[str] | None = None,
Expand All @@ -81,7 +81,7 @@ def __init__(
max_backoff_wait (float, optional):
The maximum amount of time (in seconds) to wait before retrying a request.
Defaults to 60.
backoff_factor (float, optional):
base_delay (float, optional):
The factor by which the waiting time will be multiplied in each retry attempt.
Defaults to 0.1.
jitter_ratio (float, optional):
Expand All @@ -105,7 +105,7 @@ def __init__(
)

self._max_attempts = max_attempts
self._backoff_factor = backoff_factor
self._base_delay = base_delay
self._respect_retry_after_header = respect_retry_after_header
self._retryable_methods = (
frozenset(retryable_methods)
Expand Down Expand Up @@ -255,7 +255,7 @@ def _calculate_sleep(
except ValueError:
pass

backoff = self._backoff_factor * (2 ** (attempts_made - 1))
backoff = self._base_delay * (2 ** (attempts_made - 1))
jitter = (backoff * self._jitter_ratio) * random.choice([1, -1])
total_backoff = backoff + jitter
return min(total_backoff, self._max_backoff_wait)
Expand Down

0 comments on commit ab1b54a

Please sign in to comment.