diff --git a/include/libwebsockets/lws-client.h b/include/libwebsockets/lws-client.h index 25fd062c4..85b8488d6 100644 --- a/include/libwebsockets/lws-client.h +++ b/include/libwebsockets/lws-client.h @@ -163,6 +163,12 @@ struct lws_client_connect_info { void *mqtt_cp; #endif + uint16_t keep_warm_secs; + /**< 0 means 5s. If the client connection to the endpoint becomes idle, + * defer closing it for this many seconds in case another outgoing + * connection to the same endpoint turns up. + */ + /* Add new things just above here ---^ * This is part of the ABI, don't needlessly break compatibility * diff --git a/lib/core-net/connect.c b/lib/core-net/connect.c index 96806f141..3601c5362 100644 --- a/lib/core-net/connect.c +++ b/lib/core-net/connect.c @@ -58,7 +58,10 @@ lws_client_connect_via_info(const struct lws_client_connect_info *i) if (wsi == NULL) goto bail; - + if (i->keep_warm_secs) + wsi->keep_warm_secs = i->keep_warm_secs; + else + wsi->keep_warm_secs = 5; wsi->context = i->context; wsi->desc.sockfd = LWS_SOCK_INVALID; diff --git a/lib/core-net/private-lib-core-net.h b/lib/core-net/private-lib-core-net.h index 54f6abdd1..811e4ef83 100644 --- a/lib/core-net/private-lib-core-net.h +++ b/lib/core-net/private-lib-core-net.h @@ -830,6 +830,9 @@ struct lws { uint16_t ocport, c_port; uint16_t retry; +#if defined(LWS_WITH_CLIENT) + uint16_t keep_warm_secs; +#endif /* chars */ diff --git a/lib/core-net/wsi.c b/lib/core-net/wsi.c index 8f1c83bc8..67ccd7df9 100644 --- a/lib/core-net/wsi.c +++ b/lib/core-net/wsi.c @@ -798,7 +798,8 @@ _lws_generic_transaction_completed_active_conn(struct lws **_wsi, char take_vh_l lwsl_info("%s: nothing pipelined waiting\n", __func__); lwsi_set_state(wsi, LRS_IDLING); - lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_CONN_IDLE, 5); + lws_set_timeout(wsi, PENDING_TIMEOUT_CLIENT_CONN_IDLE, + wsi->keep_warm_secs); return 0; /* no new transaction right now */ } @@ -873,7 +874,7 @@ _lws_generic_transaction_completed_active_conn(struct lws **_wsi, char take_vh_l wnew->cli_hostname_copy = wsi->cli_hostname_copy; wsi->cli_hostname_copy = NULL; - + wnew->keep_warm_secs = wsi->keep_warm_secs; /* * selected queued guy now replaces the original leader on the