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

Unbounded queues: add a Close() method. #449

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion exp/mpsc/mpsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ func (tx *Tx[T]) Send(v T) {
tx.tx.Send(v)
}

// Close the queue. Calls to Recv on the other end will return io.EOF.
func (tx *Tx[T]) Close() error {
tx.mu.Lock()
defer tx.mu.Unlock()
return tx.tx.Close()
}

// Receive a message from the queue. Blocks if the queue is empty.
// If the context ends before the receive happens, this returns
// ctx.Err().
// ctx.Err(). If Close is called on the corresponding Tx, this
// returns io.EOF
func (rx *Rx[T]) Recv(ctx context.Context) (T, error) {
return rx.rx.Recv(ctx)
}
Expand Down
17 changes: 15 additions & 2 deletions exp/spsc/spsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package spsc

import (
"context"
"io"
)

const itemsBuffer = 64
Expand Down Expand Up @@ -74,9 +75,17 @@ func (tx *Tx[T]) Send(v T) {
}
}

// Close the queue. Calls to Recv on the other end will return io.EOF.
func (tx *Tx[T]) Close() error {
close(tx.tail.items)
close(tx.tail.next)
return nil
}

// Receive a message from the queue. Blocks if the queue is empty.
// If the context ends before the receive happens, this returns
// ctx.Err().
// ctx.Err(). If Close is called on the corresponding Tx, this
// returns io.EOF
func (rx *Rx[T]) Recv(ctx context.Context) (T, error) {
for {
select {
Expand All @@ -87,7 +96,11 @@ func (rx *Rx[T]) Recv(ctx context.Context) (T, error) {
if ok {
return v, nil
}
rx.head = <-rx.head.next
rx.head, ok = <-rx.head.next
if !ok {
var zero T
return zero, io.EOF
}
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions exp/spsc/spsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spsc

import (
"context"
"io"
"testing"
"time"

Expand Down Expand Up @@ -64,3 +65,40 @@ func TestFillThenDrain(t *testing.T) {
assert.NotNil(t, err)
assert.ErrorIs(t, err, ctx.Err())
}

// Test that we get io.EOF after closing the tx.
func TestClose(t *testing.T) {
t.Parallel()

// try it with various numbers of items in the queue:
cases := []struct {
size int
desc string
}{
{0, "empty"},
{1, "one element"},
{itemsBuffer, "full buffer"},
{itemsBuffer + 1, "overfull buffer"},
}

for _, c := range cases {
t.Run("size: "+c.desc, func(t *testing.T) {
q := New[int]()

for i := 0; i < c.size; i++ {
q.Send(i)
}
q.Close()

ctx := context.Background()
for i := 0; i < c.size; i++ {
v, err := q.Recv(ctx)
assert.NoError(t, err)
assert.Equal(t, i, v)
}

_, err := q.Recv(ctx)
assert.Equal(t, err, io.EOF)
})
}
}