Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomwright/gracesarama
Sarama runners for use with grace.
https://github.com/tomwright/gracesarama
Last synced: 19 days ago
JSON representation
Sarama runners for use with grace.
- Host: GitHub
- URL: https://github.com/tomwright/gracesarama
- Owner: TomWright
- License: mit
- Created: 2021-01-28T12:05:50.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-23T14:06:23.000Z (over 3 years ago)
- Last Synced: 2024-10-11T01:03:57.318Z (about 1 month ago)
- Language: Go
- Size: 19.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go Report Card](https://goreportcard.com/badge/github.com/TomWright/gracesarama)](https://goreportcard.com/report/github.com/TomWright/gracesarama)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/tomwright/gracesarama)](https://pkg.go.dev/github.com/tomwright/gracesarama)
![GitHub License](https://img.shields.io/github/license/TomWright/gracesarama)
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/TomWright/gracesarama?label=latest%20release)# Grace Sarama Runners
Sarama runners for use with [grace](https://github.com/TomWright/grace).
## Consumer Usage
```go
package mainimport (
"context"
"fmt"
"github.com/tomwright/grace"
"github.com/tomwright/gracesarama"
"github.com/Shopify/sarama"
"log"
)func main() {
g := grace.Init(context.Background())config := sarama.NewConfig()
// Set kafka version.
config.Version = sarama.V2_1_0_0runner := gracesarama.NewConsumerGroupRunner(
[]string{"localhost:9092"},
"my-group",
config,
[]string{"topic-a", "topic-b"},
&exampleConsumerGroupHandler{},
)
runner.ErrorHandlerFn = func(err error) {
log.Printf("ERROR: %s\n", err.Error())
}g.Run(runner)
g.Wait()
}type exampleConsumerGroupHandler struct{}
func (exampleConsumerGroupHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
func (exampleConsumerGroupHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
func (h exampleConsumerGroupHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for msg := range claim.Messages() {
fmt.Printf("Message topic:%q partition:%d offset:%d\n", msg.Topic, msg.Partition, msg.Offset)
sess.MarkMessage(msg, "")
}
return nil
}
```## Producer Usage
```go
package mainimport (
"context"
"github.com/tomwright/grace"
"github.com/tomwright/gracesarama"
"github.com/Shopify/sarama"
"log"
)func main() {
g := grace.Init(context.Background())config := sarama.NewConfig()
// Set kafka version.
config.Version = sarama.V2_1_0_0
config.Producer.Return.Errors = true
config.Producer.Return.Successes = truerunner := gracesarama.NewProducerRunner([]string{"localhost:9092"}, config)
runner.SuccessHandlerFn = func(message *sarama.ProducerMessage) {
log.Printf("message published to topic: %s\n", message.Topic)
}
runner.ErrorHandlerFn = func(err *sarama.ProducerError) {
log.Printf("failed to publish to topic: %s: %s\n", err.Msg.Topic, err.Err.Error())
}produceCh := runner.Input()
g.Run(runner)
go func() {
produceCh <- &sarama.ProducerMessage{
// ...
}
}()g.Wait()
}
```