Skip to content
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

How to avoid data race when send message with sarama plugin? #78

Closed
hwwwi opened this issue Jul 13, 2023 · 1 comment
Closed

How to avoid data race when send message with sarama plugin? #78

hwwwi opened this issue Jul 13, 2023 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@hwwwi
Copy link
Contributor

hwwwi commented Jul 13, 2023

Description

I have a wrapper function of sendMessage to produce messages with a tracer. (attached below)
Since the ppsarama.WithContext(ctx, producer) access the ctx inside of producer, the data race will occur when I run this function with multiple goroutines.
It would be lovely if I can pass the context to SendMessage function, but it seems syncProducer interface doesn't support the context.
I can make a lock with mutex before the WithContext, but is there a way to prevent this without a lock?

func sendMessageWrapper() {
	tracer := pinpoint.GetAgent().NewSpanTracer("Sarama Producer Invocation", fmt.Sprintf("kafka://topic=%s", topic))
	defer func() {
		tracer.EndSpan()
	}()
	
	ctx = pinpoint.NewContext(ctx, tracer)
	ppsarama.WithContext(ctx, producer) // <--- data race !
	_, _, err := producer.SendMessage(&sarama.ProducerMessage{
		Topic:   topic,
		Key:     sarama.StringEncoder(key),
		Value:   sarama.ByteEncoder(message),
		Headers: kafkaRecordHeaders,
	})
}
@dwkang
Copy link
Contributor

dwkang commented Jul 13, 2023

In order to minimize modification of the application, the syncProducer interface of plugin was made to be the same as sarama's api. I think I need to add a new api to solve the problem.

I will provide a new SendMessageWithContext(ctx context.Context, ...) function.

dwkang added a commit that referenced this issue Sep 12, 2023
@dwkang dwkang added this to the v1.4.0 milestone Jan 10, 2024
@dwkang dwkang added the bug Something isn't working label Jan 10, 2024
dwkang added a commit that referenced this issue Jan 10, 2024
@dwkang dwkang closed this as completed Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants