Skip to content

Commit e95b536

Browse files
miDebraffael0
authored andcommitted
create new sockets when launching influx sender threads
1 parent 69c352a commit e95b536

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

include/logging/influxDb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern "C" {
3333
int initDbContext(influxDbContext *cntxt, const char *hostname, const char *port, const char *database);
3434
int deInitDbContext(influxDbContext *cntxt);
3535

36+
int createSocket(influxDbContext *cntxt);
37+
3638
int sendData(influxDbContext *cntxt, char *data, size_t length);
3739
#ifdef __cplusplus
3840
}

src/logging/InfluxDbWriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ void InfluxDbWriter::flush() {
114114

115115
// spawn a thread to send the buffer
116116
threads.emplace_back([this, buf = std::move(buf_to_send)]() mutable {
117-
sendData(&cntxt, buf.data(), buf.size());
117+
influxDbContext context = cntxt;
118+
createSocket(&context); // create a new socket to ensure thread safety
119+
sendData(&context, buf.data(), buf.size());
118120
// return the buffer to the pool
119121
std::lock_guard thread_lock(buffer_mutex);
120122
available_buffers.emplace_back(std::move(buf));

src/logging/influxDb.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ void set_credentials(influxDbContext *cntxt, const char *username, const char *p
4343
}
4444

4545
int initDbContext(influxDbContext *cntxt, const char *hostname, const char *port, const char *database) {
46-
struct addrinfo hints, *ai = NULL;
4746

4847
cntxt->sock_fd = -1;
4948
memset(cntxt->user, 0, SETTINGS_LENGTH);
@@ -54,12 +53,18 @@ int initDbContext(influxDbContext *cntxt, const char *hostname, const char *port
5453
safe_str_cpy(cntxt->port, port, SETTINGS_LENGTH);
5554
safe_str_cpy(cntxt->db_name, database, SETTINGS_LENGTH);
5655

56+
return createSocket(cntxt);
57+
}
58+
59+
int createSocket(influxDbContext *cntxt) {
60+
struct addrinfo hints, *ai = NULL;
61+
5762
memset(&hints, 0, sizeof(struct addrinfo));
5863
hints.ai_family = AF_INET;
5964
hints.ai_socktype = SOCK_STREAM;
6065

6166
do {
62-
if(getaddrinfo(hostname, port, &hints, &ai) != 0) {
67+
if(getaddrinfo(cntxt->hostname, cntxt->port, &hints, &ai) != 0) {
6368
break;
6469
}
6570

0 commit comments

Comments
 (0)