diff options
| author | SangTran-127 <tranquangsang12.7@gmail.com> | 2026-08-08 17:24:01 +0700 |
|---|---|---|
| committer | SangTran-127 <tranquangsang12.7@gmail.com> | 2026-08-08 17:24:01 +0700 |
| commit | 0958e46c3651f2c8b6d76bcffbd8820063647d59 (patch) | |
| tree | 1e917ed409eddc0b3c29fd602e551778c96c8dc6 /transport | |
| parent | acc0b6fbaace61a3befd7f726f9d80fb69391126 (diff) | |
| download | goflink-0958e46c3651f2c8b6d76bcffbd8820063647d59.tar.gz goflink-0958e46c3651f2c8b6d76bcffbd8820063647d59.zip | |
refactor: add context support to transport operators and runtime task management
Diffstat (limited to 'transport')
| -rw-r--r-- | transport/channel.go | 16 | ||||
| -rw-r--r-- | transport/local_channel.go | 35 |
2 files changed, 40 insertions, 11 deletions
diff --git a/transport/channel.go b/transport/channel.go index 011f8ae..1db6ea8 100644 --- a/transport/channel.go +++ b/transport/channel.go @@ -1,13 +1,23 @@ package transport -import "goflink/core" +import ( + "context" + + "goflink/core" +) type Receiver[T any] interface { - Receive() (core.StreamRecord[T], bool) + // Receive returns the next record, or ok=false once the stream is done or + // ctx is cancelled. + Receive(ctx context.Context) (core.StreamRecord[T], bool) Close() error } +// Emitter mirrors core.Emitter plus Close. It is declared separately because +// core cannot import transport without an import cycle. type Emitter[T any] interface { - Emit(core.StreamRecord[T]) error + // Emit blocks until the record is handed off, or returns ctx.Err() if ctx + // is cancelled first. + Emit(ctx context.Context, record core.StreamRecord[T]) error Close() error } diff --git a/transport/local_channel.go b/transport/local_channel.go index 4ac2c83..e3e4cba 100644 --- a/transport/local_channel.go +++ b/transport/local_channel.go @@ -1,6 +1,10 @@ package transport -import "goflink/core" +import ( + "context" + + "goflink/core" +) type LocalReceiveChannel[T any] struct { recordCh <-chan core.StreamRecord[T] @@ -12,9 +16,14 @@ func NewLocalReceiveChannel[T any](ch <-chan core.StreamRecord[T]) *LocalReceive } } -func (c *LocalReceiveChannel[T]) Receive() (core.StreamRecord[T], bool) { - val, ok := <-c.recordCh - return val, ok +func (c *LocalReceiveChannel[T]) Receive(ctx context.Context) (core.StreamRecord[T], bool) { + select { + case val, ok := <-c.recordCh: + return val, ok + case <-ctx.Done(): + var zero core.StreamRecord[T] + return zero, false + } } func (c *LocalReceiveChannel[T]) Close() error { @@ -26,12 +35,22 @@ type LocalEmitChannel[T any] struct { recordCh chan<- core.StreamRecord[T] } -func (l LocalEmitChannel[T]) Emit(c core.StreamRecord[T]) error { - l.recordCh <- c - return nil +func NewLocalEmitChannel[T any](ch chan<- core.StreamRecord[T]) *LocalEmitChannel[T] { + return &LocalEmitChannel[T]{ + recordCh: ch, + } +} + +func (l *LocalEmitChannel[T]) Emit(ctx context.Context, c core.StreamRecord[T]) error { + select { + case l.recordCh <- c: + return nil + case <-ctx.Done(): + return ctx.Err() + } } -func (l LocalEmitChannel[T]) Close() error { +func (l *LocalEmitChannel[T]) Close() error { close(l.recordCh) return nil } |