Skip to content
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

util: add DelayQueue::deadline #6163

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

aliu
Copy link
Contributor

@aliu aliu commented Nov 21, 2023

Motivation

When implementing a cache with item expirations (example from mini-redis), it's not possible to use DelayQueue as a more efficient backing store for the expirations. The reason is, when inserting/removing/modifying a cache entry there needs to be a way to determine whether the "next expiring time" has changed as a result of the operation. Basically, you'd need an api similar to DelayQueue::peek but for the next deadline in the queue. However, currently DelayQueue does not expose such an api, so you are left with having to store an independent mapping of Keys to expiration Instants and keep it in sync with all operations on the DelayQueue. It would be nice to not have to maintain this bookkeeping when DelayQueue already knows this information.

Solution

Originally, my first thought was to simply make DelayQueue::next_deadline public. However looking closer at the internals, it doesn't consider deadlines in the expired queue. So a user who inserts an already expired item into the queue and calls next_deadline might be surprised when they get None back. This function is used a lot internally as well, so I thought it would be best to not modify its behavior.

Instead we could expose a deadline function that takes a Key and returns its expiration time. This way, we could still get the next deadline via delay_queue.peek().map(|key| delay_queue.deadline(&key)).

@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-time Module: tokio/time labels Nov 23, 2023
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thanks.

@Darksonn Darksonn merged commit a8e8fa6 into tokio-rs:master Nov 23, 2023
76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-time Module: tokio/time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants