Skip to content

feat: Networking and Circuit Breaker Logic#6

Merged
choudlet merged 9 commits into
mainfrom
chrish/sc-31907/android-sdk-5
Jan 22, 2026
Merged

feat: Networking and Circuit Breaker Logic#6
choudlet merged 9 commits into
mainfrom
chrish/sc-31907/android-sdk-5

Conversation

@choudlet
Copy link
Copy Markdown
Collaborator

Summary

Adds HTTP client with circuit breaker pattern for fault-tolerant network
communication. This lays the foundation for PR #6 (Dispatcher & Flush Logic).

What's Included

Circuit Breaker

  • Three-state machine: Closed → Open → Half-Open → Closed
  • Exponential backoff with configurable jitter (default 20%)
  • Configurable failure threshold, cooldown, and max cooldown
  • Thread-safe via synchronized

Network Client

  • NetworkClient interface with NetworkResponse data class
  • OkHttpNetworkClient implementation using OkHttp 4.12.0
  • Per-request timeout configuration
  • Coroutine support via suspendCancellableCoroutine
  • HTTP 4xx/5xx returned as responses, IOException for transport failures

Utilities

  • RetryAfterParser - Parses Retry-After header (numeric seconds + HTTP-date
    formats)
  • FakeNetworkClient - Test double for unit testing

Test Coverage

  • 40 new network tests
  • 227 total tests passing
  • MockWebServer integration tests for real HTTP behavior

choudlet and others added 8 commits January 22, 2026 12:27
Add OkHttp 4.12.0 as an implementation dependency for the HTTP client
and MockWebServer 4.12.0 as a test dependency for network testing.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements a sealed class with three singleton states for the circuit
breaker pattern: Closed (normal operation), Open (blocking requests),
and HalfOpen (testing recovery).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implements the core CircuitBreaker with:
- Three states: Closed, Open, HalfOpen
- Failure threshold tracking to trip circuit
- Exponential backoff with jitter for cooldown
- Half-open state for recovery probing
- Thread-safe with synchronized blocks

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Adds 9 new tests to verify:
- Half-open state transitions after cooldown expires
- Success in half-open closes circuit
- Failure in half-open reopens circuit
- Half-open limits concurrent probe requests
- Exponential backoff on repeated opens
- Max cooldown is respected
- Jitter adds randomness to backoff
- Edge case: failureThreshold of 1 trips immediately
- Edge case: zero jitter produces consistent backoff

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Adds parseRetryAfterMs() utility function that parses Retry-After
HTTP headers per RFC 7231. Supports both numeric seconds and HTTP-date
formats with case-insensitive header lookup.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 22, 2026

Test Results

 36 files  + 8   36 suites  +8   43s ⏱️ +5s
227 tests +40  227 ✅ +40  0 💤 ±0  0 ❌ ±0 
454 runs  +80  454 ✅ +80  0 💤 ±0  0 ❌ ±0 

Results for commit 9dd8d7d. ± Comparison against base commit 69ff5d4.

♻️ This comment has been updated with latest results.

Map.forEach and Headers.forEach require API 24. Using Kotlin
for-in loops instead to support minSdk 21.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@choudlet choudlet merged commit 409f535 into main Jan 22, 2026
3 checks passed
choudlet added a commit that referenced this pull request Apr 10, 2026
* build: add OkHttp dependencies for networking layer

Add OkHttp 4.12.0 as an implementation dependency for the HTTP client
and MockWebServer 4.12.0 as a test dependency for network testing.

* feat(network): add CircuitState sealed class

Implements a sealed class with three singleton states for the circuit
breaker pattern: Closed (normal operation), Open (blocking requests),
and HalfOpen (testing recovery).

* feat(network): add CircuitBreaker state machine

Implements the core CircuitBreaker with:
- Three states: Closed, Open, HalfOpen
- Failure threshold tracking to trip circuit
- Exponential backoff with jitter for cooldown
- Half-open state for recovery probing
- Thread-safe with synchronized blocks

* test(network): add CircuitBreaker half-open and backoff tests

Adds 9 new tests to verify:
- Half-open state transitions after cooldown expires
- Success in half-open closes circuit
- Failure in half-open reopens circuit
- Half-open limits concurrent probe requests
- Exponential backoff on repeated opens
- Max cooldown is respected
- Jitter adds randomness to backoff
- Edge case: failureThreshold of 1 trips immediately
- Edge case: zero jitter produces consistent backoff

* feat(network): add NetworkClient interface and NetworkResponse

* feat(network): add Retry-After header parser

Adds parseRetryAfterMs() utility function that parses Retry-After
HTTP headers per RFC 7231. Supports both numeric seconds and HTTP-date
formats with case-insensitive header lookup.

* test(network): add FakeNetworkClient for testing

* feat: circuit breaker and network logic

* fix(network): use for-loops instead of forEach for API 21 compat

Map.forEach and Headers.forEach require API 24. Using Kotlin
for-in loops instead to support minSdk 21.
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