This package provides a flexible and robust implementation of the Outbox Pattern for Go applications. It supports distributed systems and allows for different storage implementations.
- Distributed processing support
- Pluggable storage backends (GORM, Memory, etc.)
- Configurable message processing
- Retry mechanism for failed messages
- Message locking to prevent duplicate processing
- Flexible dispatcher system
go get github.com/huimingz/goutbox
Here's a simple example of how to use the outbox pattern:
package main
import (
"context"
"time"
"github.com/huimingz/goutbox"
"github.com/huimingz/goutbox/storage/gorm"
"github.com/huimingz/goutbox/dispatcher/simple"
)
func main() {
// Initialize your GORM DB
db, err := gorm.Open(...)
if err != nil {
panic(err)
}
// Create storage
storage, err := gorm.NewGormStorage(db)
if err != nil {
panic(err)
}
// Create dispatcher
dispatcher := simple.NewDispatcher()
// Register event handlers
dispatcher.RegisterHandler("user.created", func(ctx context.Context, eventType string, payload []byte) error {
// Handle user created event
return nil
})
// Create processor
processor := goutbox.NewProcessor(storage, dispatcher,
goutbox.WithBatchSize(10),
goutbox.WithPollInterval(time.Second),
goutbox.WithRetryAttempts(3),
)
// Start processing
if err := processor.Start(context.Background()); err != nil {
panic(err)
}
defer processor.Stop()
// Store a message
msg := &goutbox.OutboxMessage{
ID: "msg-123",
AggregateID: "user-456",
AggregateType: "user",
EventType: "user.created",
Payload: []byte(`{"name":"John Doe"}`),
Metadata: map[string]string{
"source": "api",
"version": "1.0",
},
CreatedAt: time.Now(),
Status: goutbox.MessageStatusPending,
}
if err := storage.Store(context.Background(), msg); err != nil {
panic(err)
}
}
- GORM Storage - For production use with SQL databases
- Memory Storage - For testing and development
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.