Skip to content

A robust and efficient outbox pattern implementation in Go for reliable message delivery in distributed systems.

License

Notifications You must be signed in to change notification settings

huimingz/goutbox

Repository files navigation

GoOutbox - Outbox Pattern Implementation for Go

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.

Features

  • 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

Installation

go get github.com/huimingz/goutbox

Quick Start

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)
    }
}

Storage Implementations

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A robust and efficient outbox pattern implementation in Go for reliable message delivery in distributed systems.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages