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

Proposal: ChannelWriter<T>.DroppedWrites counter #32465

Closed
declspec opened this issue Feb 18, 2020 · 7 comments
Closed

Proposal: ChannelWriter<T>.DroppedWrites counter #32465

declspec opened this issue Feb 18, 2020 · 7 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Channels
Milestone

Comments

@declspec
Copy link

declspec commented Feb 18, 2020

As far as I'm aware, when operating a BoundedChannel<T> with a BoundedChannelFullMode of any of the Drop* variants there is no way to tell when and how many writes start being dropped. In all cases the call to TryWrite will succeed and the producer will be none the wiser.

It would be nice feature to have a DroppedWrites counter (or something that performs a similar function) that increments whenever a channel drops a pending write from the queue. Something like the following:

var channel = Channel.CreateBounded<object>(new BoundedChannelOptions(10) {
    FullMode = BoundedChannelFullMode.DropOldest
});

for (var i = 0; i < 20; ++i) {
    Debug.Assert(channel.Writer.TryWrite((object)i));
}

Debug.WriteLine(channel.Writer.DroppedWrites); // should output '10' at this point.
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Feb 18, 2020
@AaronRobinsonMSFT AaronRobinsonMSFT added area-System.Threading.Channels api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Feb 18, 2020
@stephentoub stephentoub removed the untriaged New issue has not been triaged by the area owner label Feb 25, 2020
@stephentoub stephentoub added this to the Future milestone Feb 25, 2020
@matttrawicki
Copy link

👍 I find the Channels very intriguing but unusable as implemented. I need to know when the channel is full and writes are being blocked/dropped. Even a simple Count property would be a useful step.

@stephentoub
Copy link
Member

Even a simple Count property would be a useful step.

That now exists: #312

@matttrawicki
Copy link

@stephentoub Thank you! I really did try to look before asking, but I missed it. 😥

@declspec
Copy link
Author

declspec commented Mar 5, 2020

As an alternative maybe an overload to TryWrite that adds an out parameter for communicating the action that was taken, something along the lines of:

bool TryWrite(T data, out BoundedChannelFullAction fullAction);

public enum BoundedChannelFullAction {
    None, // Channel isn't full yet
    Wait, // When BoundedChannelFullMode.Wait was specified, this could actually just be merged into `DroppedWrite` as the return value from `TryWrite` would disambiguate them
    DroppedWrite, // When BoundedChannelFullMode.DropWrite was specified
    DroppedOldest, // When BoundedChannelFullMode.DropOldest was specified
    DroppedNewest // When BoundedChannelFullModel.DropNewest was specified
}

Even just writing that out I'm not sure if it's a very nice API, but it would be nice to be able to get some extra info out of the TryWrite call, given that we can control its behaviour with BoundedChannelFullMode.

Or, without an out parameter:

public BoundedChannelWriteResult Write(T data);

public bool TryWrite(T data) {
    return Write(data) != BoundedChannelWriteResult.Failed;
}

public enum BoundedChannelWriteResult {
    Failed,
    Written,
    DroppedWrite,
    DroppedOldest,
    DroppedNewest
}

@springy76
Copy link

I don't see any counters on netcore 3.1.4

@stephentoub
Copy link
Member

I'm going to close this as a dup of #36522. Such a callback can trivially be used to implement a counter if that's what's needed. Thanks!

@declspec
Copy link
Author

declspec commented Mar 2, 2021

@stephentoub Yep that'll work perfectly, thanks.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Channels
Projects
None yet
Development

No branches or pull requests

6 participants