/goeventqueue

Event queue with pub/sub pattern in simple way

Primary LanguageGoMIT LicenseMIT

Go Event Queue

Internal event queue with pub/sub pattern in Go with goroutines and channels

Usage

Create queue to communicate between publisher and subscriber

q := goeventqueue.NewLocalQueue(2)

Create publisher to push event to queue

pub := goeventqueue.NewPublisher(q)

Create subscriber to consume event

// `MaxGoRoutine` to control max routines to consumer event
// `MaxRetry` to control max backoff times to re-consumer event if it failed
sub := goeventqueue.NewSubscriber(q, subscriber.Config{
    MaxGoRoutine: 2,
    MaxRetry:     0,
})

Define custom logger for subscriber

// custom logger should implement this interface
type Logger interface {
    Error(msg string, err error)
}

// assign custom logger to subscriber
sub.WithLogger(customLogger)

Add handler to according event

sub.Register(TestEvent, func(ctx context.Context, data interface{}) error {
    log.Println("job 1", data)
    return nil
})

Run workers to consume event

sub.Start(mainCtx)

Push event to queue

pub.Publish(mainCtx, NewEvent(TestEvent, "say"))

License

MIT

Contribution

All your contributions to project and make it better, they are welcome. Feel free to start an issue.