-
-
Notifications
You must be signed in to change notification settings - Fork 209
Implement a Redis Delayed Queue to support sorted sets #599
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
Open
toluaina
wants to merge
12
commits into
main
Choose a base branch
from
ta_delayed_redis_queue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a053821
Implement a Redis Delayed Queue to support sorted sets
toluaina 09cee50
Auto flush the ready items if no tasks in flight
toluaina 5371ed5
fixed failing tests
toluaina 5d2a8bc
reraise with exception
toluaina f656d83
acquire advisory_lock on can_create_replication_slot
toluaina e62a753
implement priority as weight; remove delayed
toluaina 447d5d3
rebase with main
toluaina 6765177
Merge branch 'main' into ta_delayed_redis_queue
toluaina 8740261
prevent same timestamp for all items
toluaina c323da4
Handle eventual consistency of the logical replication slot
toluaina 306f1a1
fic type hints
toluaina 0f6d8a6
reset the payloads after flush
toluaina 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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |||||||
| import typing as t | ||||||||
| from collections import defaultdict | ||||||||
| from itertools import groupby | ||||||||
| from math import inf | ||||||||
| from pathlib import Path | ||||||||
|
|
||||||||
| import click | ||||||||
|
|
@@ -1128,7 +1129,9 @@ | |||||||
| self._poll_redis() | ||||||||
|
|
||||||||
| async def _async_poll_redis(self) -> None: | ||||||||
| payloads: list = self.redis.pop() | ||||||||
| payloads: t.List[t.Dict] = self.redis.pop( | ||||||||
| settings.REDIS_AUTO_POP_READY_STATE | ||||||||
| ) | ||||||||
| if payloads: | ||||||||
| logger.debug(f"_async_poll_redis: {payloads}") | ||||||||
| self.count["redis"] += len(payloads) | ||||||||
|
|
@@ -1144,34 +1147,44 @@ | |||||||
| while True: | ||||||||
| await self._async_poll_redis() | ||||||||
|
|
||||||||
| def _flush_payloads(self, payloads: list[dict]) -> None: | ||||||||
| if not payloads: | ||||||||
| return | ||||||||
|
|
||||||||
| # group by weight, default=+inf so missing weights pop first | ||||||||
| weight_buckets: t.Dict[float, t.List[t.Dict]] = {} | ||||||||
| for payload in payloads: | ||||||||
| raw = payload.get("weight") | ||||||||
| weight: float = float(raw) if raw is not None else inf | ||||||||
| weight_buckets.setdefault(weight, []).append(payload) | ||||||||
|
|
||||||||
| # push each bucket in descending weight order (highest first) | ||||||||
| for weight, items in sorted( | ||||||||
| weight_buckets.items(), key=lambda kv: -kv[0] | ||||||||
| ): | ||||||||
| logger.debug(f"Pushing {len(items)} items with weight={weight}") | ||||||||
| self.redis.push(items, weight=weight) | ||||||||
|
|
||||||||
| @threaded | ||||||||
| @exception | ||||||||
| def poll_db(self) -> None: | ||||||||
| """ | ||||||||
| Producer which polls Postgres continuously. | ||||||||
|
|
||||||||
| Receive a notification message from the channel we are listening on | ||||||||
| """ | ||||||||
| conn = self.engine.connect().connection | ||||||||
| conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) | ||||||||
| cursor = conn.cursor() | ||||||||
| cursor.execute(f'LISTEN "{self.database}"') | ||||||||
| conn.cursor().execute(f'LISTEN "{self.database}"') | ||||||||
| logger.debug( | ||||||||
| f'Listening to notifications on channel "{self.database}"' | ||||||||
| ) | ||||||||
| payloads: list = [] | ||||||||
|
|
||||||||
| payloads: t.List[t.Dict] = [] | ||||||||
|
|
||||||||
| while True: | ||||||||
| # NB: consider reducing POLL_TIMEOUT to increase throughput | ||||||||
| if select.select([conn], [], [], settings.POLL_TIMEOUT) == ( | ||||||||
| [], | ||||||||
| [], | ||||||||
| [], | ||||||||
| ): | ||||||||
| # Catch any hanging items from the last poll | ||||||||
| if payloads: | ||||||||
| self.redis.push(payloads) | ||||||||
| payloads = [] | ||||||||
| self._flush_payloads(payloads) | ||||||||
|
||||||||
| self._flush_payloads(payloads) | |
| self._flush_payloads(payloads) | |
| payloads = [] |
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.
[nitpick] The condition 'if weight:' does not execute when weight is 0 (the default), which may be unintended. Consider explicitly checking for None or always setting the weight configuration regardless of its value.