Skip to content

feat: debounce_timing [android]#21

Merged
choudlet merged 5 commits into
mainfrom
chrish/sc-37713/android-network-reachability-debouncing
Apr 10, 2026
Merged

feat: debounce_timing [android]#21
choudlet merged 5 commits into
mainfrom
chrish/sc-37713/android-network-reachability-debouncing

Conversation

@choudlet
Copy link
Copy Markdown
Collaborator

@choudlet choudlet commented Apr 9, 2026

Summary

Ticket: SC-37713

Adds debouncing to network reachability online transitions in the Android SDK:

  • Online transitions are debounced by 2 seconds — the circuit breaker reset, dispatcher resume, and flush only fire after connectivity has been stable for 2s
  • Offline transitions remain immediate — no delay in pausing HTTP attempts
  • Rapid flapping (online/offline/online/...) collapses into a single online action once connectivity stabilizes

Implementation

New DebouncedNetworkMonitor decorator implements NetworkMonitor, wrapping the raw AndroidNetworkMonitor. The client creates it as DebouncedNetworkMonitor(rawMonitor, scope) and uses it like any other NetworkMonitor — the debounce is transparent. (DebouncedNetworkMonitor implementing NetworkReachability), keeping cross-platform alignment.

Uses coroutine delay() + Job cancellation for the timer mechanism, with @Volatile on the debounce job reference. stop() handles both inner monitor teardown and debounce cancellation — no separate cleanup needed in the client.

Files Changed

File Change
DebouncedNetworkMonitor.kt New — decorator wrapping NetworkMonitor with debounced online transitions (~60 lines)
DebouncedNetworkMonitorTest.kt New — 12 unit tests covering debounce timing, flapping, cancellation, isConnected state, cleanup
MetaRouterAnalyticsClient.kt Modified — wrap raw monitor in decorator; simplified wiring and reset() cleanup

choudlet and others added 3 commits April 7, 2026 20:04
…gration [android]

Add network connectivity monitoring to pause HTTP delivery when offline
and resume with circuit breaker reset when connectivity returns. This is
PR 1 of 2 for the network reachability feature.

New:
- NetworkMonitor interface + AndroidNetworkMonitor (ConnectivityManager)
- CircuitBreaker.reset() for offline->online transitions
- Dispatcher.pauseForOffline()/resumeFromOffline() with networkPaused guard
- InitOptions.maxOfflineDiskEvents parameter (default 10000)
- FakeNetworkMonitor test helper for DI in tests
- getDebugInfo() includes "networkStatus": "connected"|"disconnected"

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Online transitions are debounced by 2s before triggering circuit breaker
reset and dispatcher flush. Offline transitions remain immediate. Rapid
flapping collapses into a single online action once connectivity stabilizes.

New DebouncedNetworkHandler class sits between the network monitor callback
and dispatcher side-effects. 10 new unit tests cover debounce timing,
flapping, cancellation, and cleanup.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@choudlet choudlet changed the base branch from chrish/sc-36909/story-network-reachability-awareness to main April 10, 2026 16:18
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 10, 2026

Test Results

 58 files  + 2   58 suites  +2   55s ⏱️ -8s
442 tests +12  442 ✅ +12  0 💤 ±0  0 ❌ ±0 
884 runs  +24  884 ✅ +24  0 💤 ±0  0 ❌ ±0 

Results for commit 4064222. ± Comparison against base commit 290cc69.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown

@brandon-metarouter brandon-metarouter left a comment

Choose a reason for hiding this comment

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

Pre-approving, left a comment abount consolidating some cancels, but just a nit.

Comment on lines +26 to +43
fun onConnectivityChanged(connected: Boolean) {
if (connected) {
// Online: debounce — only act after connectivity is stable for 2s
debounceJob?.cancel()
Logger.log("Network connectivity detected — debouncing for stability")
debounceJob = scope.launch {
delay(DEBOUNCE_MS)
Logger.log("Network connectivity stable — resuming dispatcher")
onOnline()
}
} else {
// Offline: act immediately, cancel any pending online debounce
debounceJob?.cancel()
debounceJob = null
Logger.log("Network connectivity lost — pausing HTTP delivery")
onOffline()
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

So debounceJob?.cancel() is called in both if and else, maybe extract it and move it up? Also maybe just call in the cancel method instead? Note: I don't think it is a biggie or breaks anything if you call cancel() and it debounceJob = null... you reassign it in the if block anyway.

fun onConnectivityChanged(connected: Boolean) {                                                                                                  
      cancel()                                                                                                                                     
      if (connected) {   
      ...
      } else {
      ....
      }
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

swapping this!

@choudlet choudlet merged commit 96393d1 into main Apr 10, 2026
3 checks passed
choudlet added a commit that referenced this pull request Apr 10, 2026
* feat: network reachability awareness — core monitor + dispatcher integration [android]

Add network connectivity monitoring to pause HTTP delivery when offline
and resume with circuit breaker reset when connectivity returns. This is
PR 1 of 2 for the network reachability feature.

New:
- NetworkMonitor interface + AndroidNetworkMonitor (ConnectivityManager)
- CircuitBreaker.reset() for offline->online transitions
- Dispatcher.pauseForOffline()/resumeFromOffline() with networkPaused guard
- InitOptions.maxOfflineDiskEvents parameter (default 10000)
- FakeNetworkMonitor test helper for DI in tests
- getDebugInfo() includes "networkStatus": "connected"|"disconnected"

* fix: @volatile on callback + listener fields

* feat: debounce network reachability online transitions (2s)

Online transitions are debounced by 2s before triggering circuit breaker
reset and dispatcher flush. Offline transitions remain immediate. Rapid
flapping collapses into a single online action once connectivity stabilizes.

New DebouncedNetworkHandler class sits between the network monitor callback
and dispatcher side-effects. 10 new unit tests cover debounce timing,
flapping, cancellation, and cleanup.


* pr review comments

---------
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