|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "fmt" |
6 | | - "time" |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
7 | 7 |
|
8 | | - "github.com/Raezil/GoEventBus" |
| 8 | + "github.com/Raezil/GoEventBus" |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | // Handler runs for 150 ms unless its context is cancelled. |
12 | 12 | func main() { |
13 | | - dispatcher := GoEventBus.Dispatcher{ |
14 | | - "demo": func(ctx context.Context, _ map[string]any) (GoEventBus.Result, error) { |
15 | | - select { |
16 | | - case <-time.After(150 * time.Millisecond): |
17 | | - fmt.Println("handler OK") |
18 | | - return GoEventBus.Result{Message: "ok"}, nil |
19 | | - case <-ctx.Done(): |
20 | | - fmt.Println("handler cancelled:", ctx.Err()) |
21 | | - return GoEventBus.Result{}, ctx.Err() |
22 | | - } |
23 | | - }, |
24 | | - } |
25 | | - |
26 | | - store := GoEventBus.NewEventStore(&dispatcher, 4, GoEventBus.Block) |
27 | | - |
28 | | - // Handler‑only 40 ms timeout → will cancel. |
29 | | - hctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond) |
30 | | - defer cancel() |
31 | | - |
32 | | - _ = store.Subscribe(context.Background(), GoEventBus.Event{ |
33 | | - ID: "H-1", Projection: "demo", Args: map[string]any{"__ctx": hctx}, |
34 | | - }) |
35 | | - |
36 | | - store.Publish() |
37 | | - time.Sleep(60 * time.Millisecond) |
38 | | - |
39 | | - pub, proc, errs := store.Metrics() |
40 | | - fmt.Printf("published=%d processed=%d errors=%d\n", pub, proc, errs) |
| 13 | + dispatcher := GoEventBus.Dispatcher{ |
| 14 | + "demo": func(ctx context.Context, _ map[string]any) (GoEventBus.Result, error) { |
| 15 | + select { |
| 16 | + case <-time.After(150 * time.Millisecond): |
| 17 | + fmt.Println("handler OK") |
| 18 | + return GoEventBus.Result{Message: "ok"}, nil |
| 19 | + case <-ctx.Done(): |
| 20 | + fmt.Println("handler cancelled:", ctx.Err()) |
| 21 | + return GoEventBus.Result{}, ctx.Err() |
| 22 | + } |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + store := GoEventBus.NewEventStore(&dispatcher, 4, GoEventBus.Block) |
| 27 | + |
| 28 | + // Handler‑only 40 ms timeout → will cancel. |
| 29 | + hctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond) |
| 30 | + defer cancel() |
| 31 | + |
| 32 | + _ = store.Subscribe(context.Background(), GoEventBus.Event{ |
| 33 | + ID: "H-1", Projection: "demo", Args: map[string]any{"__ctx": hctx}, |
| 34 | + }) |
| 35 | + |
| 36 | + store.Publish() |
| 37 | + time.Sleep(60 * time.Millisecond) |
| 38 | + |
| 39 | + pub, proc, errs := store.Metrics() |
| 40 | + fmt.Printf("published=%d processed=%d errors=%d\n", pub, proc, errs) |
41 | 41 | } |
0 commit comments