Skip to content

Commit 9f2fda0

Browse files
Add documentation for Selector (#1115)
1 parent b09692f commit 9f2fda0

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

internal/workflow.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,64 @@ type (
7474
Close()
7575
}
7676

77-
// Selector must be used instead of native go select by workflow code.
77+
// Selector must be used instead of native go select by workflow code for determinism.
7878
// Use workflow.NewSelector(ctx) method to create a Selector instance.
79+
// The interface is to simulate Golang's Select statement.
80+
// For example, the logic of Golang code like below
81+
// chA := make(chan int)
82+
// chB := make(chan int)
83+
// counter := 0
84+
// for {
85+
// select {
86+
// case i, ok := <- chA:
87+
// if ok{
88+
// counter += i
89+
// }
90+
// case i, ok := <- chB:
91+
// if ok{
92+
// counter += i
93+
// }
94+
// }
95+
// }
96+
// should be written as
97+
// s := workflow.NewSelector(ctx)
98+
// counter := 0
99+
// s.AddReceive(workflow.GetSignalChannel(ctx, "channelA"), func(c workflow.Channel, ok bool) {
100+
// if ok{
101+
// var i int
102+
// c.Receive(ctx, &i)
103+
// counter += i
104+
// }
105+
// })
106+
// s.AddReceive(workflow.GetSignalChannel(ctx, "channelB"), func(c workflow.Channel, ok bool) {
107+
// if ok{
108+
// var i int
109+
// c.Receive(ctx, &i)
110+
// counter += i
111+
// }
112+
// })
113+
//
114+
// for {
115+
// s.Select(ctx)
116+
// }
79117
Selector interface {
80-
AddReceive(c Channel, f func(c Channel, more bool)) Selector
118+
// AddReceive adds a ReceiveChannel to the selector. f is invoked when the channel has data or closed.
119+
// ok == false indicates the channel is closed
120+
AddReceive(c Channel, f func(c Channel, ok bool)) Selector
121+
// AddSend adds a SendChannel to the selector. f is invoke when the channel is available to send
81122
AddSend(c Channel, v interface{}, f func()) Selector
123+
// AddFuture adds a Future to the selector f is invoked when future is ready
82124
AddFuture(future Future, f func(f Future)) Selector
125+
// AddDefault adds a default branch to the selector.
126+
// f is invoked when non of the other conditions(ReceiveChannel, SendChannel and Future) is met for one call of Select
83127
AddDefault(f func())
128+
// Select waits for one of the added conditions to be met and invoke the callback as described above.
129+
// When none of the added condition is met:
130+
// if there is no Default(added by AddDefault) and , then it will block the current goroutine
131+
// if Default(added by AddDefault) is used, when Default callback will be executed without blocking
132+
// When more than one of added conditions are met, only one of them will be invoked.
133+
// Usually it's recommended to use a for loop to drain all of them, and use AddDefault to break out the
134+
// loop properly(e.g. not missing any received data in channels)
84135
Select(ctx Context)
85136
}
86137

0 commit comments

Comments
 (0)