https://github.com/duysmile/goeventqueue
Event queue with pub/sub pattern in simple way
https://github.com/duysmile/goeventqueue
events golang pubsub queue
Last synced: 4 months ago
JSON representation
Event queue with pub/sub pattern in simple way
- Host: GitHub
- URL: https://github.com/duysmile/goeventqueue
- Owner: duysmile
- License: mit
- Created: 2022-06-06T07:43:47.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2025-06-25T05:22:49.000Z (12 months ago)
- Last Synced: 2025-06-25T05:27:20.345Z (12 months ago)
- Topics: events, golang, pubsub, queue
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Event Queue
Internal event queue with pub/sub pattern in Go with goroutines and channels
### Migration
Version 0.2 splits the library into separate packages. Queue, publisher and
subscriber implementations now live under `queue`, `publisher` and
`subscriber` directories respectively. Update imports accordingly:
```go
import (
"github.com/duysmile/goeventqueue/queue"
"github.com/duysmile/goeventqueue/publisher"
"github.com/duysmile/goeventqueue/subscriber"
)
```
### Usage
Create queue to communicate between publisher and subscriber
```go
q := queue.NewLocalQueue(2)
```
Create publisher to push event to queue
```go
pub := publisher.NewPublisher(q)
```
Create subscriber to consume event
```go
// `MaxGoRoutine` to control max routines to consumer event
// `MaxRetry` to control max backoff times to re-consumer event if it failed
sub := subscriber.NewSubscriber(q, subscriber.Config{
MaxGoRoutine: 2,
MaxRetry: 0,
})
```
Define custom logger for subscriber
```go
// 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
```go
sub.Register(TestEvent, func(ctx context.Context, data interface{}) error {
log.Println("job 1", data)
return nil
})
```
Run workers to consume event and stop them gracefully when done
```go
sub.Start(mainCtx)
defer sub.Stop()
```
Push event to queue
```go
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](https://github.com/duysmile/goeventqueue/issues).