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

Make timeouts part of RequestsSettings struct #450

Merged
merged 2 commits into from
Nov 1, 2023

Conversation

sergei-encord
Copy link
Collaborator

@sergei-encord sergei-encord commented Oct 17, 2023

Currently connection retry settings and connection timeouts are set in different ways.
This PR unifies them and makes sure they are correctly propagated to all objects.

Summary by CodeRabbit

New Feature:

  • Introduced a RequestsSettings struct to unify connection timeouts and retries settings across the application.
  • Updated BaseConfig, http/constants.py, http/querier.py, http/v2/api_client.py, http/utils.py to use the new RequestsSettings struct.
  • Added test cases in tests/http/timeout_overrides_setting.py to verify the unified timeout settings.
  • Updated docs/source/code_examples/tutorials/configurations/overwrite_timeouts.py to reflect the changes.

Refactor:

  • Improved type annotations in tests/fixtures.py.

🎉 Here's to the code that's now more neat, 🥂
With timeouts and retries in one seat. 🪑
No more confusion, no more fret, 🚫
This PR is the best one yet! 🏆

@github-actions
Copy link

github-actions bot commented Oct 17, 2023

Image description CodeRabbit

Walkthrough: This update introduces a unified approach to setting connection timeouts and retries across the application. It replaces hard-coded values with configurable settings via a RequestsSettings struct. The changes also improve type annotations in test fixtures for better code clarity.

Changes:

Files Summary
encord/configs.py, encord/http/constants.py, encord/http/querier.py, encord/http/v2/api_client.py, encord/http/utils.py Introduced a RequestsSettings struct to unify settings for connection timeouts and retries. Removed hard-coded values and replaced them with configurable settings. Updated function signatures and calls to accommodate these changes.
tests/http/timeout_overrides_setting.py Updated tests to verify the new unified connection retry settings and connection timeouts.
docs/source/code_examples/tutorials/configurations/overwrite_timeouts.py Updated documentation to reflect the new way of setting connect timeout, read timeout, and write timeout using the RequestsSettings struct.
tests/fixtures.py Improved type annotations in test fixtures for better code clarity. No change in functionality.

Notes

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Ignoring further reviews

  • Type @coderabbitai: ignore anywhere in the PR description to ignore further reviews from the bot.

@github-actions
Copy link

github-actions bot commented Oct 17, 2023

Unit test report

124 tests   113 ✔️  5s ⏱️
    1 suites    11 💤
    1 files        0

Results for commit e77fc59.

♻️ This comment has been updated with latest results.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 2308da3 and e3b94c3 commits.
Files selected (6)
  • encord/configs.py (1)
  • encord/http/constants.py (2)
  • encord/http/querier.py (2)
  • encord/http/v2/api_client.py (1)
  • tests/fixtures.py (2)
  • tests/http/timeout_overrides_setting.py (1)
Files not reviewed due to errors (2)
  • encord/http/constants.py (no response)
  • encord/http/v2/api_client.py (no response)
Files skipped from review due to trivial changes (1)
  • tests/fixtures.py
Review comments generated (5)
  • Review: 3
  • LGTM: 2

tests/http/timeout_overrides_setting.py Outdated Show resolved Hide resolved
@sergei-encord sergei-encord force-pushed the fix-timeout-settings-propagation branch from e3b94c3 to 17b9d27 Compare October 20, 2023 08:29
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Commits Files that changed from the base of the PR and between e141932 and 17b9d27 commits.
Files selected (7)
  • encord/configs.py (1)
  • encord/http/constants.py (2)
  • encord/http/querier.py (2)
  • encord/http/utils.py (1)
  • encord/http/v2/api_client.py (1)
  • tests/fixtures.py (2)
  • tests/http/timeout_overrides_setting.py (1)
Files not reviewed due to errors (3)
  • encord/http/constants.py (no response)
  • encord/configs.py (no response)
  • encord/http/v2/api_client.py (no response)
Files skipped from review due to trivial changes (1)
  • tests/fixtures.py
Review comments generated (5)
  • Review: 3
  • LGTM: 2

Comment on lines 148 to +153
backoff_factor: float,
cache_max_age: int = CACHE_DURATION_IN_SECONDS,
) -> None:
with create_new_session(max_retries=max_retries, backoff_factor=backoff_factor) as session:
with create_new_session(
max_retries=max_retries, backoff_factor=backoff_factor, connect_retries=max_retries
) as session:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The create_new_session function now includes a new parameter connect_retries. This is a good change as it allows the number of connection retries to be configured, improving the flexibility and robustness of the HTTP client. However, it's not clear from this hunk whether the create_new_session function has been updated to handle this new argument. Please ensure that the function definition of create_new_session has been updated accordingly.

