From c9aa3ca84701574e6c7f40d69d99cd161b7ad4eb Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 14 Jan 2021 14:10:28 +0000 Subject: [PATCH] Add MOSQ_OPT_DISABLE_SOCKETPAIR. --- ChangeLog.txt | 10 ++++++++++ include/mosquitto.h | 9 +++++++++ lib/mosquitto.c | 12 +++++++----- lib/mosquitto_internal.h | 1 + lib/options.c | 4 ++++ 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 06fa2b91..bf67d2fe 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,13 @@ +2.1.0 - 2021-xx-xx +================== + +Client library: +- Add MOSQ_OPT_DISABLE_SOCKETPAIR to allow the disabling of the socketpair + feature that allows the network thread to be woken from select() by another + thread when e.g. mosquitto_publish() is called. This reduces the number of + sockets used by each client by two. + + 2.0.6 - 2021-01-xx ================== diff --git a/include/mosquitto.h b/include/mosquitto.h index cd10ea60..17f9a9c7 100644 --- a/include/mosquitto.h +++ b/include/mosquitto.h @@ -138,6 +138,7 @@ enum mosq_opt_t { MOSQ_OPT_TCP_NODELAY = 11, MOSQ_OPT_BIND_ADDRESS = 12, MOSQ_OPT_TLS_USE_OS_CERTS = 13, + MOSQ_OPT_DISABLE_SOCKETPAIR = 14, }; @@ -1540,6 +1541,14 @@ libmosq_EXPORT int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t op * MOSQ_OPT_TLS_USE_OS_CERTS - Set to 1 to instruct the client to load and * trust OS provided CA certificates for use with TLS connections. * Set to 0 (the default) to only use manually specified CA certs. + * + * MOSQ_OPT_DISABLE_SOCKETPAIR - By default, each client connected will create + * an internal pair of connected sockets to allow the network thread + * to be notified and woken up if another thread calls + * or other similar command. If you are + * operating with an external loop, this is not necessary and + * consumes an extra two sockets per client. Set this option to 1 to + * disable the use of the socket pair. */ libmosq_EXPORT int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value); diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 52fd30da..daa9ce75 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -213,11 +213,13 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st pthread_mutex_init(&mosq->mid_mutex, NULL); mosq->thread_id = pthread_self(); #endif - /* This must be after pthread_mutex_init(), otherwise the log mutex may be - * used before being initialised. */ - if(net__socketpair(&mosq->sockpairR, &mosq->sockpairW)){ - log__printf(mosq, MOSQ_LOG_WARNING, - "Warning: Unable to open socket pair, outgoing publish commands may be delayed."); + if(mosq->disable_socketpair == false){ + /* This must be after pthread_mutex_init(), otherwise the log mutex may be + * used before being initialised. */ + if(net__socketpair(&mosq->sockpairR, &mosq->sockpairW)){ + log__printf(mosq, MOSQ_LOG_WARNING, + "Warning: Unable to open socket pair, outgoing publish commands may be delayed."); + } } return MOSQ_ERR_SUCCESS; diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 2d30f7e8..38230d23 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -332,6 +332,7 @@ struct mosquitto { unsigned int reconnect_delay; unsigned int reconnect_delay_max; bool reconnect_exponential_backoff; + bool disable_socketpair; char threaded; struct mosquitto__packet *out_packet_last; mosquitto_property *connect_properties; diff --git a/lib/options.c b/lib/options.c index 8fe1007d..a150dd0e 100644 --- a/lib/options.c +++ b/lib/options.c @@ -416,6 +416,10 @@ int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int val if(!mosq) return MOSQ_ERR_INVAL; switch(option){ + case MOSQ_OPT_DISABLE_SOCKETPAIR: + mosq->disable_socketpair = (bool)value; + break; + case MOSQ_OPT_PROTOCOL_VERSION: if(value == MQTT_PROTOCOL_V31){ mosq->protocol = mosq_p_mqtt31;