Skip to content

refactor: replace connectivity Debouncer with Throttler in ConnectivityChangesNotifier#3202

Merged
veloce merged 1 commit into
lichess-org:mainfrom
YairAndrade1:refactor/connectivity-throttler
May 19, 2026
Merged

refactor: replace connectivity Debouncer with Throttler in ConnectivityChangesNotifier#3202
veloce merged 1 commit into
lichess-org:mainfrom
YairAndrade1:refactor/connectivity-throttler

Conversation

@YairAndrade1

@YairAndrade1 YairAndrade1 commented May 16, 2026

Copy link
Copy Markdown
Contributor

Closes #3201

Motivation

The current implementation uses a Debouncer with a 5-second delay to handle rapid connectivity changes. A debouncer delays every event in a burst, including the first one. This means that when the user reconnects after a brief disconnection, the app waits a full 5 seconds before reacting and resuming socket connections or syncing correspondence moves, even when the network is already stable.

Change

Replace the Debouncer with the existing Throttler from lib/src/utils/rate_limit.dart. A throttler delivers the first event in a burst immediately and suppresses subsequent events within the same time window. This preserves the protection against rapid network fluctuations while eliminating the unnecessary delay on the first reconnection event.

The throttle delay is extracted into a named constant (kConnectivityThrottleDelay) to make it configurable in tests without relying on real clock waits.

Behavior difference

Scenario Before (Debouncer) After (Throttler)
Single reconnection event Delayed 5 seconds Delivered immediately
Rapid network fluctuation (WiFi → mobile → WiFi) Delivered after 5s silence First event delivered, rest suppressed
Test clock dependency Requires real clock waits Configurable via constant

Testing

Existing tests pass. No new dependencies introduced. Throttler already exists in lib/src/utils/rate_limit.dart.


Note: This contribution was made as part of a mobile application development course. The change was identified through a micro-optimization audit of the codebase, where the behavioral difference between Debouncer and Throttler was analyzed as a real-world example of rate-limiting patterns in a production Flutter app.

@veloce veloce left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch! Thank you.

@veloce veloce merged commit ef4128a into lichess-org:main May 19, 2026
2 checks passed
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.

ConnectivityChangesNotifier: debouncer delays reconnection by 5 seconds

2 participants