Skip to content

Commit 4d1f011

Browse files
committed
docs: enhance and refactor package examples
1 parent 18f0d8a commit 4d1f011

File tree

4 files changed

+163
-24
lines changed

4 files changed

+163
-24
lines changed

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
goroutine/goroutine
22
future/future
3+
cyclic_barrier/cyclic_barrier

examples/cyclic_barrier/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"sync"
7+
"time"
8+
9+
"github.com/reugn/async"
10+
)
11+
12+
func main() {
13+
fmt.Println("=== CyclicBarrier Example ===")
14+
barrier := async.NewCyclicBarrier(3)
15+
var wg sync.WaitGroup
16+
17+
for i := 1; i <= 3; i++ {
18+
wg.Add(1)
19+
go func(id int) {
20+
defer wg.Done()
21+
fmt.Printf("Goroutine %d: starting work\n", id)
22+
time.Sleep(time.Duration(id) * 100 * time.Millisecond)
23+
fmt.Printf("Goroutine %d: reached barrier\n", id)
24+
if err := barrier.Await(); err != nil {
25+
fmt.Printf("Goroutine %d: barrier error: %v\n", id, err)
26+
return
27+
}
28+
fmt.Printf("Goroutine %d: passed barrier\n", id)
29+
}(i)
30+
}
31+
32+
wg.Wait()
33+
fmt.Println("All goroutines completed")
34+
35+
// Example with context
36+
fmt.Println("\n=== CyclicBarrier with Context ===")
37+
barrier2 := async.NewCyclicBarrier(2)
38+
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
39+
defer cancel()
40+
41+
wg.Add(2)
42+
43+
go func() {
44+
defer wg.Done()
45+
time.Sleep(100 * time.Millisecond)
46+
if err := barrier2.AwaitContext(ctx); err != nil {
47+
fmt.Printf("Goroutine 1: %v\n", err)
48+
}
49+
}()
50+
51+
go func() {
52+
defer wg.Done()
53+
if err := barrier2.AwaitContext(ctx); err != nil {
54+
fmt.Printf("Goroutine 2: %v\n", err)
55+
}
56+
}()
57+
58+
wg.Wait()
59+
}

examples/future/main.go

Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,128 @@ package main
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"log"
68
"time"
79

810
"github.com/reugn/async"
11+
"github.com/reugn/async/internal/ptr"
912
)
1013

11-
const ok = "OK"
12-
1314
func main() {
14-
// using a promise
15-
future1 := asyncAction()
16-
result1, err := future1.Join()
15+
runPromiseExample()
16+
runTaskExample()
17+
runExecutorExample()
18+
runTransformationsExample()
19+
runRecoveryExample()
20+
}
21+
22+
func runPromiseExample() {
23+
fmt.Println("=== Promise Example ===")
24+
future := asyncAction()
25+
result, err := future.Join()
1726
if err != nil {
1827
log.Fatal(err)
1928
}
20-
log.Print(result1)
29+
fmt.Printf("Result: %s\n", result)
30+
}
2131

22-
// using a task
23-
task := async.NewTask(func() (string, error) { return ok, nil })
24-
result2, err := task.Call().Join()
32+
func runTaskExample() {
33+
fmt.Println("\n=== Task Example ===")
34+
task := async.NewTask(func() (string, error) {
35+
time.Sleep(100 * time.Millisecond)
36+
return "Task completed", nil
37+
})
38+
result, err := task.Call().Join()
2539
if err != nil {
2640
log.Fatal(err)
2741
}
28-
log.Print(result2)
42+
fmt.Printf("Result: %s\n", result)
43+
}
2944

30-
// using the executor
45+
func runExecutorExample() {
46+
fmt.Println("\n=== Executor Example ===")
3147
ctx := context.Background()
3248
executor := async.NewExecutor[*string](ctx, async.NewExecutorConfig(2, 2))
3349

34-
future3, err := executor.Submit(func(_ context.Context) (*string, error) {
35-
value := ok
36-
return &value, nil
50+
future, err := executor.Submit(func(_ context.Context) (*string, error) {
51+
value := "Executor task completed"
52+
return ptr.Of(value), nil
3753
})
54+
if err != nil {
55+
if shutdownErr := executor.Shutdown(); shutdownErr != nil {
56+
log.Printf("Error shutting down executor: %v", shutdownErr)
57+
}
58+
log.Fatal(err)
59+
}
60+
61+
result, err := future.Get(ctx)
62+
if err != nil {
63+
if shutdownErr := executor.Shutdown(); shutdownErr != nil {
64+
log.Printf("Error shutting down executor: %v", shutdownErr)
65+
}
66+
log.Fatal(err)
67+
}
68+
fmt.Printf("Result: %s\n", *result)
69+
70+
if err := executor.Shutdown(); err != nil {
71+
log.Printf("Error shutting down executor: %v", err)
72+
}
73+
}
74+
75+
func runTransformationsExample() {
76+
fmt.Println("\n=== Future Transformations ===")
77+
promise := async.NewPromise[int]()
78+
go func() {
79+
time.Sleep(50 * time.Millisecond)
80+
promise.Success(10)
81+
}()
82+
83+
transformed := promise.Future().
84+
Map(func(v int) (int, error) {
85+
return v * 2, nil
86+
}).
87+
FlatMap(func(v int) (async.Future[int], error) {
88+
p := async.NewPromise[int]()
89+
go func() {
90+
time.Sleep(50 * time.Millisecond)
91+
p.Success(v + 5)
92+
}()
93+
return p.Future(), nil
94+
})
95+
96+
result, err := transformed.Join()
3897
if err != nil {
3998
log.Fatal(err)
4099
}
100+
fmt.Printf("Transformed result: %d\n", result)
101+
}
102+
103+
func runRecoveryExample() {
104+
fmt.Println("\n=== Error Recovery ===")
105+
failingPromise := async.NewPromise[int]()
106+
go func() {
107+
time.Sleep(50 * time.Millisecond)
108+
failingPromise.Failure(errors.New("operation failed"))
109+
}()
41110

42-
result3, err := future3.Get(ctx)
111+
recovered := failingPromise.Future().Recover(func() (int, error) {
112+
return 10, nil // fallback value
113+
})
114+
115+
result, err := recovered.Join()
43116
if err != nil {
44117
log.Fatal(err)
45118
}
46-
log.Print(*result3)
119+
fmt.Printf("Recovered result: %d\n", result)
47120
}
48121

49122
func asyncAction() async.Future[string] {
50123
promise := async.NewPromise[string]()
51124
go func() {
52-
time.Sleep(time.Second)
53-
promise.Success(ok)
125+
time.Sleep(100 * time.Millisecond)
126+
promise.Success("Promise completed")
54127
}()
55-
56128
return promise.Future()
57129
}

examples/goroutine/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ import (
88
)
99

1010
func main() {
11+
fmt.Println("=== Goroutine ID Example ===")
1112
var wg sync.WaitGroup
1213
wg.Add(5)
14+
1315
for i := 0; i < 5; i++ {
14-
go func() {
15-
id, _ := async.GoroutineID()
16-
fmt.Println(id)
17-
wg.Done()
18-
}()
16+
go func(id int) {
17+
defer wg.Done()
18+
goroutineID, err := async.GoroutineID()
19+
if err != nil {
20+
fmt.Printf("Goroutine %d: failed to get ID: %v\n", id, err)
21+
return
22+
}
23+
fmt.Printf("Goroutine %d: ID = %d\n", id, goroutineID)
24+
}(i)
1925
}
26+
2027
wg.Wait()
2128
}

0 commit comments

Comments
 (0)