Greg Bala
Greg Bala

Reputation: 3903

Is it safe for multiple threads to use the same EventHubClient object to publish events?

Is eventHubClient.SendAsync thread safe? That is, can I get one instance of EventHubClient from EventHubClient.CreateFromConnectionString() and use it to write to event hub from multiple threads. This the code I am using, Logger.Log will be used by many threads in a asp.net web application.

Could not find the answer for this in the documentation..

class Logger
{
    ...
    static EventHubClient eventHubClient;

    static Logger()
    {
        eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString, eventHubName);
    }

    public static void Log()
    {
        ...
        eventHubClient.SendAsync(eventObject);

    }
}

Upvotes: 0

Views: 442

Answers (1)

Serkant Karaca
Serkant Karaca

Reputation: 2042

EventHubClient.SendASync() is totally thread-safe.

One side note though. Each client will maintain a single TCP connection underneath. This connection can become a bottle-neck when there are large number of parallel sends waiting to either be queued or in the queue to be processed. So consider monitoring number of pending sends for sometime and scale out your sends to multiple clients if needed.

Upvotes: 1

Related Questions