Skip to content

Commit d1ac258

Browse files
authored
Merge pull request #157 from xrdavies/develop
optimize pool connection
2 parents 702655c + 3b45d33 commit d1ac258

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

client/pool.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ static struct pollfd *g_fds;
123123
static connection_list_element *g_connection_list_head = NULL;
124124
static connection_list_element *g_accept_connection_list_head = NULL;
125125
static miner_list_element *g_miner_list_head = NULL;
126+
static uint32_t g_connection_changed = 0;
126127
static pthread_mutex_t g_descriptors_mutex = PTHREAD_MUTEX_INITIALIZER;
127128
static pthread_mutex_t g_pool_mutex = PTHREAD_MUTEX_INITIALIZER;
128129

@@ -468,6 +469,7 @@ static void close_connection(connection_list_element *connection, const char *me
468469
pthread_mutex_lock(&g_descriptors_mutex);
469470
LL_DELETE(g_connection_list_head, connection);
470471
--g_connections_count;
472+
g_connection_changed = 1;
471473

472474
close(conn_data->connection_descriptor.fd);
473475

@@ -815,17 +817,22 @@ void *pool_main_thread(void *arg)
815817
{
816818
LL_DELETE(g_accept_connection_list_head, elt);
817819
LL_APPEND(g_connection_list_head, elt);
820+
g_connection_changed = 1;
818821
}
819-
820822
int index = 0;
821-
LL_FOREACH(g_connection_list_head, elt)
822-
{
823-
memcpy(g_fds + index, &elt->connection_data.connection_descriptor, sizeof(struct pollfd));
824-
++index;
823+
if(g_connection_changed) {
824+
g_connection_changed = 0;
825+
LL_FOREACH(g_connection_list_head, elt)
826+
{
827+
memcpy(g_fds + index, &elt->connection_data.connection_descriptor, sizeof(struct pollfd));
828+
++index;
829+
}
825830
}
831+
832+
int connections_count = g_connections_count;
826833
pthread_mutex_unlock(&g_descriptors_mutex);
827834

828-
int res = poll(g_fds, index, 1000);
835+
int res = poll(g_fds, connections_count, 1000);
829836

830837
if(!res) continue;
831838

0 commit comments

Comments
 (0)