Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/teivah/resequencer
Resequencer library
https://github.com/teivah/resequencer
eip go golang library
Last synced: 2 months ago
JSON representation
Resequencer library
- Host: GitHub
- URL: https://github.com/teivah/resequencer
- Owner: teivah
- Created: 2022-01-18T20:04:50.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-18T20:37:28.000Z (almost 3 years ago)
- Last Synced: 2024-06-20T01:59:53.722Z (7 months ago)
- Topics: eip, go, golang, library
- Language: Go
- Homepage: https://www.enterpriseintegrationpatterns.com/Resequencer.html
- Size: 151 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# resequencer
![CI](https://github.com/teivah/resequencer/actions/workflows/ci.yml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/teivah/resequencer)](https://goreportcard.com/report/github.com/teivah/resequencer)A Go resequencer
## Introduction
`resequencer` is a Go library that implements the [resequencer pattern](https://www.enterpriseintegrationpatterns.com/Resequencer.html).
One use case, for example, is when using Sarama with a consumer group, and we distribute each message to a set of workers. If we want to make sure the offsets are committed in sequence, we can use a resequencer per partition.
## How to Use
First, we need to create a new `Handler`. Then we have to use the two methods:
* `Push` to add new sequence IDs
* `Messages` that returns a `<-chan []int` that contains the ordered sequence IDs```go
ctx, cancel := context.WithCancel(context.Background())
handler := resequencer.NewHandler(ctx, -1) // Create a resequencer and initialize the first sequence ID to -1max := 10
for i := 0; i < max; i++ {
i := i
go func() {
handler.Push(i) // Push a new sequence ID
}()
}for sequenceIDs := range handler.Messages() { // Read all the sequence IDs (sequenceIDs is an []int)
for _, sequenceID := range sequenceIDs {
fmt.Println(sequenceID)
if sequenceID == max-1 {
cancel()
return
}
}
}
```It will output the sequence IDs in order, regardless of the goroutines execution:
```
0
1
2
3
4
5
6
7
8
9
```