-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
66 lines (50 loc) · 1.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"github.com/resonatehq/gocoro"
"github.com/resonatehq/gocoro/pkg/io"
)
func coroutine(n int) gocoro.CoroutineFunc[func() (string, error), string, string] {
return func(c gocoro.Coroutine[func() (string, error), string, string]) (string, error) {
fmt.Println("coroutine:", n)
if n == 0 {
return "", nil
}
fooPromise := gocoro.Yield(c, func() (string, error) {
return fmt.Sprintf("foo.%d", n), nil
})
barPromise := gocoro.Yield(c, func() (string, error) {
return fmt.Sprintf("bar.%d", n), nil
})
bazPromise := gocoro.Spawn(c, coroutine(n-1))
foo, _ := gocoro.Await(c, fooPromise)
bar, _ := gocoro.Await(c, barPromise)
baz, _ := gocoro.Await(c, bazPromise)
return fmt.Sprintf("%s:%s:%s", foo, bar, baz), nil
}
}
func main() {
// instantiate io
fio := io.NewFIO[string](100)
// start io worker on a goroutine
go fio.Worker()
// instantiate scheduler
scheduler := gocoro.New(fio, 100)
// add coroutine to scheduler
promise, _ := gocoro.Add(scheduler, coroutine(3))
// run scheduler until blocked repeatedly
for scheduler.Size() > 0 {
for _, cqe := range fio.Dequeue(3) {
cqe.Callback(cqe.Value, cqe.Error)
}
scheduler.RunUntilBlocked(0)
}
// shutdown scheduler
scheduler.Shutdown()
// await and print result
if v, e := promise.Await(); e != nil {
fmt.Println("error:", e)
} else {
fmt.Println("value:", v)
}
}