tobiasschuerg / InfluxDB-Client-for-Arduino

Simple library for sending measurements to an InfluxDB with a single network request. Supports ESP8266 and ESP32.
MIT License
382 stars 95 forks source link

Is the library thread safe? #195

Closed maxdd closed 2 years ago

maxdd commented 2 years ago

Hello, i was wondering whether the library is thread safe? In my case i have multiple classes representing different sensors each with its own scheduling time. Currently each sensor "gets" the influxdbclient handle and pushes its Points to influxdb database. Given each class is running asynchronously in RTOS tasks can i assume the library is thread-safe? Shall i instead put a mutex everytime time the handle is retrieved till the class pushes a point?

e.g

_client->Take();
_client->writePoint(p);
_client->flushBuffer();
_client->Give();

Regards,

vlastahajek commented 2 years ago

@maxdd, no, this library is not thread-safe. There is no synchronization inside. I don't think ESP32 can send data through the network layer on different cores. So, It seems the best pattern would be to keep writing in a single task and sending data (points) from sensors to the writing tasks using a queue.

maxdd commented 2 years ago

Hello, i also thought about that solution by using a queue of Points (or pointers to memory regions) and a condition variable. Do you think it is a better design? Would you mind to provide a basic example as a reference?

Regards,

vlastahajek commented 2 years ago

Definitively, there should be a single place when the client sends data to the server.

I don't have any such code. You should be able to find several examples of using a queue with a custom type.