From 3bd2d7483e23c8290a1a7f64c4c817883aabd3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kuzn=C3=ADk?= Date: Fri, 20 Apr 2018 13:46:34 +0100 Subject: [PATCH] Reuse connection_walk for client matters --- servers/lloadd/client.c | 35 +++-------------------------------- servers/lloadd/daemon.c | 5 ++++- servers/lloadd/monitor.c | 11 +++++++++-- servers/lloadd/proto-lload.h | 3 +-- 4 files changed, 17 insertions(+), 37 deletions(-) diff --git a/servers/lloadd/client.c b/servers/lloadd/client.c index 1b4f62a385..b7903d3d45 100644 --- a/servers/lloadd/client.c +++ b/servers/lloadd/client.c @@ -554,39 +554,10 @@ client_destroy( LloadConnection *c ) } void -clients_destroy( void ) +clients_destroy( int gentle ) { ldap_pvt_thread_mutex_lock( &clients_mutex ); - while ( !LDAP_CIRCLEQ_EMPTY( &clients ) ) { - LloadConnection *c = LDAP_CIRCLEQ_FIRST( &clients ); - - ldap_pvt_thread_mutex_unlock( &clients_mutex ); - CONNECTION_LOCK(c); - /* We have shut down all processing, a dying connection connection - * should have been reclaimed by now! */ - assert( c->c_live ); - /* Upstream connections have already been destroyed, there should be no - * ops left */ - assert( !c->c_ops ); - CONNECTION_DESTROY(c); - ldap_pvt_thread_mutex_lock( &clients_mutex ); - } - ldap_pvt_thread_mutex_unlock( &clients_mutex ); -} - -void -clients_walk( CONNCB apply, void *argv ) -{ - LloadConnection *c; - ldap_pvt_thread_mutex_lock( &clients_mutex ); - if ( LDAP_CIRCLEQ_EMPTY( &clients ) ) { - ldap_pvt_thread_mutex_unlock( &clients_mutex ); - return; - } - - /* Todo is it possible to do this without holding this lock? */ - LDAP_CIRCLEQ_FOREACH ( c, &clients, c_next ) { - apply( c, argv ); - } + connections_walk( + &clients_mutex, &clients, lload_connection_close, &gentle ); ldap_pvt_thread_mutex_unlock( &clients_mutex ); } diff --git a/servers/lloadd/daemon.c b/servers/lloadd/daemon.c index 4d4a8c4423..35d9470e51 100644 --- a/servers/lloadd/daemon.c +++ b/servers/lloadd/daemon.c @@ -1396,6 +1396,9 @@ lloadd_daemon( struct event_base *daemon_base ) ldap_pvt_thread_mutex_unlock( &b->b_mutex ); } + /* Do the same for clients */ + clients_destroy( 1 ); + for ( i = 0; i < lload_daemon_threads; i++ ) { /* * https://github.com/libevent/libevent/issues/623 @@ -1420,7 +1423,7 @@ lloadd_daemon( struct event_base *daemon_base ) #endif lload_backends_destroy(); - clients_destroy(); + clients_destroy( 0 ); lload_bindconf_free( &bindconf ); evdns_base_free( dnsbase, 0 ); diff --git a/servers/lloadd/monitor.c b/servers/lloadd/monitor.c index 176a33d4a9..28354cb943 100644 --- a/servers/lloadd/monitor.c +++ b/servers/lloadd/monitor.c @@ -548,7 +548,10 @@ lload_monitor_in_conn_create( mp_parent = e_parent->e_private; arg.ms = (monitor_subsys_t *)mp_parent->mp_info; - clients_walk( lload_monitor_in_conn_entry, &arg ); + ldap_pvt_thread_mutex_lock( &clients_mutex ); + connections_walk( + &clients_mutex, &clients, lload_monitor_in_conn_entry, &arg ); + ldap_pvt_thread_mutex_unlock( &clients_mutex ); return 0; } @@ -935,8 +938,12 @@ lload_monitor_update_global_stats( void *ctx, void *arg ) Debug( LDAP_DEBUG_TRACE, "lload_monitor_update_global_stats: " "updating stats\n" ); + /* count incoming connections */ - clients_walk( lload_monitor_incoming_count, &tmp_stats ); + ldap_pvt_thread_mutex_lock( &clients_mutex ); + connections_walk( &clients_mutex, &clients, lload_monitor_incoming_count, + &tmp_stats ); + ldap_pvt_thread_mutex_unlock( &clients_mutex ); LDAP_CIRCLEQ_FOREACH ( b, &backend, b_next ) { ldap_pvt_thread_mutex_lock( &b->b_mutex ); diff --git a/servers/lloadd/proto-lload.h b/servers/lloadd/proto-lload.h index accf5e2863..21885319e2 100644 --- a/servers/lloadd/proto-lload.h +++ b/servers/lloadd/proto-lload.h @@ -63,8 +63,7 @@ LDAP_SLAPD_F (void) client_tls_handshake_cb( evutil_socket_t s, short what, void LDAP_SLAPD_F (LloadConnection *) client_init( ber_socket_t s, LloadListener *url, const char *peername, struct event_base *base, int use_tls ); LDAP_SLAPD_F (void) client_reset( LloadConnection *c ); LDAP_SLAPD_F (void) client_destroy( LloadConnection *c ); -LDAP_SLAPD_F (void) clients_destroy( void ); -LDAP_SLAPD_F (void) clients_walk( CONNCB apply, void *argv ); +LDAP_SLAPD_F (void) clients_destroy( int gentle ); /* * config.c