Connect Telegram users after startup lazily to avoid slow startup times #698
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #697
Fixes #316
This PR refactors the way connections are made to Telegram slightly which hopefully doesn't hurt smaller bridges, but actively improves the startup time of larger bridges. Previously, the Telegram bridge would attempt to connect all puppeted users on startup to Telegram and only become "ready" once everyone had been connected (or an attempt had been made). However, this becomes a challenge at scale in the 100s of clients because these connections can each take some time which causes traffic to be delayed and ultimately leads to a worse experience.
This PR instead starts connections to Telegram after the bridge has finished it's startup seqeuence. This is also backed by a semaphore to prevent more than N connections to be attempted at once, this is by design as it allows active Matrix users to "jump ahead" in the queue by calling
ensure_started
when handling a Matrix message and therefore get connected before they would usually via the queue.The semaphore lock is important, as by default all users attempt to connect at once and thus lock in a waiting state until the bridge has connected them. This would mean that calls to ensure_started would fail as a connection would be in progress.
Thus, this solution hopefully ensures that active users get connected quickly without slowing down traffic processing.
Myself and @tadzik have tested this PR on our side, so we believe it has legs.