Skip to content

Drain channel immediately after last receiver is dropped #23

Open
@akhilles

Description

@akhilles

After the last receiver is dropped, all the items in the channel are effectively abandoned and can never be accessed. In this scenario, I think it's appropriate to drain the channel and drop all the items. One use case where this is relevant is a dispatch mechanism that uses oneshot channels:

let (req_s, req_r) = bounded(10);

// dispatch
for i in 0.. {
    let (s, r) = oneshot();
    req_s.send((i, s)).await.unwrap();
    let res = r.recv().await.unwrap();
}

// worker
while let Ok((i, s)) = req_r.recv().await {
    s.send(do_work(i));
}

Here, if worker terminates while there are still requests in the channel, dispatch can end up waiting for the response forever. This can be addressed with timeouts, but I think automatically draining the channel is a better solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions