|
1 | | -# dispatcher |
| 1 | +# Dispatcher |
| 2 | + |
| 3 | +A high-performance, type-safe dispatcher for Go. |
| 4 | + |
| 5 | +[](https://pkg.go.dev/github.com/struct0x/dispatcher) |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Dispatcher is a Go library that provides a fast and efficient way to route values to their appropriate handlers based on type. |
| 10 | +It's designed with performance in mind, |
| 11 | +offering both thread-safe and immutable variants with different performance characteristics. |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- **Type Safety**: Compile-time type safety with generic handlers |
| 16 | +- **Zero Allocations**: Dispatch operations produce zero allocations in steady state |
| 17 | +- **High Performance**: Optimized for concurrent access patterns |
| 18 | +- **Middleware Support**: Chainable middleware for cross-cutting concerns |
| 19 | +- **Two Registry Types**: |
| 20 | + - `Registry`: Thread-safe, dynamic registration |
| 21 | + - `SealedRegistry`: Immutable, zero mutex overhead |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +```bash |
| 26 | +go get github.com/struct0x/dispatcher |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Basic Example |
| 32 | + |
| 33 | +```go |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "context" |
| 38 | + "fmt" |
| 39 | + "log" |
| 40 | + |
| 41 | + "github.com/struct0x/dispatcher" |
| 42 | +) |
| 43 | + |
| 44 | +type UserCreated struct { |
| 45 | + ID int |
| 46 | + Name string |
| 47 | +} |
| 48 | + |
| 49 | +type OrderPlaced struct { |
| 50 | + OrderID string |
| 51 | + Amount float64 |
| 52 | +} |
| 53 | + |
| 54 | +func main() { |
| 55 | + // Create a new registry |
| 56 | + reg := dispatcher.NewRegistry() |
| 57 | + |
| 58 | + // Register handlers for different types |
| 59 | + dispatcher.Register[UserCreated](reg, func(ctx context.Context, event UserCreated) error { |
| 60 | + fmt.Printf("User created: %s (ID: %d)\n", event.Name, event.ID) |
| 61 | + return nil |
| 62 | + }) |
| 63 | + |
| 64 | + dispatcher.Register[OrderPlaced](reg, func(ctx context.Context, event OrderPlaced) error { |
| 65 | + fmt.Printf("Order placed: %s for $%.2f\n", event.OrderID, event.Amount) |
| 66 | + return nil |
| 67 | + }) |
| 68 | + |
| 69 | + // Dispatch events |
| 70 | + ctx := context.Background() |
| 71 | + |
| 72 | + if err := dispatcher.Dispatch(reg, ctx, UserCreated{ID: 1, Name: "Alice"}); err != nil { |
| 73 | + log.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + if err := dispatcher.Dispatch(reg, ctx, OrderPlaced{OrderID: "ORD-001", Amount: 99.99}); err != nil { |
| 77 | + log.Fatal(err) |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Using Middleware |
| 83 | + |
| 84 | +```go |
| 85 | +// Define middleware |
| 86 | +loggingMiddleware := func(next dispatcher.HandlerFunc[UserCreated]) dispatcher.HandlerFunc[UserCreated] { |
| 87 | + return func(ctx context.Context, event UserCreated) error { |
| 88 | + fmt.Printf("Processing user: %s\n", event.Name) |
| 89 | + err := next(ctx, event) |
| 90 | + fmt.Printf("Finished processing user: %s\n", event.Name) |
| 91 | + return err |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Register with middleware |
| 96 | +dispatcher.Register[UserCreated](reg, handler, loggingMiddleware) |
| 97 | +``` |
| 98 | + |
| 99 | +### Sealed Registry for Maximum Performance |
| 100 | + |
| 101 | +```go |
| 102 | +// After registering all handlers, seal the registry for better performance |
| 103 | +sealedReg := reg.Seal() |
| 104 | + |
| 105 | +// SealedRegistry has zero mutex overhead |
| 106 | +if err := dispatcher.Dispatch(sealedReg, ctx, UserCreated{ID: 2, Name: "Bob"}); err != nil { |
| 107 | + log.Fatal(err) |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Performance |
| 112 | + |
| 113 | +Dispatcher is optimized for high-performance scenarios: |
| 114 | + |
| 115 | +- **Zero Allocations**: Dispatch operations after initialization produce zero allocations |
| 116 | +- **Concurrent Safe**: `Registry` can be used safely across goroutines |
| 117 | +- **Sealed Optimization**: `SealedRegistry` eliminates all mutex overhead |
| 118 | +- **Benchmark Results** (Apple M2 Max): |
| 119 | + - `Registry`: |
| 120 | + - 1 CPU: 21.46 ns/op |
| 121 | + - 4 CPU: 64.80 ns/op |
| 122 | + - 8 CPU: 122.0 ns/op |
| 123 | + - `SealedRegistry`: |
| 124 | + - 1 CPU: 14.59 ns/op |
| 125 | + - 4 CPU: 28.95 ns/op |
| 126 | + - 8 CPU: 45.53 ns/op |
| 127 | + |
| 128 | +*Note: Performance may vary based on your system and workload. Run benchmarks on your target system for accurate measurements.* |
| 129 | + |
| 130 | +Run benchmarks with: |
| 131 | +```bash |
| 132 | +go test -bench=. -benchmem |
| 133 | +``` |
| 134 | + |
| 135 | +## How It Works |
| 136 | + |
| 137 | +1. **Registration**: Handlers are registered for specific types using Go generics |
| 138 | +2. **Type Mapping**: Types are mapped to handlers using `reflecgt.Type` as keys |
| 139 | +3. **Dispatch**: Values are routed to appropriate handlers based on their runtime type |
| 140 | +4. **Middleware Chain**: Middleware is applied in the order provided during registration |
| 141 | + |
| 142 | +## API Reference |
| 143 | + |
| 144 | +### Core Types |
| 145 | + |
| 146 | +- `dispatcher.Registry`: Thread-safe registry for dynamic handler registration |
| 147 | +- `dispatcher.SealedRegistry`: Immutable registry with zero mutex overhead |
| 148 | +- `dispatcher.HandlerFunc[T]`: Type-safe handler function |
| 149 | +- `dispatcher.Middleware[T]`: Type-safe middleware function |
| 150 | + |
| 151 | +### Core Functions |
| 152 | + |
| 153 | +- `dispatcher.NewRegistry()`: Creates a new thread-safe registry |
| 154 | +- `dispatcher.Register[T](reg, handler, middleware...)`: Registers a handler for type T |
| 155 | + - ⚠️ If called multiple times for the same type `T`, later registrations overwrite earlier ones. |
| 156 | +- `dispatcher.Dispatch(reg, ctx, value)`: Dispatches a value to its registered handler |
| 157 | +- `registry.Seal()`: Creates a sealed immutable copy of a registry |
| 158 | + |
| 159 | +## Use Cases |
| 160 | + |
| 161 | +- **Event-driven architectures** |
| 162 | +- **Message routing systems** |
| 163 | +- **Command handlers in CQRS** |
| 164 | +- **Plugin systems** |
| 165 | +- **Any scenario requiring type-based dispatch** |
| 166 | + |
| 167 | +## License |
| 168 | + |
| 169 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 170 | + |
| 171 | +## Contributing |
| 172 | + |
| 173 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments