vthiery / cpp-statsd-client

A header-only StatsD client implemented in C++
MIT License
51 stars 19 forks source link

If the client is thread safe? #12

Open hh23485 opened 4 years ago

hh23485 commented 4 years ago

Hi, thanks for the rare cpp statsd client!

We'd like to use the client but my concern is about if the client is thread safe? if not do you have some suggestions to use the client in multi-threads web services?

vthiery commented 4 years ago

Hi!

Unfortunately this client is not thread-safe. Nothing prevents a thread to alter the internal udp sender while another one is already sending metrics.

hh23485 commented 4 years ago

@vthiery hi, Thanks for your response!

About the un-thread safe client, do you have some suggestions to use it in web project? Or we have to implements a local-free queue as a buffer to a singleton client consumer?

vthiery commented 4 years ago

@hh23485 I suppose that, as long as you don't explicitly reconfigure or destroy the client while using it, it will be ok. In the context of a web project, one should first initialize/configure the client, the start spawning threads to treat requests concurrently, and make sure all the requests have been handled before destroying the client.

hh23485 commented 4 years ago

@vthiery Sure, initialize only once, and destroy the client at the end of the application.

But to be more clear, do you mean, it's safe to call the singleton client's increment or timing from multi threads? or I need to do my actions to only one thread call these functions at the same time?

vthiery commented 4 years ago

But to be more clear, do you mean, it's safe to call the singleton client's increment or timing from multi threads? or I need to do my actions to only one thread call these functions at the same time?

It should be. The only sensitive piece is batching (m_batchingMessageQueue), but I defined a mutex (m_batchingMutex) and take locks whenever I manipulate the queue.

hh23485 commented 4 years ago

But to be more clear, do you mean, it's safe to call the singleton client's increment or timing from multi threads? or I need to do my actions to only one thread call these functions at the same time?

It should be. The only sensitive piece is batching (m_batchingMessageQueue), but I defined a mutex (m_batchingMutex) and take locks whenever I manipulate the queue.

That's great, thanks for your explain. I'll test and give a feedback.

Thanks, again.

vthiery commented 4 years ago

@hh23485 have you had any issue related thread-safety? otherwise, I'll close this issue.

hh23485 commented 4 years ago

@vthiery Sure, It looks good to me.

jakila commented 2 years ago

If you came here because you wonder if this library is thread safe: Please have a look into this later PR: https://github.com/vthiery/cpp-statsd-client/pull/29 From my understanding that PR does break thread safety, and the statements made earlier in this issues are not true anymore.

vthiery commented 2 years ago

@jakila thanks for pointing that out. I'll re-open this issue and hopefully get some time to investigate it soon