Skip to content

Add Read cancellation#121

Open
kroppt wants to merge 2 commits into
bugst:masterfrom
kroppt:master
Open

Add Read cancellation#121
kroppt wants to merge 2 commits into
bugst:masterfrom
kroppt:master

Conversation

@kroppt

@kroppt kroppt commented Sep 22, 2021

Copy link
Copy Markdown

Fixes #105
Related to #117

In this solution, I gave serial.Serial a new function called ReadContext, which is identical to Read, except it also takes in a context.Context as its first parameter.

I understand that there may be some concern over the API, given that Go developers as a whole have not decided on how to resolve the problem of cancellation for abstract file systems, but it seems OK to add this feature, because the interface is defined in this library and is not a generalized interface that is a part of the standard library. I am happy to talk through any concerns.

I tested this on both Linux and Windows.
It probably also works for other Unix platforms, since their implementation is the same as Linux.

I cleaned up the Linux tests, since they weren't very careful.
Additionally, I added Windows tests, which were a bit trickier. I didn't see an obvious way to create a dummy serial port, so the tests will run using the first serial port on the system, otherwise skipping the tests.

@soypat

soypat commented Feb 18, 2024

Copy link
Copy Markdown

I'll note that this could best be solved by an external library that implements cancellation for any kind of io.Reader implementation. The way I've gone about dealing with time-limited readers is by using this API: https://github.com/soypat/cereal/blob/main/nonblocking.go

@kroppt

kroppt commented Mar 8, 2024

Copy link
Copy Markdown
Author

I'll note that this could best be solved by an external library that implements cancellation for any kind of io.Reader implementation. The way I've gone about dealing with time-limited readers is by using this API: https://github.com/soypat/cereal/blob/main/nonblocking.go

It looks like it creates a long-running goroutine that handles reading and then buffers it for the primary code to read. I think that's a good approach. If I recall, I might have done something like this in the code I was writing. If you use channels to pass along those bytes, it seamlessly integrates with select semantics from the cancellation channel (which avoids having to poll). And if you need it buffered potentially infinitely, you can insert an intermediate goroutine that just buffers the incoming bytes. Boy, this is getting complicated! But I remember it worked well for me.

@KiddoV

KiddoV commented Mar 19, 2024

Copy link
Copy Markdown

I am interested in this feature too, as sometimes I need to stop the reading manually instead of waiting for an error or the received byte = 0.

@ccollins476ad

Copy link
Copy Markdown

This is a very useful addition. Not sure whom to direct this question to, but are there any plans to merge this? The merge conflicts don't look too bad.

@kroppt

kroppt commented May 21, 2024

Copy link
Copy Markdown
Author

@ccollins476ad

This is a very useful addition. Not sure whom to direct this question to, but are there any plans to merge this? The merge conflicts don't look too bad.

Many changes have been made to the repository since this pull request has been opened. It seems that it won't ever be looked at. Plan accordingly. See the above alternatives. I'm happy to help if further clarification is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add cancellation

4 participants