'Producing event to kafka topic in a Fire and Forget way in .NET Web API
Scenario:
My web API have to generate some message to kafka topic and in the same request context I have to send the response back to the client.
- My request processing have two functions
- SendToKafka()
- ReturnResponse()
I have to return response even if the SendToKafka method fails (may be due to kafka server not available) My approach was to put the ProduceMessage method in a t Task with timeout of 100 ms so that the thread wont wait for more than 100 ms for the task.
Main()
{
SendToKafka();
return ReturnResponse();
}
void SendToKafka()
{
try
{
var task = Task.Run(() => { ProduceMessage()})
if(task.Wait(100))
{
// log message as successful
}
else
{
// timed out
}
}
catch()
{
// log exception
}
}
I am using custom wrapper for using confluent kafka which does not providing an option to set timeout or cancellation token when producing message to topic.
Is there any better way to solve the issue
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
