|
1 | 1 | # go-pubsub |
2 | | -A lightweight real-time Pub-Sub library for Go. |
| 2 | + |
| 3 | +<img src="logo.png" width="128px" alt="logo"> |
| 4 | + |
| 5 | +Lightweight, real-time Pub/Sub for Go—perfect for transient data flows like live dashboards, game events, short-lived alerts, and real-time streaming-media packet fan-out. It’s pure fire-and-forget: zero persistence, no delivery guarantees, just ultra-fast one-way messaging. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +go get github.com/F2077/go-pubsub |
| 13 | +``` |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +```go |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "github.com/F2077/go-pubsub/pubsub" |
| 23 | + "time" |
| 24 | +) |
| 25 | + |
| 26 | +func main() { |
| 27 | + // 1. Create a broker (supports generics) |
| 28 | + broker, _ := pubsub.NewBroker[string]() |
| 29 | + |
| 30 | + // 2. Create a publisher |
| 31 | + publisher := pubsub.NewPublisher[string](broker) |
| 32 | + |
| 33 | + // 3. Create a subscriber |
| 34 | + subscriber := pubsub.NewSubscriber[string](broker) |
| 35 | + |
| 36 | + // 4. Subscribe to a topic with buffer size and timeout |
| 37 | + sub, _ := subscriber.Subscribe("alerts", |
| 38 | + pubsub.WithChannelSize[string](pubsub.Medium), // Buffer 100 messages |
| 39 | + pubsub.WithTimeout[string](5*time.Second), // Auto-close if idle |
| 40 | + ) |
| 41 | + defer func(sub *pubsub.Subscription[string]) { |
| 42 | + _ = sub.Close() |
| 43 | + }(sub) |
| 44 | + |
| 45 | + // 5. Publish a message |
| 46 | + go func() { |
| 47 | + _ = publisher.Publish("alerts", "CPU over 90%!") |
| 48 | + }() |
| 49 | + |
| 50 | + // 6. Listen for messages or timeouts |
| 51 | + select { |
| 52 | + case msg := <-sub.Ch: |
| 53 | + fmt.Println("Received:", msg) // Output: "CPU over 90%!" |
| 54 | + case err := <-sub.ErrCh: |
| 55 | + fmt.Println("Error:", err) // e.g., timeout |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +## Key Features |
| 62 | + |
| 63 | +- 🚀 **Zero Persistence**: Messages vanish if channels are full or subscribers time out. |
| 64 | +- ⏱️ **Auto-Expiry**: Idle subscriptions close automatically (configurable timeout). |
| 65 | +- 🔒 **Concurrency-Safe**: Efficient locking for high concurrency. |
| 66 | +- 📦 **Capacity Control**: Set max subscriptions per broker (prevents memory leaks). |
| 67 | +- 📡 **Topic-Based**: Simple publish/subscribe with string topics. |
| 68 | + |
| 69 | +## Advanced Configuration |
| 70 | + |
| 71 | +### Custom Broker |
| 72 | + |
| 73 | +```go |
| 74 | +broker, _ := pubsub.NewBroker[string]( |
| 75 | +pubsub.WithCapacity(5000), // Max 5000 topics |
| 76 | +pubsub.WithLogger(customLogger), // Inject your logger |
| 77 | +pubsub.WithId("broker-1"), // Custom broker ID |
| 78 | +) |
| 79 | +``` |
| 80 | + |
| 81 | +### Subscriber Options |
| 82 | + |
| 83 | +```go |
| 84 | +// Subscribe with custom settings |
| 85 | +sub, _ := subscriber.Subscribe("metrics", |
| 86 | +pubsub.WithChannelSize[string](pubsub.Huge), // 1000-message buffer |
| 87 | +pubsub.WithTimeout[string](10 * time.Second), // Timeout after 10s inactivity |
| 88 | +) |
| 89 | +``` |
| 90 | + |
| 91 | +## When to Use |
| 92 | + |
| 93 | +- ✅ Real-time pub-sub |
| 94 | +- ✅ Low-latency gaming/live events |
| 95 | +- ❌ **Not for**: Persistent queues, guaranteed delivery. |
| 96 | + |
| 97 | +## Performance Notes |
| 98 | + |
| 99 | +- 🔥 **Fast fan-out**: Optimized for many subscribers per topic. |
| 100 | +- ⚠️ **No backpressure**: Full channels drop messages silently. |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Benchmark Results |
| 105 | + |
| 106 | +_All benchmarks run on_ **goos: windows**, **goarch: amd64**, **pkg: github.com/F2077/go-pubsub**, **cpu: Intel(R) Core(TM) i7-10700F CPU @ 2.90GHz** |
| 107 | + |
| 108 | +| Benchmark | Iterations | ns/op | B/op | allocs/op | |
| 109 | +|--------------------------------------------------|-----------:|----------------:|-----:|----------:| |
| 110 | +| BenchmarkPublishSingleSubscriber-16 | 5 188 107 | 233.0 ns/op | 96 | 2 | |
| 111 | +| BenchmarkMultipleSubscribers-16 | 143 594 | 8 089 ns/op | 96 | 2 | |
| 112 | +| BenchmarkMultiPublisherSingleSubscriber-16 | 259 663 | 4 732 ns/op | 776 | 21 | |
| 113 | +| BenchmarkMultiPublisherMultipleSubscribers-16 | 67 593 | 17 823 ns/op | 776 | 21 | |
| 114 | +| BenchmarkUltraLargeSubscribersSinglePublisher-16 | 471 | 2 846 125 ns/op | 96 | 2 | |
| 115 | +| BenchmarkPublishChannelSizes/Small-16 | 5 271 156 | 230.2 ns/op | 96 | 2 | |
| 116 | +| BenchmarkPublishChannelSizes/Medium-16 | 5 134 640 | 229.5 ns/op | 96 | 2 | |
| 117 | +| BenchmarkPublishChannelSizes/Large-16 | 5 238 266 | 231.8 ns/op | 96 | 2 | |
| 118 | +| BenchmarkPublishWithTimeout-16 | 1 345 124 | 861.1 ns/op | 507 | 7 | |
| 119 | +| BenchmarkHighLoadParallel-16 | 14 728 | 83 785 ns/op | 100 | 2 | |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +**Contributions welcome!** Report bugs or suggest features via issues. |
0 commit comments