-
-
Couldn't load subscription status.
- Fork 30
Description
Is your feature request related to a problem? Please describe.
Yes. Currently, when the onStatusChange stream detects a lost internet connection, the internal checker retries the connection at a fixed interval.
While this works, it can be inefficient if a device is offline for an extended period. The constant, rapid polling can lead to:
- Unnecessary battery drain, which is critical on mobile devices.
- Wasted CPU cycles performing checks that are very likely to fail.
- Repetitive network traffic that adds noise and serves no purpose during an outage.
Proposed Solution
I propose adding an optional exponential backoff strategy for the retry mechanism.
When a connection fails, the checker would wait for an initial delay (e.g., 1 second). If the next check also fails, it would double the delay (2 seconds, then 4, then 8, and so on) up to a configurable maximum limit.
This could be implemented by adding new optional parameters to the InternetConnectionChecker.createInstance constructor, for example:
useExponentialBackoff(bool): flag to enable/disable the feature. Defaults tofalsefor backward compatibility.initialDelay(Duration): starting delay after the first failure.maxDelay(Duration): maximum time to wait between retries, preventing excessively long delays.multiplier(double): factor to multiply the delay by on each failure (typically2.0).
Once the internet connection is restored, the delay timer should reset to its initial value.
Alternative Solution
Here are other proposal if the proposed backoff is not feasible.
- The current fixed-interval polling: This is the status quo. It's simple but inefficient for prolonged outages, as described above.
- Linear backoff: We could simply add a fixed amount of time to the delay after each failure (e.g., 1s, 2s, 3s, 4s). However, exponential backoff is the industry standard for this kind of problem because it scales much more effectively, finding a good balance between responsiveness for short outages and efficiency for long ones.
Additional context
Implementing this feature would make internet_connection_checker_plus a more robust and battery-friendly package. It's a common pattern used in network libraries to handle retries gracefully.