Skip to content

improved Queue shutdown functionality #9844

@mberndt123

Description

@mberndt123

Hey, I've recently proposed something on Discord, and since feedback has been rather positive, I'm making a ticket to track the idea.

I've been working with Queues recently, and I've been having some issues around shutdown that I would like to address.

Specifically, I find it a common pattern that I send some kind of request object through a queue because I want another fiber to perform some action on my behalf. Along with the request, I send a Promise to have that fiber communicate the outcome of that action to me. By and large this works fine. The issue arises when the fiber that I'm sending requests to fails. In that case, I would like it to communicate the cause of the failure back to the other fibers. This is easy enough for the requests that I've already pulled out of the queue: I simply fail those promises.
But I also need to deal with other cases: fibers currently blocked in an offer call, future attempts to offer to the queue, and I also need to deal with requests that have been submitted to the queue but not yet retrieved.

So my idea is as follows:

  • add an E type parameter to Queue
  • add a shutdownCause method that takes a type parameter of type Cause[E]
  • shutdownCause would also return the items currently buffered in the queue in order to dispose of them
  • after shutdownCause has been called, any attempt to interact with the queue will fail with the cause
  • methods like take, offer etc. should indicate errors of type E
  • streams created with ZStream.fromQueue would also fail with this cause
  • shutdownCause should be atomic: when multiple fibers call it at the same time, one of them wins and the others fail with the cause supplied by the winner

Afaik, adding a new method is a binary compatible change, as is adding a new type parameter. Hence I think this is a source incompatible but binary compatible change. @ghostdogpr therefore suggested it could be added in a ZIO 2.2 release.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions