Repository navigation
Release idle shared-storage topics #4016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+747
−85
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9818de3
Release idle shared-storage topics (#4010)
T4rk1n 56365de
Make topic ttl per publish and expire topics as a whole
T4rk1n 5e6be57
Keep waiting readers' topics alive and sweep empty topics
T4rk1n a9afdfc
Merge branch 'dev' into fix/4010-free-shared-storage-topics
T4rk1n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| import abc | ||
| import asyncio | ||
| import math | ||
| from typing import Any, AsyncIterator, Iterator, List, Optional, Tuple | ||
|
|
||
|
|
||
|
|
@@ -35,6 +36,18 @@ class SharedStorageGap(SharedStorageError): | |
| """ | ||
|
|
||
|
|
||
| # Shortest topic ttl a publish accepts. A reconnecting reader can easily be | ||
| # gone for a second, and a shorter ttl would drop its topic before it is back. | ||
| MIN_TOPIC_TTL = 1.0 | ||
|
Comment on lines
+39
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect for Redis, 1s might be too low. Not sure if you think it will be a problem in practice, but I'll call it out here for your consideration. |
||
|
|
||
|
|
||
| def check_topic_ttl(ttl: Optional[float]) -> None: | ||
| if ttl is not None and not (math.isfinite(ttl) and ttl >= MIN_TOPIC_TTL): | ||
| raise SharedStorageError( | ||
| f"topic ttl must be None or a finite {MIN_TOPIC_TTL}s or more, got {ttl!r}" | ||
| ) | ||
|
|
||
|
|
||
| class Subscription(abc.ABC): | ||
| """A live, ordered view of a topic. | ||
|
|
||
|
|
@@ -133,8 +146,16 @@ def delete(self, key: str) -> None: | |
| ... | ||
|
|
||
| @abc.abstractmethod | ||
| def publish(self, topic: str, message: Any) -> None: | ||
| """Append ``message`` to ``topic``; delivered to every current subscriber.""" | ||
| def publish(self, topic: str, message: Any, ttl: Optional[float] = None) -> None: | ||
| """Append ``message`` to ``topic``; delivered to every current subscriber. | ||
|
|
||
| ``ttl`` (seconds, at least ``MIN_TOPIC_TTL``) lets the store release the | ||
| topic, buffer and sequence both, once nobody has published to or read | ||
| from it for that long. A reader that keeps polling keeps it alive. A | ||
| later publish starts the topic over, so a consumer returning with an old | ||
| cursor gets ``SharedStorageGap``. ``None`` (the default) keeps the topic | ||
| for the life of the store. The latest publish's ``ttl`` applies. | ||
| """ | ||
|
|
||
| # --- asyncio variants -------------------------------------------------- | ||
| # Code running on an event loop (ASGI request handlers, the streaming | ||
|
|
@@ -155,9 +176,11 @@ async def aset(self, key: str, value: Any, ttl: Optional[float] = None) -> None: | |
| async def adelete(self, key: str) -> None: | ||
| await asyncio.get_running_loop().run_in_executor(None, self.delete, key) | ||
|
|
||
| async def apublish(self, topic: str, message: Any) -> None: | ||
| async def apublish( | ||
| self, topic: str, message: Any, ttl: Optional[float] = None | ||
| ) -> None: | ||
| await asyncio.get_running_loop().run_in_executor( | ||
| None, self.publish, topic, message | ||
| None, self.publish, topic, message, ttl | ||
| ) | ||
|
|
||
| @abc.abstractmethod | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A placeholder topic created by a
readoperation does not have any ttl, so a cleanup sweep will never release it. This can happen in ordinary use:A user loses their connection mid-stream (or closes the laptop) for longer than the grace window plus the ttl. The pump gives up and publishes its last frame, and the topic is released after the ttl. When the browser returns it polls the old topic, poll creates it again empty with no ttl, and the client gets {reset: true}. That topic is never released.
Since it should always be safe to drop an empty topic, maybe the sweep can also drop topics with no messages and no readers after a short idle time?