-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide report channel Events()
for confluent kafka producer
#1031
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,8 +33,9 @@ func main() { | |
} | ||
defer receiver.Close(ctx) | ||
|
||
// Setting the 'client.WithPollGoroutines(1)' to make sure the events from kafka partition are processed in order | ||
c, err := cloudevents.NewClient(receiver, client.WithPollGoroutines(1)) | ||
// The 'WithBlockingCallback()' is to make event processing serialized (no concurrency), use this option along with WithPollGoroutines(1). | ||
// These two options make sure the events from kafka partition are processed in order | ||
c, err := cloudevents.NewClient(receiver, client.WithBlockingCallback(), client.WithPollGoroutines(1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh wow, man...this is really not intuitive to use |
||
if err != nil { | ||
log.Fatalf("failed to create client, %v", err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ package main | |
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
|
||
confluent "github.com/cloudevents/sdk-go/protocol/kafka_confluent/v2" | ||
"github.com/confluentinc/confluent-kafka-go/v2/kafka" | ||
|
@@ -26,8 +27,49 @@ func main() { | |
sender, err := confluent.New(confluent.WithConfigMap(&kafka.ConfigMap{ | ||
"bootstrap.servers": "127.0.0.1:9092", | ||
}), confluent.WithSenderTopic(topic)) | ||
if err != nil { | ||
log.Fatalf("failed to create protocol, %v", err) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
defer sender.Close(ctx) | ||
// Listen to all the events on the default events channel | ||
// It's important to read these events otherwise the events channel will eventually fill up | ||
go func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you're not waiting for this goroutine to finish before returning from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
defer wg.Done() | ||
eventChan, err := sender.Events() | ||
if err != nil { | ||
log.Fatalf("failed to get events channel for sender, %v", err) | ||
} | ||
for e := range eventChan { | ||
switch ev := e.(type) { | ||
case *kafka.Message: | ||
// The message delivery report, indicating success or | ||
// permanent failure after retries have been exhausted. | ||
// Application level retries won't help since the client | ||
// is already configured to do that. | ||
m := ev | ||
if m.TopicPartition.Error != nil { | ||
log.Printf("Delivery failed: %v\n", m.TopicPartition.Error) | ||
} else { | ||
log.Printf("Delivered message to topic %s [%d] at offset %v\n", | ||
*m.TopicPartition.Topic, m.TopicPartition.Partition, m.TopicPartition.Offset) | ||
} | ||
case kafka.Error: | ||
// Generic client instance-level errors, such as | ||
// broker connection failures, authentication issues, etc. | ||
// | ||
// These errors should generally be considered informational | ||
// as the underlying client will automatically try to | ||
// recover from any errors encountered, the application | ||
// does not need to take action on them. | ||
log.Printf("Error: %v\n", ev) | ||
default: | ||
log.Printf("Ignored event: %v\n", ev) | ||
} | ||
} | ||
}() | ||
|
||
c, err := cloudevents.NewClient(sender, cloudevents.WithTimeNow(), cloudevents.WithUUIDs()) | ||
if err != nil { | ||
|
@@ -43,14 +85,13 @@ func main() { | |
"message": "Hello, World!", | ||
}) | ||
|
||
if result := c.Send( | ||
// Set the producer message key | ||
confluent.WithMessageKey(ctx, e.ID()), | ||
e, | ||
); cloudevents.IsUndelivered(result) { | ||
if result := c.Send(confluent.WithMessageKey(ctx, e.ID()), e); cloudevents.IsUndelivered(result) { | ||
log.Printf("failed to send: %v", result) | ||
} else { | ||
log.Printf("sent: %d, accepted: %t", i, cloudevents.IsACK(result)) | ||
} | ||
} | ||
|
||
sender.Close(ctx) | ||
wg.Wait() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add a comment on Send to reference/explain to also use the Events channel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.