Skip to content

Zero-capacity channel - #1108

Merged
fwbrasil merged 10 commits into
getkyo:mainfrom
johnhungerford:zero-capacity-channel
Apr 3, 2025
Merged

fwbrasil merged 10 commits into
getkyo:mainfrom
johnhungerford:zero-capacity-channel

Conversation

@johnhungerford

Copy link
Copy Markdown
Collaborator

Addresses #987

Problem

Zero-capacity channels did not function properly since the default implementation expected to be able to offer to and pull from the intermediate queue directly.

Solution

Updated Channel.Unsafe.init to return a separate implementation of Channel.Unsafe when _capacity == 0. This new implementation has no queue. It's behavior is subtly different in the following ways:

  1. Channel closure is tracked by an atomic boolean instead of being tied to the underlying queue (which doesn't exist)
  2. Offer and poll interacts with the puts and takes queues directly
  3. flush() handles only two cases: (a) channel is closed, and (b) puts and takes are both non-empty

Notes

Comment on lines +423 to +424
def empty()(using AllowUnsafe) = result(true)
def full()(using AllowUnsafe) = result(true)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫨

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct though, isn't it?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is, yes. Just a bit ironic, as I generally would expect empty != full

if result.contains(true) then flush()
result
end offer
def offer(value: A)(using AllowUnsafe) = result(false)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this behavior isn't ideal because there can be a pending take that could be satisfied. It seems we could just attempt to get a pending take from the queue?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right -- I meant to do that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I added this update (along with others -- see resolved conversations). This is now breaking a bunch of tests. Do we want to revert or change tests to expect different behavior for the zero-capacity case?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on the behavior changes that break the tests?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current implementation of ZeroCapacityChannel#close always returns an empty backlog, since there's no queue, but since it's offer behavior is different, the accounting that's used in tests for comparing the backlog to what's been offered doesn't translate.

For instance, the test "offer and close" offers a bunch of values and expects to get everything that was successfully offered back when calling close(). A zero-capacity channel can successfully offer them, but returns none of them on close.

Basically any test that asserts on backlogs from close() either fail or are flaky. Some Hub tests are failing now too.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems just a test issue? The behavior sounds ok to me

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made backlog assertions conditional on channel size.

discard(promise.complete(Result.unit))
Absent
case Present(value) =>
if batch.tail.nonEmpty then discard(puts.offer(Put.Batch(batch.tail, promise)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can break fairness since the batched put goes back to the end of the queue. It's pre-existing so we could follow up separately

def loop(current: Chunk[A]): Result[Closed, Chunk[A]] =
val next = queue.drain()
final class NonZeroCapacityUnsafe[A](
override val capacity: Int,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to specify the size of the queue

@johnhungerford

Copy link
Copy Markdown
Collaborator Author

I think the latest updates address all PR review comments

@fwbrasil fwbrasil left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM besides the couple of comments

@fwbrasil
fwbrasil merged commit 829bb4f into getkyo:main Apr 3, 2025
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.

3 participants