diff options
Diffstat (limited to 'transport')
| -rw-r--r-- | transport/channel.go | 23 | ||||
| -rw-r--r-- | transport/local_channel.go | 56 |
2 files changed, 79 insertions, 0 deletions
diff --git a/transport/channel.go b/transport/channel.go new file mode 100644 index 0000000..56d5f2b --- /dev/null +++ b/transport/channel.go | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | package transport | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | |||
| 6 | "flux/core" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type Receiver[T any] interface { | ||
| 10 | // Receive returns the next record, or ok=false once the stream is done or | ||
| 11 | // ctx is cancelled. | ||
| 12 | Receive(ctx context.Context) (core.StreamRecord[T], bool) | ||
| 13 | Close() error | ||
| 14 | } | ||
| 15 | |||
| 16 | // Emitter mirrors core.Emitter plus Close. It is declared separately because | ||
| 17 | // core cannot import transport without an import cycle. | ||
| 18 | type Emitter[T any] interface { | ||
| 19 | // Emit blocks until the record is handed off, or returns ctx.Err() if ctx | ||
| 20 | // is cancelled first. | ||
| 21 | Emit(ctx context.Context, record core.StreamRecord[T]) error | ||
| 22 | Close() error | ||
| 23 | } | ||
diff --git a/transport/local_channel.go b/transport/local_channel.go new file mode 100644 index 0000000..2fac029 --- /dev/null +++ b/transport/local_channel.go | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | package transport | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | |||
| 6 | "flux/core" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type LocalReceiveChannel[T any] struct { | ||
| 10 | recordCh <-chan core.StreamRecord[T] | ||
| 11 | } | ||
| 12 | |||
| 13 | func NewLocalReceiveChannel[T any](ch <-chan core.StreamRecord[T]) *LocalReceiveChannel[T] { | ||
| 14 | return &LocalReceiveChannel[T]{ | ||
| 15 | recordCh: ch, | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | func (c *LocalReceiveChannel[T]) Receive(ctx context.Context) (core.StreamRecord[T], bool) { | ||
| 20 | select { | ||
| 21 | case val, ok := <-c.recordCh: | ||
| 22 | return val, ok | ||
| 23 | case <-ctx.Done(): | ||
| 24 | var zero core.StreamRecord[T] | ||
| 25 | return zero, false | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | func (c *LocalReceiveChannel[T]) Close() error { | ||
| 30 | // The emitter will close | ||
| 31 | return nil | ||
| 32 | } | ||
| 33 | |||
| 34 | type LocalEmitChannel[T any] struct { | ||
| 35 | recordCh chan<- core.StreamRecord[T] | ||
| 36 | } | ||
| 37 | |||
| 38 | func NewLocalEmitChannel[T any](ch chan<- core.StreamRecord[T]) *LocalEmitChannel[T] { | ||
| 39 | return &LocalEmitChannel[T]{ | ||
| 40 | recordCh: ch, | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | func (l *LocalEmitChannel[T]) Emit(ctx context.Context, c core.StreamRecord[T]) error { | ||
| 45 | select { | ||
| 46 | case l.recordCh <- c: | ||
| 47 | return nil | ||
| 48 | case <-ctx.Done(): | ||
| 49 | return ctx.Err() | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | func (l *LocalEmitChannel[T]) Close() error { | ||
| 54 | close(l.recordCh) | ||
| 55 | return nil | ||
| 56 | } | ||
