-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ea3c94
commit 621bee3
Showing
8 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Wait Node | ||
|
||
**The Wait Node** introduces a delay in packet processing, allowing for timed pauses in workflows. This node is useful for pacing operations or waiting for external conditions before continuing to process data. | ||
|
||
## Specification | ||
|
||
- **interval**: Defines the duration (in milliseconds or a Go `time.Duration` format) for which the node will delay before passing the input packet to the output. | ||
|
||
## Ports | ||
|
||
- **in**: Receives the input packet and initiates a delay. | ||
- **out**: Outputs the original input packet after the specified delay. | ||
|
||
## Example | ||
|
||
```yaml | ||
- kind: wait | ||
interval: 2000 # Delay of 2 seconds | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Wait 노드 | ||
|
||
**Wait 노드**는 패킷 처리에 지연을 추가하여 워크플로우에서 일정한 대기 시간을 설정할 수 있습니다. 이 노드는 작업의 속도를 조절하거나 외부 조건을 기다려야 할 때 유용합니다. | ||
|
||
## 명세 | ||
|
||
- **interval**: 입력 패킷을 출력하기 전까지 지연할 시간을 정의합니다. 밀리초 단위 또는 Go의 `time.Duration` 형식으로 설정할 수 있습니다. | ||
|
||
## 포트 | ||
|
||
- **in**: 입력 패킷을 받아 지연을 시작합니다. | ||
- **out**: 지정된 지연 시간 후에 원래의 입력 패킷을 출력합니다. | ||
|
||
## 예시 | ||
|
||
```yaml | ||
- kind: wait | ||
interval: 2000 # 2초 지연 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package control | ||
|
||
import ( | ||
"github.com/siyul-park/uniflow/pkg/node" | ||
"github.com/siyul-park/uniflow/pkg/packet" | ||
"github.com/siyul-park/uniflow/pkg/process" | ||
"github.com/siyul-park/uniflow/pkg/scheme" | ||
"github.com/siyul-park/uniflow/pkg/spec" | ||
"time" | ||
) | ||
|
||
// WaitNodeSpec defines the configuration for WaitNode, including a delay duration. | ||
type WaitNodeSpec struct { | ||
spec.Meta `map:",inline"` | ||
Interval time.Duration `map:"timeout"` | ||
} | ||
|
||
// WaitNode adds a delay to packet processing, using a specified interval. | ||
type WaitNode struct { | ||
*node.OneToOneNode | ||
interval time.Duration | ||
} | ||
|
||
const KindWait = "wait" | ||
|
||
// NewWaitNodeCodec creates a codec to build WaitNode from WaitNodeSpec. | ||
func NewWaitNodeCodec() scheme.Codec { | ||
return scheme.CodecWithType(func(spec *WaitNodeSpec) (node.Node, error) { | ||
return NewWaitNode(spec.Interval), nil | ||
}) | ||
} | ||
|
||
// NewWaitNode creates a WaitNode with the given delay interval. | ||
func NewWaitNode(interval time.Duration) *WaitNode { | ||
n := &WaitNode{interval: interval} | ||
n.OneToOneNode = node.NewOneToOneNode(n.action) | ||
return n | ||
} | ||
|
||
func (n *WaitNode) action(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) { | ||
time.Sleep(n.interval) | ||
return inPck, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package control | ||
|
||
import ( | ||
"context" | ||
"github.com/go-faker/faker/v4" | ||
"github.com/siyul-park/uniflow/pkg/node" | ||
"github.com/siyul-park/uniflow/pkg/packet" | ||
"github.com/siyul-park/uniflow/pkg/port" | ||
"github.com/siyul-park/uniflow/pkg/process" | ||
"github.com/siyul-park/uniflow/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestWaitNodeCodec_Decode(t *testing.T) { | ||
codec := NewWaitNodeCodec() | ||
|
||
spec := &WaitNodeSpec{ | ||
Interval: 0, | ||
} | ||
|
||
n, err := codec.Compile(spec) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, n) | ||
assert.NoError(t, n.Close()) | ||
} | ||
|
||
func TestNewWaitNode(t *testing.T) { | ||
n := NewWaitNode(0) | ||
assert.NotNil(t, n) | ||
assert.NoError(t, n.Close()) | ||
} | ||
|
||
func TestWaitNode_SendAndReceive(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second) | ||
defer cancel() | ||
|
||
n := NewWaitNode(0) | ||
defer n.Close() | ||
|
||
in := port.NewOut() | ||
in.Link(n.In(node.PortIn)) | ||
|
||
proc := process.New() | ||
defer proc.Exit(nil) | ||
|
||
inWriter := in.Open(proc) | ||
|
||
inPayload := types.NewString(faker.Word()) | ||
inPck := packet.New(inPayload) | ||
|
||
inWriter.Write(inPck) | ||
|
||
select { | ||
case outPck := <-inWriter.Receive(): | ||
assert.Equal(t, inPayload, outPck.Payload()) | ||
case <-ctx.Done(): | ||
assert.Fail(t, ctx.Err().Error()) | ||
} | ||
} | ||
|
||
func BenchmarkWaitNode_SendAndReceive(b *testing.B) { | ||
n := NewWaitNode(0) | ||
defer n.Close() | ||
|
||
in := port.NewOut() | ||
in.Link(n.In(node.PortIn)) | ||
|
||
proc := process.New() | ||
defer proc.Exit(nil) | ||
|
||
inWriter := in.Open(proc) | ||
|
||
var inPayload types.Value | ||
inPck := packet.New(inPayload) | ||
|
||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
inWriter.Write(inPck) | ||
<-inWriter.Receive() | ||
} | ||
} |