cpp-statsd-client icon indicating copy to clipboard operation
cpp-statsd-client copied to clipboard

If the client is thread safe?

Open hh23485 opened this issue 5 years ago • 15 comments

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?

hh23485 avatar Apr 22 '20 12:04 hh23485

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.

vthiery avatar Apr 22 '20 14:04 vthiery

@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?

hh23485 avatar Apr 25 '20 12:04 hh23485

@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.

vthiery avatar Apr 25 '20 13:04 vthiery

@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?

hh23485 avatar Apr 25 '20 14:04 hh23485

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.

vthiery avatar Apr 25 '20 14:04 vthiery

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.

hh23485 avatar Apr 26 '20 02:04 hh23485

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

vthiery avatar Jul 21 '20 07:07 vthiery

@vthiery Sure, It looks good to me.

hh23485 avatar Jul 21 '20 07:07 hh23485

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.

jakila avatar Jan 28 '22 09:01 jakila

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

vthiery avatar Jan 31 '22 07:01 vthiery