aboutsummaryrefslogtreecommitdiff
path: root/transport
diff options
context:
space:
mode:
Diffstat (limited to 'transport')
-rw-r--r--transport/channel.go23
-rw-r--r--transport/local_channel.go56
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 @@
1package transport
2
3import (
4 "context"
5
6 "flux/core"
7)
8
9type 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.
18type 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 @@
1package transport
2
3import (
4 "context"
5
6 "flux/core"
7)
8
9type LocalReceiveChannel[T any] struct {
10 recordCh <-chan core.StreamRecord[T]
11}
12
13func NewLocalReceiveChannel[T any](ch <-chan core.StreamRecord[T]) *LocalReceiveChannel[T] {
14 return &LocalReceiveChannel[T]{
15 recordCh: ch,
16 }
17}
18
19func (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
29func (c *LocalReceiveChannel[T]) Close() error {
30 // The emitter will close
31 return nil
32}
33
34type LocalEmitChannel[T any] struct {
35 recordCh chan<- core.StreamRecord[T]
36}
37
38func NewLocalEmitChannel[T any](ch chan<- core.StreamRecord[T]) *LocalEmitChannel[T] {
39 return &LocalEmitChannel[T]{
40 recordCh: ch,
41 }
42}
43
44func (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
53func (l *LocalEmitChannel[T]) Close() error {
54 close(l.recordCh)
55 return nil
56}