1d4afb5ceSopenharmony_ci# Implementation background 2d4afb5ceSopenharmony_ci 3d4afb5ceSopenharmony_ci## Client connection Queueing 4d4afb5ceSopenharmony_ci 5d4afb5ceSopenharmony_ciBy default lws treats each client connection as completely separate, and each is 6d4afb5ceSopenharmony_cimade from scratch with its own network connection independently. 7d4afb5ceSopenharmony_ci 8d4afb5ceSopenharmony_ciIf the user code sets the `LCCSCF_PIPELINE` bit on `info.ssl_connection` when 9d4afb5ceSopenharmony_cicreating the client connection though, lws attempts to optimize multiple client 10d4afb5ceSopenharmony_ciconnections to the same place by sharing any existing connection and its tls 11d4afb5ceSopenharmony_citunnel where possible. 12d4afb5ceSopenharmony_ci 13d4afb5ceSopenharmony_ciThere are two basic approaches, for h1 additional connections of the same type 14d4afb5ceSopenharmony_ciand endpoint basically queue on a leader and happen sequentially. 15d4afb5ceSopenharmony_ci 16d4afb5ceSopenharmony_ciFor muxed protocols like h2, they may also queue if the initial connection is 17d4afb5ceSopenharmony_cinot up yet, but subsequently the will all join the existing connection 18d4afb5ceSopenharmony_cisimultaneously "broadside". 19d4afb5ceSopenharmony_ci 20d4afb5ceSopenharmony_ci## h1 queueing 21d4afb5ceSopenharmony_ci 22d4afb5ceSopenharmony_ciThe initial wsi to start the network connection becomes the "leader" that 23d4afb5ceSopenharmony_cisubsequent connection attempts will queue against. Each vhost has a dll2_owner 24d4afb5ceSopenharmony_ci`wsi->dll_cli_active_conns_owner` that "leaders" who are actually making network 25d4afb5ceSopenharmony_ciconnections themselves can register on as "active client connections". 26d4afb5ceSopenharmony_ci 27d4afb5ceSopenharmony_ciOther client wsi being created who find there is already a leader on the active 28d4afb5ceSopenharmony_ciclient connection list for the vhost, can join their dll2 wsi->dll2_cli_txn_queue 29d4afb5ceSopenharmony_cito the leader's wsi->dll2_cli_txn_queue_owner to "queue" on the leader. 30d4afb5ceSopenharmony_ci 31d4afb5ceSopenharmony_ciThe user code does not know which wsi was first or is queued, it just waits for 32d4afb5ceSopenharmony_cistuff to happen the same either way. 33d4afb5ceSopenharmony_ci 34d4afb5ceSopenharmony_ciWhen the "leader" wsi connects, it performs its client transaction as normal, 35d4afb5ceSopenharmony_ciand at the end arrives at `lws_http_transaction_completed_client()`. Here, it 36d4afb5ceSopenharmony_cicalls through to the lws_mux `_lws_generic_transaction_completed_active_conn()` 37d4afb5ceSopenharmony_cihelper. This helper sees if anything else is queued, and if so, migrates assets 38d4afb5ceSopenharmony_cilike the SSL *, the socket fd, and any remaining queue from the original leader 39d4afb5ceSopenharmony_cito the head of the list, which replaces the old leader as the "active client 40d4afb5ceSopenharmony_ciconnection" any subsequent connects would queue on. 41d4afb5ceSopenharmony_ci 42d4afb5ceSopenharmony_ciIt has to be done this way so that user code which may know each client wsi by 43d4afb5ceSopenharmony_ciits wsi, or have marked it with an opaque_user_data pointer, is getting its 44d4afb5ceSopenharmony_cispecific request handled by the wsi it expects it to be handled by. 45d4afb5ceSopenharmony_ci 46d4afb5ceSopenharmony_ciA side effect of this, and in order to be able to handle POSTs cleanly, lws 47d4afb5ceSopenharmony_cidoes not attempt to send the headers for the next queued child before the 48d4afb5ceSopenharmony_ciprevious child has finished. 49d4afb5ceSopenharmony_ci 50d4afb5ceSopenharmony_ciThe process of moving the SSL context and fd etc between the queued wsi continues 51d4afb5ceSopenharmony_ciuntil the queue is all handled. 52d4afb5ceSopenharmony_ci 53d4afb5ceSopenharmony_ci## muxed protocol queueing and stream binding 54d4afb5ceSopenharmony_ci 55d4afb5ceSopenharmony_cih2 connections act the same as h1 before the initial connection has been made, 56d4afb5ceSopenharmony_cibut once it is made all the queued connections join the network connection as 57d4afb5ceSopenharmony_cichild mux streams immediately, "broadside", binding the stream to the existing 58d4afb5ceSopenharmony_cinetwork connection. 59