Confluent-kafka-dotnet: Is IProducer.ProduceAsync(...) thread safe?

Created on 28 Aug 2019  路  3Comments  路  Source: confluentinc/confluent-kafka-dotnet

Description

Is it safe to re-use an instance of IProducer to call ProduceAsync() from multiple threads?

My use case is an API method in an ASP.NET core application, in which I want to produce a message when the API is called.

How to reproduce

[Route( "api/demo/producer" )]
[ApiController]
public class ProducerController : ControllerBase
{
    private readonly IProducer<int, string> _kafkaProducer;

    public ProducerController( IProducer<int, string> kafkaProducer )
    {
        _kafkaProducer = kafkaProducer;
    }

    [HttpPost( "produce" )]
    public async Task<string> Produce( [FromBody] int key, [FromBody] string value )
    {
        var message = new Message<int, string> { Key = key, Value = value };
        var result = await _kafkaProducer.ProduceAsync( "test-topic", message );
        return $"Delivered '{result.Key}','{result.Value}' to '{result.TopicPartitionOffset}'";
    }
}

where the producer instance is injected as a singleton by adding it as a singleton in ConfigureServices() like this:

services.AddSingleton( new ProducerBuilder<int, string> ( config ).Build( ) );

Checklist

Please provide the following information:

  • [ ] A complete (i.e. we can run it), minimal program demonstrating the problem. No need to supply a project file.
  • [ ] Confluent.Kafka nuget version.
  • [ ] Apache Kafka version.
  • [ ] Client configuration.
  • [ ] Operating system.
  • [ ] Provide logs (with "debug" : "..." as necessary in configuration).
  • [ ] Provide broker log excerpts.
  • [ ] Critical issue.
question

Most helpful comment

yes, our documentation is badly lacking. it's on the list.

All 3 comments

yes

Ok, thanks for the quick answer, may I suggest that this be specifically mentioned in README.md or in the examples?

yes, our documentation is badly lacking. it's on the list.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ravindranrajendran picture Ravindranrajendran  路  3Comments

michael-huxtable picture michael-huxtable  路  4Comments

ietvijay picture ietvijay  路  3Comments

zoeysaurusrex picture zoeysaurusrex  路  4Comments

Eibwen picture Eibwen  路  3Comments