Skip to content

Commit 952f8f6

Browse files
committed
Adds some example
1 parent eeb6fde commit 952f8f6

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/openshift-online/async-routine/opid"
7+
"log/slog"
8+
"os"
9+
"runtime/pprof"
10+
"time"
11+
)
12+
13+
func main() {
14+
slog.Info("Program started")
15+
16+
for i := 0; i < 10; i++ {
17+
foo(opid.NewContext())
18+
}
19+
20+
// Wait enough time to have some routine started
21+
time.Sleep(4 * time.Second)
22+
23+
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
24+
fmt.Scanln()
25+
}
26+
27+
func foo(ctx context.Context) {
28+
slog.Info("foo() started",
29+
"opid", opid.FromContext(ctx))
30+
bar(ctx)
31+
slog.Info("foo() ended",
32+
"opid", opid.FromContext(ctx))
33+
}
34+
35+
func bar(ctx context.Context) {
36+
slog.Info("bar() started",
37+
"opid", opid.FromContext(ctx))
38+
go parentGoroutine(ctx)
39+
slog.Info("bar() ended",
40+
"opid", opid.FromContext(ctx))
41+
}
42+
43+
func parentGoroutine(ctx context.Context) {
44+
slog.Info("parentGoroutine() started",
45+
"opid", opid.FromContext(ctx))
46+
47+
go stuckInSelect()
48+
time.Sleep(500 * time.Millisecond)
49+
slog.Info("parentGoroutine() ended",
50+
"opid", opid.FromContext(ctx))
51+
}
52+
53+
func stuckInSelect() {
54+
slog.Info("stuckInSelect() started")
55+
select {}
56+
slog.Info("stuckInSelect() ended")
57+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/openshift-online/async-routine"
6+
"github.com/openshift-online/async-routine/opid"
7+
"log/slog"
8+
"time"
9+
)
10+
11+
var _ async.RoutinesObserver = (*exampleRoutineObserver)(nil)
12+
13+
func main() {
14+
slog.Info("Program started")
15+
16+
// Setup the AsyncRoutineManager
17+
async.Manager(
18+
// Take a snapshot every 2 seconds
19+
async.WithSnapshottingInterval(2 * time.Second)).
20+
// Start the routine monitor
21+
Monitor().Start()
22+
23+
// Add our custom observer to the list of routine observers
24+
_ = async.Manager().AddObserver(&exampleRoutineObserver{})
25+
26+
for i := 0; i < 10; i++ {
27+
foo(opid.NewContext())
28+
}
29+
30+
// Wait enough time to have some routine started
31+
time.Sleep(4 * time.Second)
32+
}
33+
34+
func foo(ctx context.Context) {
35+
slog.Info("foo() started",
36+
"opid", opid.FromContext(ctx))
37+
bar(ctx)
38+
slog.Info("foo() ended",
39+
"opid", opid.FromContext(ctx))
40+
}
41+
42+
func bar(ctx context.Context) {
43+
slog.Info("bar() started",
44+
"opid", opid.FromContext(ctx))
45+
async.NewAsyncRoutine("parent go routine", ctx,
46+
func() {
47+
parentGoroutine(ctx)
48+
}).
49+
Timebox(2 * time.Second).
50+
Run()
51+
slog.Info("bar() ended",
52+
"opid", opid.FromContext(ctx))
53+
}
54+
55+
func parentGoroutine(ctx context.Context) {
56+
slog.Info("parentGoroutine() started",
57+
"opid", opid.FromContext(ctx))
58+
59+
async.NewAsyncRoutine("stuck in select", ctx, stuckInSelect).
60+
Timebox(2 * time.Second).
61+
Run()
62+
time.Sleep(500 * time.Millisecond)
63+
64+
slog.Info("parentGoroutine() ended",
65+
"opid", opid.FromContext(ctx))
66+
}
67+
68+
func stuckInSelect() {
69+
slog.Info("parentGoroutine() started")
70+
select {}
71+
slog.Info("parentGoroutine() ended")
72+
}
73+
74+
type exampleRoutineObserver struct{}
75+
76+
func (e exampleRoutineObserver) RoutineStarted(routine async.AsyncRoutine) {
77+
slog.Info("Routine started",
78+
"name", routine.Name(),
79+
"opid", routine.OpId(),
80+
"parent-opid", routine.OriginatorOpId(),
81+
)
82+
}
83+
84+
func (e exampleRoutineObserver) RoutineFinished(routine async.AsyncRoutine) {
85+
slog.Info("Routine finished",
86+
"name", routine.Name(),
87+
"opid", routine.OpId(),
88+
"parent-opid", routine.OriginatorOpId(),
89+
)
90+
}
91+
92+
func (e exampleRoutineObserver) RoutineExceededTimebox(routine async.AsyncRoutine) {
93+
slog.Warn("Routine exceeded timebox",
94+
"name", routine.Name(),
95+
"opid", routine.OpId(),
96+
"parent-opid", routine.OriginatorOpId(),
97+
"startedAt", routine.StartedAt(),
98+
)
99+
}
100+
101+
func (e exampleRoutineObserver) RunningRoutineCount(count int) {
102+
// nothing to do in this example
103+
}
104+
105+
func (e exampleRoutineObserver) RunningRoutineByNameCount(name string, count int) {
106+
// nothing to do in this example
107+
}

0 commit comments

Comments
 (0)