Comment on lines 156 to 162
except Exception:
return RequestContext()

def _request(self, method: QueryMethods, db_object_type: Type[T], uid, timeout, payload=None):
def _request(self, method: QueryMethods, db_object_type: Type[T], uid, timeout: int, payload=None) -> Request:
request = Request(method, db_object_type, uid, timeout, self._config.connect_timeout, payload)

request.headers = self._config.define_headers(request.data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The _request method now includes a timeout parameter. However, it's not clear from the provided context whether this timeout value is being used correctly within the Request object. If the Request class doesn't handle the timeout parameter properly, it could lead to unexpected behavior. Please ensure that the Request class or any other classes/functions using this method are updated accordingly to handle this new parameter.

Comment on lines 205 to 211

@contextmanager
def create_new_session(
max_retries: Optional[int] = None, backoff_factor: float = 0, connect_retries=3
max_retries: Optional[int], backoff_factor: float, connect_retries
) -> Generator[Session, None, None]:
retry_policy = Retry(
connect=connect_retries,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The signature of the create_new_session function has been modified to remove default values for max_retries, backoff_factor, and connect_retries. This change will require all calls to create_new_session to provide these parameters explicitly. Ensure that all parts of the codebase that call this function have been updated to pass these parameters. Otherwise, this could lead to a TypeError at runtime when the function is called with missing arguments.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 17b9d27 and e77fc59 commits.
Files selected (4)
  • docs/source/code_examples/tutorials/configurations/overwrite_timeouts.py (1)
  • encord/configs.py (1)
  • encord/http/constants.py (2)
  • tests/http/timeout_overrides_setting.py (1)
Files skipped from review due to trivial changes (1)
  • docs/source/code_examples/tutorials/configurations/overwrite_timeouts.py
Review comments generated (5)
  • Review: 2
  • LGTM: 3

Comment on lines 50 to 65
_ENCORD_SSH_KEY = "ENCORD_SSH_KEY"
_ENCORD_SSH_KEY_FILE = "ENCORD_SSH_KEY_FILE"

READ_TIMEOUT = 180 # In seconds
WRITE_TIMEOUT = 180 # In seconds
CONNECT_TIMEOUT = 180 # In seconds

logger = logging.getLogger(__name__)

from encord.exceptions import ResourceNotFoundError


class BaseConfig(ABC):
def __init__(self, endpoint: str, requests_settings: RequestsSettings = DEFAULT_REQUESTS_SETTINGS):
self.read_timeout: int = READ_TIMEOUT
self.write_timeout: int = WRITE_TIMEOUT
self.connect_timeout: int = CONNECT_TIMEOUT
self.read_timeout: int = requests_settings.read_timeout
self.write_timeout: int = requests_settings.write_timeout
self.connect_timeout: int = requests_settings.connect_timeout

self.endpoint: str = endpoint
self.requests_settings = requests_settings
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The changes look good. The timeouts are now part of the RequestsSettings struct and are no longer hard-coded, which improves flexibility and maintainability. The BaseConfig class now uses these settings from the requests_settings object. This is a good practice as it allows for easier configuration changes in the future.

Comment on lines +75 to +81
def verify_timeout_and_reset(mock: MagicMock, requests_settings: RequestsSettings, expected_call_count=1) -> None:
assert mock.call_count == expected_call_count
assert all(
call[1]["timeout"] == (requests_settings.connect_timeout, requests_settings.read_timeout)
for call in mock.call_args_list
)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

The function verify_timeout_and_reset checks if the timeout settings are correctly propagated. However, it only checks for connect_timeout and read_timeout. The write_timeout is not checked here. If the write_timeout is also supposed to be propagated and checked, this would be a logic issue.

Suggested change
def verify_timeout_and_reset(mock: MagicMock, requests_settings: RequestsSettings, expected_call_count=1) -> None:
assert mock.call_count == expected_call_count
assert all(
call[1]["timeout"] == (requests_settings.connect_timeout, requests_settings.read_timeout)
for call in mock.call_args_list
)
def verify_timeout_and_reset(mock: MagicMock, requests_settings: RequestsSettings, expected_call_count=1) -> None:
assert mock.call_count == expected_call_count
assert all(
call[1]["timeout"] == (requests_settings.connect_timeout, requests_settings.read_timeout, requests_settings.write_timeout)
for call in mock.call_args_list
)
mock.reset_mock()

@sergei-encord sergei-encord merged commit 9624391 into master Nov 1, 2023
4 checks passed
@sergei-encord sergei-encord deleted the fix-timeout-settings-propagation branch November 1, 2023 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant