Skip to content

Change exponential backoff timeouts + add constants#51

Merged
enoch85 merged 5 commits intomainfrom
fix/replace-hardcoded-timeouts-with-constants
Nov 3, 2025
Merged

Change exponential backoff timeouts + add constants#51
enoch85 merged 5 commits intomainfrom
fix/replace-hardcoded-timeouts-with-constants

Conversation

@enoch85
Copy link
Copy Markdown
Owner

@enoch85 enoch85 commented Nov 2, 2025

Eliminates hardcoded timeout, retry, cache TTL, and time conversion values across 17 files, replacing them with centralized constants from Network.Defaults and TimeInterval.

Changes

Added constants to Network.Defaults:

  • SECONDS_PER_HOUR = 3600
  • RETRY_CUTOFF_TIME_HOUR = 23, RETRY_CUTOFF_TIME_MINUTE = 50
  • CACHE_DEFAULT_TTL_SECONDS = 3600

Replaced hardcoded values:

  • HTTP timeouts: 60Network.Defaults.HTTP_TIMEOUT in comed.py, api_client.py, session_manager.py
  • Retry configuration: max_retries=3Network.Defaults.RETRY_COUNT in error_handler.py
  • Retry intervals: 1800Network.Defaults.STANDARD_UPDATE_INTERVAL_MINUTES * SECONDS_PER_MINUTE in 4 API clients
  • Cutoff times: time(23, 50)time(RETRY_CUTOFF_TIME_HOUR, RETRY_CUTOFF_TIME_MINUTE) in 3 API clients
  • Cache TTLs: 6 * 60 * 60Network.Defaults.CACHE_TTL in data_fetch.py
  • Time intervals: 15TimeInterval.get_interval_minutes() in 3 parsers/coordinators
  • Time conversions: 3600, * 60SECONDS_PER_HOUR, SECONDS_PER_MINUTE in 5 utility modules

Example:

# Before
async with asyncio.timeout(60):
    await session.get(url)
cache_ttl = 6 * 60 * 60
if now.time() > time(23, 50):
    return

# After  
async with asyncio.timeout(Network.Defaults.HTTP_TIMEOUT):
    await session.get(url)
cache_ttl = Network.Defaults.CACHE_TTL
if now.time() > time(Network.Defaults.RETRY_CUTOFF_TIME_HOUR, Network.Defaults.RETRY_CUTOFF_TIME_MINUTE):
    return

…onstant

- Changed hardcoded timeout in comed.py: asyncio.timeout(60) → asyncio.timeout(Network.Defaults.HTTP_TIMEOUT)
- Changed hardcoded timeout in api_client.py: aiohttp.ClientTimeout(total=60) → aiohttp.ClientTimeout(total=Network.Defaults.HTTP_TIMEOUT)
- Changed hardcoded timeout in session_manager.py: timeout=60 → timeout=Network.Defaults.HTTP_TIMEOUT
- Added Network import to all three files
- Formatted all changes with black

All 346 tests passing.

Rationale:
- HTTP_TIMEOUT (60s) is a safety net above FallbackManager's max timeout (45s)
- Ensures consistent timeout configuration across the codebase
- FallbackManager controls actual retry strategy with exponential backoff (5s → 15s → 45s)
- HTTP layer timeout prevents indefinite hangs while allowing proper retry logic
Comment thread custom_components/ge_spot/api/base/session_manager.py Outdated
Copy link
Copy Markdown

Copilot AI commented Nov 2, 2025

@enoch85 I've opened a new pull request, #52, to work on those changes. Once the pull request is ready, I'll request review from you.

…tants (#52)

* Initial plan

* Replace hardcoded max_retries=3 with Network.Defaults.RETRY_COUNT constant

Co-authored-by: enoch85 <4511254+enoch85@users.noreply.github.com>

* Replace hardcoded retry and delay values with Network.Defaults constants in error_handler.py

Co-authored-by: enoch85 <4511254+enoch85@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: enoch85 <4511254+enoch85@users.noreply.github.com>
@enoch85
Copy link
Copy Markdown
Owner Author

enoch85 commented Nov 2, 2025

@copilot Please look through the changed constants here in this PR. Then look for more occurances were we forgot to change to the use of constants instead of hard coded values. Be thorough, think deep, inspect every line of the code base. Read all the code base in sections by 400 lines each. Analyze if we need constant replacements in more places.

Use your built in tools to grep, sed, and whatever you need to truly find everything.

Create a PR with your changes, if any.

Copy link
Copy Markdown

Copilot AI commented Nov 2, 2025

@enoch85 I've opened a new pull request, #53, to work on those changes. Once the pull request is ready, I'll request review from you.

…#53)

* Initial plan

* Replace hardcoded values with constants across codebase

- Added new constants to Network.Defaults:
  * SECONDS_PER_HOUR = 3600
  * RETRY_CUTOFF_TIME_HOUR = 23
  * RETRY_CUTOFF_TIME_MINUTE = 50
  * CACHE_DEFAULT_TTL_SECONDS = 3600

- Replaced hardcoded retry interval (1800) with Network.Defaults.STANDARD_UPDATE_INTERVAL_MINUTES * SECONDS_PER_MINUTE in:
  * api/utils.py
  * api/nordpool.py
  * api/energi_data.py
  * api/entsoe.py

- Replaced hardcoded time(23, 50) with new RETRY_CUTOFF_TIME constants in:
  * api/nordpool.py
  * api/energi_data.py
  * api/entsoe.py

- Replaced hardcoded cache age (6 * 60 * 60) with Network.Defaults.CACHE_TTL in:
  * api/base/data_fetch.py

- Replaced hardcoded 15 minutes with TimeInterval.get_interval_minutes() in:
  * api/parsers/comed_parser.py (timedelta and // 15 calculations)
  * api/parsers/entsoe_parser.py (timedelta calculation)
  * coordinator/data_validity.py (timedelta calculation)

- Replaced hardcoded fetch_interval_minutes default (15) with Network.Defaults.MIN_UPDATE_INTERVAL_MINUTES in:
  * coordinator/fetch_decision.py

- Replaced hardcoded 3600 with CACHE_DEFAULT_TTL_SECONDS or SECONDS_PER_HOUR in:
  * utils/advanced_cache.py (default TTL)
  * timezone/dst_handler.py (time conversions)
  * timezone/interval_calculator.py (time conversions)

- Replaced hardcoded * 60 conversions with Network.Defaults.SECONDS_PER_MINUTE in:
  * coordinator/cache_manager.py
  * coordinator/unified_price_manager.py

All files formatted with black. Syntax validated successfully.

Co-authored-by: enoch85 <4511254+enoch85@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: enoch85 <4511254+enoch85@users.noreply.github.com>

This comment was marked as outdated.

@enoch85 enoch85 changed the title Replace hardcoded timeout values with Network.Defaults.HTTP_TIMEOUT c… Chache backoff timout intervals Nov 2, 2025
@enoch85 enoch85 changed the title Chache backoff timout intervals Change backoff timout intervals Nov 2, 2025
@enoch85 enoch85 changed the title Change backoff timout intervals Change exponential backoff timeouts + add constants Nov 2, 2025
@enoch85 enoch85 merged commit 3c56cd4 into main Nov 3, 2025
13 of 14 checks passed
@enoch85 enoch85 deleted the fix/replace-hardcoded-timeouts-with-constants branch November 3, 2025 17:24
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.

2 participants