EventHubClient.SendBatchAsync - is it thread-safe?

落爺英雄遲暮 提交于 2019-12-24 12:05:02

问题


According to documentation all non-static calls of EventHubClient are not thread-safe. This means that I cannot easily call the following function from wherever I want:

    public async Task SendBatchAsync(IList<EventData> eventHubBatch)
    {
        try
        {
            await this.eventHubClient.SendBatchAsync(eventHubBatch);
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR (EH WRITER): {0}", ex.Message);
        }
    }

Wonder what are the alternatives?

  1. Locking
  2. Sync thread context
  3. Save to some queue and have one threadpool-based async/await while loop which reads from a queue and posts further to EventHub

UPDATE: Eric from Event Hub team confirmed that it is safe to use SendAsync in multithreaded environment as is.

UPDATE 2: MSDN documentation just got updated (within 2 hours, wow!). Now it says: "Any public static or instance members of this type are thread safe.".


回答1:


Cesar has provided the stackoverflow link that is fine as an answer. To supplement the info for your specific (EventHub) question:

  1. If you are asking "Will my data be corrupted if multiple threads call the API using the same instance" then answer is no, we do guarantee thread safety for data corruption scenario.

  2. If you are asking "Will my data ordering be preserved if multiple threads call the API using the same instance" then answer is depends, since we do preserve order as we receive data, but having multiple threads call the same instance means you cannot really be sure which call comes to our network implementation layer first. If you want ordering, send them as a single batch (SendBatchAsync) since we do guarantee order in a batch. Note that a single batch has a size limit though (256k in total)

The documentation link that you provided (https://msdn.microsoft.com/library/azure/microsoft.servicebus.messaging.eventhubclient.aspx) has incomplete/incorrect information and we will go back and review/edit as needed.




回答2:


According to MSDN EventHubClient documentation SendBatchAsync method is not guaranteed to be thread safe.

That being said, from my experience having quite a few workers using EventHubClient in multi-threaded manner, i never had any issue with thread safety using that specific method.

Regarding the options for handling this situation, it's no different than using any multi-threaded code which requires synchronization.

You can use any of the alternatives you suggested, in addition you can also create new EventHubClient per thread and therefor since only one thread uses it, you don't have the problem of mutual exclusion. I don't know about your scenario, this solution for example wouldn't be very good for system where you spawn many threads which uses EventHubClient and then get shut down (event hub connection time + allocation problems etc..)



来源:https://stackoverflow.com/questions/35944887/eventhubclient-sendbatchasync-is-it-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!