# DelayQueue implementation

**URL:** <https://forums.swift.org/t/delayqueue-implementation/65549>\
**Category:** Collections\
**Tags:** concurrency\
**Created:** [June 13, 2023, 5:02pm UTC](https://forums.swift.org/t/delayqueue-implementation/65549 "2023-06-13T17:02:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tstromberg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tstromberg/32/18093_2.png) [@tstromberg](https://forums.swift.org/u/tstromberg)\
**Post date:** [June 13, 2023, 5:02pm UTC](https://forums.swift.org/t/delayqueue-implementation/65549/1 "2023-06-13T17:02:33Z")

</div>

My team at work is in the process of converting some code over to Swift concurrency, and we find ourselves needing a replacement for our `RetryQueue`, which leverages `Timer`s and a `PriorityQueue` to retry work items after a specified delay. Something like `DelayQueue` from Java seems ideal for this use case. Does anyone have thoughts on how to build something like that in Swift?

My current thinking is to use `AsyncStream` to pass work items ready to be retried to consumers and `Task.sleep` to delay. I'm getting stuck trying to understand the best way to reset the logic when a new item gets enqueued.

---

<div class="post-metadata">

**Author:** ![lorentey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lorentey/32/1299_2.png) [@lorentey](https://forums.swift.org/u/lorentey)\
**Post date:** [June 26, 2023, 9:58pm UTC](https://forums.swift.org/t/delayqueue-implementation/65549/3 "2023-06-26T21:58:44Z")

</div>

This would probably not be the best fit for the swift-collections package, because it is mostly for publishing classic synchronous data structure implementations -- which is already a quite large domain to cover. Adding concurrent/async data structures would dramatically increase the scope of the package (and reduce its focus).

On the other hand, I think there would be room for one or more new "Standard Library-adjacent" packages dedicated to this general topic, though. Most of that work is currently blocked on properly modeling synchronization primitives in Swift, though.
