|
| 1 | +// Example chunking demonstrates WithChunkFn for consistent work distribution. |
| 2 | +// Items with the same key always go to the same worker, enabling per-key |
| 3 | +// aggregation without synchronization. Useful for grouping events by user, |
| 4 | +// session, or any other key. |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/go-pkgz/pool" |
| 14 | + "github.com/go-pkgz/pool/metrics" |
| 15 | +) |
| 16 | + |
| 17 | +// Event represents something happening for a user |
| 18 | +type Event struct { |
| 19 | + UserID string |
| 20 | + Action string |
| 21 | +} |
| 22 | + |
| 23 | +func main() { |
| 24 | + // track which worker processes which users (for demonstration) |
| 25 | + var mu sync.Mutex |
| 26 | + workerUsers := make(map[int]map[string]int) // workerID -> userID -> count |
| 27 | + |
| 28 | + // create pool with chunking by UserID |
| 29 | + // this ensures all events for a user go to the same worker |
| 30 | + p := pool.New[Event](3, pool.WorkerFunc[Event](func(ctx context.Context, e Event) error { |
| 31 | + // get worker ID from context |
| 32 | + workerID := metrics.WorkerID(ctx) |
| 33 | + |
| 34 | + mu.Lock() |
| 35 | + if workerUsers[workerID] == nil { |
| 36 | + workerUsers[workerID] = make(map[string]int) |
| 37 | + } |
| 38 | + workerUsers[workerID][e.UserID]++ |
| 39 | + mu.Unlock() |
| 40 | + |
| 41 | + fmt.Printf("worker %d: user=%s action=%s\n", workerID, e.UserID, e.Action) |
| 42 | + time.Sleep(10 * time.Millisecond) |
| 43 | + return nil |
| 44 | + })).WithChunkFn(func(e Event) string { |
| 45 | + return e.UserID // route by user ID |
| 46 | + }) |
| 47 | + |
| 48 | + ctx := context.Background() |
| 49 | + if err := p.Go(ctx); err != nil { |
| 50 | + fmt.Printf("failed to start: %v\n", err) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + // submit events for different users |
| 55 | + // notice: same user's events will always go to the same worker |
| 56 | + events := []Event{ |
| 57 | + {"alice", "login"}, |
| 58 | + {"bob", "login"}, |
| 59 | + {"charlie", "login"}, |
| 60 | + {"alice", "view_page"}, |
| 61 | + {"bob", "view_page"}, |
| 62 | + {"alice", "click_button"}, |
| 63 | + {"charlie", "view_page"}, |
| 64 | + {"bob", "logout"}, |
| 65 | + {"alice", "logout"}, |
| 66 | + {"charlie", "click_button"}, |
| 67 | + {"charlie", "logout"}, |
| 68 | + } |
| 69 | + |
| 70 | + for _, e := range events { |
| 71 | + p.Submit(e) |
| 72 | + } |
| 73 | + |
| 74 | + if err := p.Close(ctx); err != nil { |
| 75 | + fmt.Printf("error: %v\n", err) |
| 76 | + } |
| 77 | + |
| 78 | + // show which worker handled which users |
| 79 | + fmt.Printf("\nworker assignment (each user always goes to same worker):\n") |
| 80 | + for workerID, users := range workerUsers { |
| 81 | + fmt.Printf(" worker %d: ", workerID) |
| 82 | + for user, count := range users { |
| 83 | + fmt.Printf("%s(%d) ", user, count) |
| 84 | + } |
| 85 | + fmt.Println() |
| 86 | + } |
| 87 | + |
| 88 | + stats := p.Metrics().GetStats() |
| 89 | + fmt.Printf("\nprocessed %d events in %v\n", stats.Processed, stats.TotalTime.Round(time.Millisecond)) |
| 90 | +} |
0 commit comments