Skip to content

Commit 29ee2e2

Browse files
committed
Add README for cmd/samples/recipes/branch
Signed-off-by: Diana Zawadzki <[email protected]>
1 parent ee28973 commit 29ee2e2

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Branch Sample
2+
3+
This sample demonstrates **parallel execution** - running multiple activities concurrently using two different approaches.
4+
5+
> **Looking for a visual guide?** See [new_samples/hello_world](../../../../new_samples/hello_world/) for a step-by-step tutorial with screenshots.
6+
7+
## Two Approaches to Parallelism
8+
9+
### 1. Futures (`-c branch`)
10+
Execute activities in parallel, collect futures, then wait for all:
11+
12+
```
13+
┌─────────────┐
14+
│ Start │
15+
└──────┬──────┘
16+
┌────────┼────────┐
17+
▼ ▼ ▼
18+
┌─────┐ ┌─────┐ ┌─────┐
19+
│ A1 │ │ A2 │ │ A3 │ (parallel)
20+
└──┬──┘ └──┬──┘ └──┬──┘
21+
└────────┼────────┘
22+
23+
┌─────────────┐
24+
│ Wait all │
25+
└─────────────┘
26+
```
27+
28+
### 2. Coroutines (`workflow.Go`)
29+
Spawn goroutine-like coroutines with channels for coordination:
30+
31+
```
32+
┌─────────────┐
33+
│ Start │
34+
└──────┬──────┘
35+
┌───┴───┐
36+
▼ ▼
37+
┌─────────┐ ┌─────┐
38+
│ A1 → A2 │ │ A3 │ (parallel branches)
39+
└────┬────┘ └──┬──┘
40+
└────┬────┘
41+
42+
┌─────────────┐
43+
│ channel.Receive │
44+
└─────────────┘
45+
```
46+
47+
## Prerequisites
48+
49+
1. Cadence server running (see [main README](../../../../README.md))
50+
2. Build the samples: `make`
51+
52+
## Running the Sample
53+
54+
```bash
55+
# Terminal 1: Start worker
56+
./bin/branch -m worker
57+
58+
# Terminal 2: Trigger workflows
59+
./bin/branch -m trigger # Coroutines approach (default)
60+
./bin/branch -m trigger -c branch # Futures approach
61+
```
62+
63+
## Key Code
64+
65+
### Futures Pattern
66+
```go
67+
var futures []workflow.Future
68+
for i := 1; i <= totalBranches; i++ {
69+
future := workflow.ExecuteActivity(ctx, sampleActivity, input)
70+
futures = append(futures, future)
71+
}
72+
// Wait for all
73+
for _, future := range futures {
74+
future.Get(ctx, nil)
75+
}
76+
```
77+
78+
### Coroutines Pattern
79+
```go
80+
waitChannel := workflow.NewChannel(ctx)
81+
82+
workflow.Go(ctx, func(ctx workflow.Context) {
83+
workflow.ExecuteActivity(ctx, activity1).Get(ctx, nil)
84+
workflow.ExecuteActivity(ctx, activity2).Get(ctx, nil)
85+
waitChannel.Send(ctx, "done")
86+
})
87+
88+
workflow.Go(ctx, func(ctx workflow.Context) {
89+
workflow.ExecuteActivity(ctx, activity3).Get(ctx, nil)
90+
waitChannel.Send(ctx, "done")
91+
})
92+
93+
// Wait for both coroutines
94+
waitChannel.Receive(ctx, nil)
95+
waitChannel.Receive(ctx, nil)
96+
```
97+
98+
## Testing
99+
100+
```bash
101+
go test -v ./cmd/samples/recipes/branch/
102+
```
103+
104+
## References
105+
106+
- [Parallel Execution](https://cadenceworkflow.io/docs/go-client/execute-activity/)
107+
- [Workflow Coroutines](https://cadenceworkflow.io/docs/go-client/workflow-patterns/)
108+

0 commit comments

Comments
 (0)