conncache.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Linus Nielsen Feltzing, <[email protected]>
  9. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #include "url.h"
  29. #include "cfilters.h"
  30. #include "progress.h"
  31. #include "multiif.h"
  32. #include "sendf.h"
  33. #include "conncache.h"
  34. #include "http_negotiate.h"
  35. #include "http_ntlm.h"
  36. #include "share.h"
  37. #include "sigpipe.h"
  38. #include "connect.h"
  39. #include "select.h"
  40. #include "strcase.h"
  41. /* The last 3 #include files should be in this order */
  42. #include "curl_printf.h"
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. #define CPOOL_IS_LOCKED(c) ((c) && (c)->locked)
  46. #define CPOOL_LOCK(c) \
  47. do { \
  48. if((c)) { \
  49. if(CURL_SHARE_KEEP_CONNECT((c)->share)) \
  50. Curl_share_lock(((c)->idata), CURL_LOCK_DATA_CONNECT, \
  51. CURL_LOCK_ACCESS_SINGLE); \
  52. DEBUGASSERT(!(c)->locked); \
  53. (c)->locked = TRUE; \
  54. } \
  55. } while(0)
  56. #define CPOOL_UNLOCK(c) \
  57. do { \
  58. if((c)) { \
  59. DEBUGASSERT((c)->locked); \
  60. (c)->locked = FALSE; \
  61. if(CURL_SHARE_KEEP_CONNECT((c)->share)) \
  62. Curl_share_unlock((c)->idata, CURL_LOCK_DATA_CONNECT); \
  63. } \
  64. } while(0)
  65. /* A list of connections to the same destination. */
  66. struct cpool_bundle {
  67. struct Curl_llist conns; /* connections in the bundle */
  68. size_t dest_len; /* total length of destination, including NUL */
  69. char *dest[1]; /* destination of bundle, allocated to keep dest_len bytes */
  70. };
  71. static void cpool_discard_conn(struct cpool *cpool,
  72. struct Curl_easy *data,
  73. struct connectdata *conn,
  74. bool aborted);
  75. static void cpool_close_and_destroy(struct cpool *cpool,
  76. struct connectdata *conn,
  77. struct Curl_easy *data,
  78. bool do_shutdown);
  79. static void cpool_run_conn_shutdown(struct Curl_easy *data,
  80. struct connectdata *conn,
  81. bool *done);
  82. static void cpool_run_conn_shutdown_handler(struct Curl_easy *data,
  83. struct connectdata *conn);
  84. static CURLMcode cpool_update_shutdown_ev(struct Curl_multi *multi,
  85. struct Curl_easy *data,
  86. struct connectdata *conn);
  87. static void cpool_shutdown_all(struct cpool *cpool,
  88. struct Curl_easy *data, int timeout_ms);
  89. static void cpool_close_and_destroy_all(struct cpool *cpool);
  90. static struct connectdata *cpool_get_oldest_idle(struct cpool *cpool);
  91. static size_t cpool_shutdown_dest_count(struct cpool *cpool,
  92. const char *destination);
  93. static struct cpool_bundle *cpool_bundle_create(const char *dest,
  94. size_t dest_len)
  95. {
  96. struct cpool_bundle *bundle;
  97. bundle = calloc(1, sizeof(*bundle) + dest_len);
  98. if(!bundle)
  99. return NULL;
  100. Curl_llist_init(&bundle->conns, NULL);
  101. bundle->dest_len = dest_len;
  102. memcpy(bundle->dest, dest, dest_len);
  103. return bundle;
  104. }
  105. static void cpool_bundle_destroy(struct cpool_bundle *bundle)
  106. {
  107. DEBUGASSERT(!Curl_llist_count(&bundle->conns));
  108. free(bundle);
  109. }
  110. /* Add a connection to a bundle */
  111. static void cpool_bundle_add(struct cpool_bundle *bundle,
  112. struct connectdata *conn)
  113. {
  114. DEBUGASSERT(!Curl_node_llist(&conn->cpool_node));
  115. Curl_llist_append(&bundle->conns, conn, &conn->cpool_node);
  116. conn->bits.in_cpool = TRUE;
  117. }
  118. /* Remove a connection from a bundle */
  119. static void cpool_bundle_remove(struct cpool_bundle *bundle,
  120. struct connectdata *conn)
  121. {
  122. (void)bundle;
  123. DEBUGASSERT(Curl_node_llist(&conn->cpool_node) == &bundle->conns);
  124. Curl_node_remove(&conn->cpool_node);
  125. conn->bits.in_cpool = FALSE;
  126. }
  127. static void cpool_bundle_free_entry(void *freethis)
  128. {
  129. cpool_bundle_destroy((struct cpool_bundle *)freethis);
  130. }
  131. int Curl_cpool_init(struct cpool *cpool,
  132. Curl_cpool_disconnect_cb *disconnect_cb,
  133. struct Curl_multi *multi,
  134. struct Curl_share *share,
  135. size_t size)
  136. {
  137. DEBUGASSERT(!!multi != !!share); /* either one */
  138. Curl_hash_init(&cpool->dest2bundle, size, Curl_hash_str,
  139. Curl_str_key_compare, cpool_bundle_free_entry);
  140. Curl_llist_init(&cpool->shutdowns, NULL);
  141. DEBUGASSERT(disconnect_cb);
  142. if(!disconnect_cb)
  143. return 1;
  144. /* allocate a new easy handle to use when closing cached connections */
  145. cpool->idata = curl_easy_init();
  146. if(!cpool->idata)
  147. return 1; /* bad */
  148. cpool->idata->state.internal = TRUE;
  149. /* TODO: this is quirky. We need an internal handle for certain
  150. * operations, but we do not add it to the multi (if there is one).
  151. * But we give it the multi so that socket event operations can work.
  152. * Probably better to have an internal handle owned by the multi that
  153. * can be used for cpool operations. */
  154. cpool->idata->multi = multi;
  155. #ifdef DEBUGBUILD
  156. if(getenv("CURL_DEBUG"))
  157. cpool->idata->set.verbose = TRUE;
  158. #endif
  159. cpool->disconnect_cb = disconnect_cb;
  160. cpool->idata->multi = cpool->multi = multi;
  161. cpool->idata->share = cpool->share = share;
  162. return 0; /* good */
  163. }
  164. void Curl_cpool_destroy(struct cpool *cpool)
  165. {
  166. if(cpool) {
  167. if(cpool->idata) {
  168. cpool_close_and_destroy_all(cpool);
  169. /* The internal closure handle is special and we need to
  170. * disconnect it from multi/share before closing it down. */
  171. cpool->idata->multi = NULL;
  172. cpool->idata->share = NULL;
  173. Curl_close(&cpool->idata);
  174. }
  175. Curl_hash_destroy(&cpool->dest2bundle);
  176. cpool->multi = NULL;
  177. }
  178. }
  179. static struct cpool *cpool_get_instance(struct Curl_easy *data)
  180. {
  181. if(data) {
  182. if(CURL_SHARE_KEEP_CONNECT(data->share))
  183. return &data->share->cpool;
  184. else if(data->multi_easy)
  185. return &data->multi_easy->cpool;
  186. else if(data->multi)
  187. return &data->multi->cpool;
  188. }
  189. return NULL;
  190. }
  191. void Curl_cpool_xfer_init(struct Curl_easy *data)
  192. {
  193. struct cpool *cpool = cpool_get_instance(data);
  194. DEBUGASSERT(cpool);
  195. if(cpool) {
  196. CPOOL_LOCK(cpool);
  197. /* the identifier inside the connection cache */
  198. data->id = cpool->next_easy_id++;
  199. if(cpool->next_easy_id <= 0)
  200. cpool->next_easy_id = 0;
  201. data->state.lastconnect_id = -1;
  202. /* The closure handle only ever has default timeouts set. To improve the
  203. state somewhat we clone the timeouts from each added handle so that the
  204. closure handle always has the same timeouts as the most recently added
  205. easy handle. */
  206. cpool->idata->set.timeout = data->set.timeout;
  207. cpool->idata->set.server_response_timeout =
  208. data->set.server_response_timeout;
  209. cpool->idata->set.no_signal = data->set.no_signal;
  210. CPOOL_UNLOCK(cpool);
  211. }
  212. else {
  213. /* We should not get here, but in a non-debug build, do something */
  214. data->id = 0;
  215. data->state.lastconnect_id = -1;
  216. }
  217. }
  218. static struct cpool_bundle *cpool_find_bundle(struct cpool *cpool,
  219. struct connectdata *conn)
  220. {
  221. return Curl_hash_pick(&cpool->dest2bundle,
  222. conn->destination, conn->destination_len);
  223. }
  224. static struct cpool_bundle *
  225. cpool_add_bundle(struct cpool *cpool, struct connectdata *conn)
  226. {
  227. struct cpool_bundle *bundle;
  228. bundle = cpool_bundle_create(conn->destination, conn->destination_len);
  229. if(!bundle)
  230. return NULL;
  231. if(!Curl_hash_add(&cpool->dest2bundle,
  232. bundle->dest, bundle->dest_len, bundle)) {
  233. cpool_bundle_destroy(bundle);
  234. return NULL;
  235. }
  236. return bundle;
  237. }
  238. static void cpool_remove_bundle(struct cpool *cpool,
  239. struct cpool_bundle *bundle)
  240. {
  241. if(!cpool)
  242. return;
  243. Curl_hash_delete(&cpool->dest2bundle, bundle->dest, bundle->dest_len);
  244. }
  245. static struct connectdata *
  246. cpool_bundle_get_oldest_idle(struct cpool_bundle *bundle);
  247. int Curl_cpool_check_limits(struct Curl_easy *data,
  248. struct connectdata *conn)
  249. {
  250. struct cpool *cpool = cpool_get_instance(data);
  251. struct cpool_bundle *bundle;
  252. size_t dest_limit = 0;
  253. size_t total_limit = 0;
  254. size_t shutdowns;
  255. int result = CPOOL_LIMIT_OK;
  256. if(!cpool)
  257. return CPOOL_LIMIT_OK;
  258. if(data && data->multi) {
  259. dest_limit = data->multi->max_host_connections;
  260. total_limit = data->multi->max_total_connections;
  261. }
  262. if(!dest_limit && !total_limit)
  263. return CPOOL_LIMIT_OK;
  264. CPOOL_LOCK(cpool);
  265. if(dest_limit) {
  266. size_t live;
  267. bundle = cpool_find_bundle(cpool, conn);
  268. live = bundle ? Curl_llist_count(&bundle->conns) : 0;
  269. shutdowns = cpool_shutdown_dest_count(cpool, conn->destination);
  270. while(!shutdowns && bundle && live >= dest_limit) {
  271. struct connectdata *oldest_idle = NULL;
  272. /* The bundle is full. Extract the oldest connection that may
  273. * be removed now, if there is one. */
  274. oldest_idle = cpool_bundle_get_oldest_idle(bundle);
  275. if(!oldest_idle)
  276. break;
  277. /* disconnect the old conn and continue */
  278. DEBUGF(infof(data, "Discarding connection #%"
  279. FMT_OFF_T " from %zu to reach destination "
  280. "limit of %zu", oldest_idle->connection_id,
  281. Curl_llist_count(&bundle->conns), dest_limit));
  282. Curl_cpool_disconnect(data, oldest_idle, FALSE);
  283. /* in case the bundle was destroyed in disconnect, look it up again */
  284. bundle = cpool_find_bundle(cpool, conn);
  285. live = bundle ? Curl_llist_count(&bundle->conns) : 0;
  286. shutdowns = cpool_shutdown_dest_count(cpool, conn->destination);
  287. }
  288. if((live + shutdowns) >= dest_limit) {
  289. result = CPOOL_LIMIT_DEST;
  290. goto out;
  291. }
  292. }
  293. if(total_limit) {
  294. shutdowns = Curl_llist_count(&cpool->shutdowns);
  295. while((cpool->num_conn + shutdowns) >= total_limit) {
  296. struct connectdata *oldest_idle = cpool_get_oldest_idle(cpool);
  297. if(!oldest_idle)
  298. break;
  299. /* disconnect the old conn and continue */
  300. DEBUGF(infof(data, "Discarding connection #%"
  301. FMT_OFF_T " from %zu to reach total "
  302. "limit of %zu",
  303. oldest_idle->connection_id, cpool->num_conn, total_limit));
  304. Curl_cpool_disconnect(data, oldest_idle, FALSE);
  305. shutdowns = Curl_llist_count(&cpool->shutdowns);
  306. }
  307. if((cpool->num_conn + shutdowns) >= total_limit) {
  308. result = CPOOL_LIMIT_TOTAL;
  309. goto out;
  310. }
  311. }
  312. out:
  313. CPOOL_UNLOCK(cpool);
  314. return result;
  315. }
  316. CURLcode Curl_cpool_add_conn(struct Curl_easy *data,
  317. struct connectdata *conn)
  318. {
  319. CURLcode result = CURLE_OK;
  320. struct cpool_bundle *bundle = NULL;
  321. struct cpool *cpool = cpool_get_instance(data);
  322. DEBUGASSERT(conn);
  323. DEBUGASSERT(cpool);
  324. if(!cpool)
  325. return CURLE_FAILED_INIT;
  326. CPOOL_LOCK(cpool);
  327. bundle = cpool_find_bundle(cpool, conn);
  328. if(!bundle) {
  329. bundle = cpool_add_bundle(cpool, conn);
  330. if(!bundle) {
  331. result = CURLE_OUT_OF_MEMORY;
  332. goto out;
  333. }
  334. }
  335. cpool_bundle_add(bundle, conn);
  336. conn->connection_id = cpool->next_connection_id++;
  337. cpool->num_conn++;
  338. DEBUGF(infof(data, "Added connection %" FMT_OFF_T ". "
  339. "The cache now contains %zu members",
  340. conn->connection_id,
  341. cpool->num_conn + Curl_llist_count(&cpool->shutdowns)));
  342. out:
  343. CPOOL_UNLOCK(cpool);
  344. return result;
  345. }
  346. static void cpool_remove_conn(struct cpool *cpool,
  347. struct connectdata *conn)
  348. {
  349. struct Curl_llist *list = Curl_node_llist(&conn->cpool_node);
  350. DEBUGASSERT(cpool);
  351. if(list) {
  352. /* The connection is certainly in the pool, but where? */
  353. struct cpool_bundle *bundle = cpool_find_bundle(cpool, conn);
  354. if(bundle && (list == &bundle->conns)) {
  355. cpool_bundle_remove(bundle, conn);
  356. if(!Curl_llist_count(&bundle->conns))
  357. cpool_remove_bundle(cpool, bundle);
  358. conn->bits.in_cpool = FALSE;
  359. cpool->num_conn--;
  360. }
  361. else {
  362. /* Not in a bundle, already in the shutdown list? */
  363. DEBUGASSERT(list == &cpool->shutdowns);
  364. }
  365. }
  366. }
  367. /* This function iterates the entire connection pool and calls the function
  368. func() with the connection pointer as the first argument and the supplied
  369. 'param' argument as the other.
  370. The cpool lock is still held when the callback is called. It needs it,
  371. so that it can safely continue traversing the lists once the callback
  372. returns.
  373. Returns TRUE if the loop was aborted due to the callback's return code.
  374. Return 0 from func() to continue the loop, return 1 to abort it.
  375. */
  376. static bool cpool_foreach(struct Curl_easy *data,
  377. struct cpool *cpool,
  378. void *param,
  379. int (*func)(struct Curl_easy *data,
  380. struct connectdata *conn, void *param))
  381. {
  382. struct Curl_hash_iterator iter;
  383. struct Curl_hash_element *he;
  384. if(!cpool)
  385. return FALSE;
  386. Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
  387. he = Curl_hash_next_element(&iter);
  388. while(he) {
  389. struct Curl_llist_node *curr;
  390. struct cpool_bundle *bundle = he->ptr;
  391. he = Curl_hash_next_element(&iter);
  392. curr = Curl_llist_head(&bundle->conns);
  393. while(curr) {
  394. /* Yes, we need to update curr before calling func(), because func()
  395. might decide to remove the connection */
  396. struct connectdata *conn = Curl_node_elem(curr);
  397. curr = Curl_node_next(curr);
  398. if(1 == func(data, conn, param)) {
  399. return TRUE;
  400. }
  401. }
  402. }
  403. return FALSE;
  404. }
  405. /* Return a live connection in the pool or NULL. */
  406. static struct connectdata *cpool_get_live_conn(struct cpool *cpool)
  407. {
  408. struct Curl_hash_iterator iter;
  409. struct Curl_hash_element *he;
  410. struct cpool_bundle *bundle;
  411. struct Curl_llist_node *conn_node;
  412. Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
  413. for(he = Curl_hash_next_element(&iter); he;
  414. he = Curl_hash_next_element(&iter)) {
  415. bundle = he->ptr;
  416. conn_node = Curl_llist_head(&bundle->conns);
  417. if(conn_node)
  418. return Curl_node_elem(conn_node);
  419. }
  420. return NULL;
  421. }
  422. /*
  423. * A connection (already in the pool) has become idle. Do any
  424. * cleanups in regard to the pool's limits.
  425. *
  426. * Return TRUE if idle connection kept in pool, FALSE if closed.
  427. */
  428. bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
  429. struct connectdata *conn)
  430. {
  431. unsigned int maxconnects = !data->multi->maxconnects ?
  432. data->multi->num_easy * 4 : data->multi->maxconnects;
  433. struct connectdata *oldest_idle = NULL;
  434. struct cpool *cpool = cpool_get_instance(data);
  435. bool kept = TRUE;
  436. conn->lastused = Curl_now(); /* it was used up until now */
  437. if(cpool && maxconnects) {
  438. /* may be called form a callback already under lock */
  439. bool do_lock = !CPOOL_IS_LOCKED(cpool);
  440. if(do_lock)
  441. CPOOL_LOCK(cpool);
  442. if(cpool->num_conn > maxconnects) {
  443. infof(data, "Connection pool is full, closing the oldest one");
  444. oldest_idle = cpool_get_oldest_idle(cpool);
  445. kept = (oldest_idle != conn);
  446. if(oldest_idle) {
  447. Curl_cpool_disconnect(cpool->idata, oldest_idle, FALSE);
  448. }
  449. }
  450. if(do_lock)
  451. CPOOL_UNLOCK(cpool);
  452. }
  453. return kept;
  454. }
  455. /*
  456. * This function finds the connection in the connection bundle that has been
  457. * unused for the longest time.
  458. */
  459. static struct connectdata *
  460. cpool_bundle_get_oldest_idle(struct cpool_bundle *bundle)
  461. {
  462. struct Curl_llist_node *curr;
  463. timediff_t highscore = -1;
  464. timediff_t score;
  465. struct curltime now;
  466. struct connectdata *oldest_idle = NULL;
  467. struct connectdata *conn;
  468. now = Curl_now();
  469. curr = Curl_llist_head(&bundle->conns);
  470. while(curr) {
  471. conn = Curl_node_elem(curr);
  472. if(!CONN_INUSE(conn)) {
  473. /* Set higher score for the age passed since the connection was used */
  474. score = Curl_timediff(now, conn->lastused);
  475. if(score > highscore) {
  476. highscore = score;
  477. oldest_idle = conn;
  478. }
  479. }
  480. curr = Curl_node_next(curr);
  481. }
  482. return oldest_idle;
  483. }
  484. static struct connectdata *cpool_get_oldest_idle(struct cpool *cpool)
  485. {
  486. struct Curl_hash_iterator iter;
  487. struct Curl_llist_node *curr;
  488. struct Curl_hash_element *he;
  489. struct connectdata *oldest_idle = NULL;
  490. struct cpool_bundle *bundle;
  491. struct curltime now;
  492. timediff_t highscore =- 1;
  493. timediff_t score;
  494. now = Curl_now();
  495. Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
  496. for(he = Curl_hash_next_element(&iter); he;
  497. he = Curl_hash_next_element(&iter)) {
  498. struct connectdata *conn;
  499. bundle = he->ptr;
  500. for(curr = Curl_llist_head(&bundle->conns); curr;
  501. curr = Curl_node_next(curr)) {
  502. conn = Curl_node_elem(curr);
  503. if(CONN_INUSE(conn) || conn->bits.close || conn->connect_only)
  504. continue;
  505. /* Set higher score for the age passed since the connection was used */
  506. score = Curl_timediff(now, conn->lastused);
  507. if(score > highscore) {
  508. highscore = score;
  509. oldest_idle = conn;
  510. }
  511. }
  512. }
  513. return oldest_idle;
  514. }
  515. bool Curl_cpool_find(struct Curl_easy *data,
  516. const char *destination, size_t dest_len,
  517. Curl_cpool_conn_match_cb *conn_cb,
  518. Curl_cpool_done_match_cb *done_cb,
  519. void *userdata)
  520. {
  521. struct cpool *cpool = cpool_get_instance(data);
  522. struct cpool_bundle *bundle;
  523. bool result = FALSE;
  524. DEBUGASSERT(cpool);
  525. DEBUGASSERT(conn_cb);
  526. if(!cpool)
  527. return FALSE;
  528. CPOOL_LOCK(cpool);
  529. bundle = Curl_hash_pick(&cpool->dest2bundle, (void *)destination, dest_len);
  530. if(bundle) {
  531. struct Curl_llist_node *curr = Curl_llist_head(&bundle->conns);
  532. while(curr) {
  533. struct connectdata *conn = Curl_node_elem(curr);
  534. /* Get next node now. callback might discard current */
  535. curr = Curl_node_next(curr);
  536. if(conn_cb(conn, userdata)) {
  537. result = TRUE;
  538. break;
  539. }
  540. }
  541. }
  542. if(done_cb) {
  543. result = done_cb(result, userdata);
  544. }
  545. CPOOL_UNLOCK(cpool);
  546. return result;
  547. }
  548. /* How many connections to the given destination are in shutdown? */
  549. static size_t cpool_shutdown_dest_count(struct cpool *cpool,
  550. const char *destination)
  551. {
  552. size_t n = 0;
  553. struct Curl_llist_node *e = Curl_llist_head(&cpool->shutdowns);
  554. while(e) {
  555. struct connectdata *conn = Curl_node_elem(e);
  556. if(!strcmp(destination, conn->destination))
  557. ++n;
  558. e = Curl_node_next(e);
  559. }
  560. return n;
  561. }
  562. static void cpool_shutdown_discard_all(struct cpool *cpool)
  563. {
  564. struct Curl_llist_node *e = Curl_llist_head(&cpool->shutdowns);
  565. struct connectdata *conn;
  566. if(!e)
  567. return;
  568. DEBUGF(infof(cpool->idata, "cpool_shutdown_discard_all"));
  569. while(e) {
  570. conn = Curl_node_elem(e);
  571. Curl_node_remove(e);
  572. DEBUGF(infof(cpool->idata, "discard connection #%" FMT_OFF_T,
  573. conn->connection_id));
  574. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  575. e = Curl_llist_head(&cpool->shutdowns);
  576. }
  577. }
  578. static void cpool_close_and_destroy_all(struct cpool *cpool)
  579. {
  580. struct connectdata *conn;
  581. int timeout_ms = 0;
  582. SIGPIPE_VARIABLE(pipe_st);
  583. DEBUGASSERT(cpool);
  584. /* Move all connections to the shutdown list */
  585. sigpipe_init(&pipe_st);
  586. CPOOL_LOCK(cpool);
  587. conn = cpool_get_live_conn(cpool);
  588. while(conn) {
  589. cpool_remove_conn(cpool, conn);
  590. sigpipe_apply(cpool->idata, &pipe_st);
  591. connclose(conn, "kill all");
  592. cpool_discard_conn(cpool, cpool->idata, conn, FALSE);
  593. conn = cpool_get_live_conn(cpool);
  594. }
  595. CPOOL_UNLOCK(cpool);
  596. /* Just for testing, run graceful shutdown */
  597. #ifdef DEBUGBUILD
  598. {
  599. char *p = getenv("CURL_GRACEFUL_SHUTDOWN");
  600. if(p) {
  601. long l = strtol(p, NULL, 10);
  602. if(l > 0 && l < INT_MAX)
  603. timeout_ms = (int)l;
  604. }
  605. }
  606. #endif
  607. sigpipe_apply(cpool->idata, &pipe_st);
  608. cpool_shutdown_all(cpool, cpool->idata, timeout_ms);
  609. /* discard all connections in the shutdown list */
  610. cpool_shutdown_discard_all(cpool);
  611. Curl_hostcache_clean(cpool->idata, cpool->idata->dns.hostcache);
  612. sigpipe_restore(&pipe_st);
  613. }
  614. static void cpool_shutdown_destroy_oldest(struct cpool *cpool)
  615. {
  616. struct Curl_llist_node *e;
  617. struct connectdata *conn;
  618. e = Curl_llist_head(&cpool->shutdowns);
  619. if(e) {
  620. SIGPIPE_VARIABLE(pipe_st);
  621. conn = Curl_node_elem(e);
  622. Curl_node_remove(e);
  623. sigpipe_init(&pipe_st);
  624. sigpipe_apply(cpool->idata, &pipe_st);
  625. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  626. sigpipe_restore(&pipe_st);
  627. }
  628. }
  629. static void cpool_discard_conn(struct cpool *cpool,
  630. struct Curl_easy *data,
  631. struct connectdata *conn,
  632. bool aborted)
  633. {
  634. bool done = FALSE;
  635. DEBUGASSERT(data);
  636. DEBUGASSERT(cpool);
  637. DEBUGASSERT(!conn->bits.in_cpool);
  638. /*
  639. * If this connection is not marked to force-close, leave it open if there
  640. * are other users of it
  641. */
  642. if(CONN_INUSE(conn) && !aborted) {
  643. DEBUGF(infof(data, "[CCACHE] not discarding #%" FMT_OFF_T
  644. " still in use by %zu transfers", conn->connection_id,
  645. CONN_INUSE(conn)));
  646. return;
  647. }
  648. /* treat the connection as aborted in CONNECT_ONLY situations, we do
  649. * not know what the APP did with it. */
  650. if(conn->connect_only)
  651. aborted = TRUE;
  652. conn->bits.aborted = aborted;
  653. /* We do not shutdown dead connections. The term 'dead' can be misleading
  654. * here, as we also mark errored connections/transfers as 'dead'.
  655. * If we do a shutdown for an aborted transfer, the server might think
  656. * it was successful otherwise (for example an ftps: upload). This is
  657. * not what we want. */
  658. if(aborted)
  659. done = TRUE;
  660. if(!done) {
  661. /* Attempt to shutdown the connection right away. */
  662. Curl_attach_connection(data, conn);
  663. cpool_run_conn_shutdown(data, conn, &done);
  664. DEBUGF(infof(data, "[CCACHE] shutdown #%" FMT_OFF_T ", done=%d",
  665. conn->connection_id, done));
  666. Curl_detach_connection(data);
  667. }
  668. if(done) {
  669. cpool_close_and_destroy(cpool, conn, data, FALSE);
  670. return;
  671. }
  672. /* Add the connection to our shutdown list for non-blocking shutdown
  673. * during multi processing. */
  674. if(data->multi && data->multi->max_total_connections > 0 &&
  675. (data->multi->max_total_connections <=
  676. (long)(cpool->num_conn + Curl_llist_count(&cpool->shutdowns)))) {
  677. DEBUGF(infof(data, "[CCACHE] discarding oldest shutdown connection "
  678. "due to connection limit of %ld",
  679. data->multi->max_total_connections));
  680. cpool_shutdown_destroy_oldest(cpool);
  681. }
  682. if(data->multi && data->multi->socket_cb) {
  683. DEBUGASSERT(cpool == &data->multi->cpool);
  684. /* Start with an empty shutdown pollset, so out internal closure handle
  685. * is added to the sockets. */
  686. memset(&conn->shutdown_poll, 0, sizeof(conn->shutdown_poll));
  687. if(cpool_update_shutdown_ev(data->multi, cpool->idata, conn)) {
  688. DEBUGF(infof(data, "[CCACHE] update events for shutdown failed, "
  689. "discarding #%" FMT_OFF_T,
  690. conn->connection_id));
  691. cpool_close_and_destroy(cpool, conn, data, FALSE);
  692. return;
  693. }
  694. }
  695. Curl_llist_append(&cpool->shutdowns, conn, &conn->cpool_node);
  696. DEBUGF(infof(data, "[CCACHE] added #%" FMT_OFF_T
  697. " to shutdowns, now %zu conns in shutdown",
  698. conn->connection_id, Curl_llist_count(&cpool->shutdowns)));
  699. }
  700. void Curl_cpool_disconnect(struct Curl_easy *data,
  701. struct connectdata *conn,
  702. bool aborted)
  703. {
  704. struct cpool *cpool = cpool_get_instance(data);
  705. bool do_lock;
  706. DEBUGASSERT(cpool);
  707. DEBUGASSERT(data && !data->conn);
  708. if(!cpool)
  709. return;
  710. /* If this connection is not marked to force-close, leave it open if there
  711. * are other users of it */
  712. if(CONN_INUSE(conn) && !aborted) {
  713. DEBUGASSERT(0); /* does this ever happen? */
  714. DEBUGF(infof(data, "Curl_disconnect when inuse: %zu", CONN_INUSE(conn)));
  715. return;
  716. }
  717. /* This method may be called while we are under lock, e.g. from a
  718. * user callback in find. */
  719. do_lock = !CPOOL_IS_LOCKED(cpool);
  720. if(do_lock)
  721. CPOOL_LOCK(cpool);
  722. if(conn->bits.in_cpool) {
  723. cpool_remove_conn(cpool, conn);
  724. DEBUGASSERT(!conn->bits.in_cpool);
  725. }
  726. /* Run the callback to let it clean up anything it wants to. */
  727. aborted = cpool->disconnect_cb(data, conn, aborted);
  728. if(data->multi) {
  729. /* Add it to the multi's cpool for shutdown handling */
  730. infof(data, "%s connection #%" FMT_OFF_T,
  731. aborted ? "closing" : "shutting down", conn->connection_id);
  732. cpool_discard_conn(&data->multi->cpool, data, conn, aborted);
  733. }
  734. else {
  735. /* No multi available. Make a best-effort shutdown + close */
  736. infof(data, "closing connection #%" FMT_OFF_T, conn->connection_id);
  737. cpool_close_and_destroy(NULL, conn, data, !aborted);
  738. }
  739. if(do_lock)
  740. CPOOL_UNLOCK(cpool);
  741. }
  742. static void cpool_run_conn_shutdown_handler(struct Curl_easy *data,
  743. struct connectdata *conn)
  744. {
  745. if(!conn->bits.shutdown_handler) {
  746. if(conn->dns_entry)
  747. Curl_resolv_unlink(data, &conn->dns_entry);
  748. /* Cleanup NTLM connection-related data */
  749. Curl_http_auth_cleanup_ntlm(conn);
  750. /* Cleanup NEGOTIATE connection-related data */
  751. Curl_http_auth_cleanup_negotiate(conn);
  752. if(conn->handler && conn->handler->disconnect) {
  753. /* This is set if protocol-specific cleanups should be made */
  754. DEBUGF(infof(data, "connection #%" FMT_OFF_T
  755. ", shutdown protocol handler (aborted=%d)",
  756. conn->connection_id, conn->bits.aborted));
  757. conn->handler->disconnect(data, conn, conn->bits.aborted);
  758. }
  759. /* possible left-overs from the async name resolvers */
  760. Curl_resolver_cancel(data);
  761. conn->bits.shutdown_handler = TRUE;
  762. }
  763. }
  764. static void cpool_run_conn_shutdown(struct Curl_easy *data,
  765. struct connectdata *conn,
  766. bool *done)
  767. {
  768. CURLcode r1, r2;
  769. bool done1, done2;
  770. /* We expect to be attached when called */
  771. DEBUGASSERT(data->conn == conn);
  772. cpool_run_conn_shutdown_handler(data, conn);
  773. if(conn->bits.shutdown_filters) {
  774. *done = TRUE;
  775. return;
  776. }
  777. if(!conn->connect_only && Curl_conn_is_connected(conn, FIRSTSOCKET))
  778. r1 = Curl_conn_shutdown(data, FIRSTSOCKET, &done1);
  779. else {
  780. r1 = CURLE_OK;
  781. done1 = TRUE;
  782. }
  783. if(!conn->connect_only && Curl_conn_is_connected(conn, SECONDARYSOCKET))
  784. r2 = Curl_conn_shutdown(data, SECONDARYSOCKET, &done2);
  785. else {
  786. r2 = CURLE_OK;
  787. done2 = TRUE;
  788. }
  789. /* we are done when any failed or both report success */
  790. *done = (r1 || r2 || (done1 && done2));
  791. if(*done)
  792. conn->bits.shutdown_filters = TRUE;
  793. }
  794. static CURLcode cpool_add_pollfds(struct cpool *cpool,
  795. struct curl_pollfds *cpfds)
  796. {
  797. CURLcode result = CURLE_OK;
  798. if(Curl_llist_head(&cpool->shutdowns)) {
  799. struct Curl_llist_node *e;
  800. struct easy_pollset ps;
  801. struct connectdata *conn;
  802. for(e = Curl_llist_head(&cpool->shutdowns); e;
  803. e = Curl_node_next(e)) {
  804. conn = Curl_node_elem(e);
  805. memset(&ps, 0, sizeof(ps));
  806. Curl_attach_connection(cpool->idata, conn);
  807. Curl_conn_adjust_pollset(cpool->idata, &ps);
  808. Curl_detach_connection(cpool->idata);
  809. result = Curl_pollfds_add_ps(cpfds, &ps);
  810. if(result) {
  811. Curl_pollfds_cleanup(cpfds);
  812. goto out;
  813. }
  814. }
  815. }
  816. out:
  817. return result;
  818. }
  819. CURLcode Curl_cpool_add_pollfds(struct cpool *cpool,
  820. struct curl_pollfds *cpfds)
  821. {
  822. CURLcode result;
  823. CPOOL_LOCK(cpool);
  824. result = cpool_add_pollfds(cpool, cpfds);
  825. CPOOL_UNLOCK(cpool);
  826. return result;
  827. }
  828. /* return information about the shutdown connections */
  829. unsigned int Curl_cpool_add_waitfds(struct cpool *cpool,
  830. struct Curl_waitfds *cwfds)
  831. {
  832. unsigned int need = 0;
  833. CPOOL_LOCK(cpool);
  834. if(Curl_llist_head(&cpool->shutdowns)) {
  835. struct Curl_llist_node *e;
  836. struct easy_pollset ps;
  837. struct connectdata *conn;
  838. for(e = Curl_llist_head(&cpool->shutdowns); e;
  839. e = Curl_node_next(e)) {
  840. conn = Curl_node_elem(e);
  841. memset(&ps, 0, sizeof(ps));
  842. Curl_attach_connection(cpool->idata, conn);
  843. Curl_conn_adjust_pollset(cpool->idata, &ps);
  844. Curl_detach_connection(cpool->idata);
  845. need += Curl_waitfds_add_ps(cwfds, &ps);
  846. }
  847. }
  848. CPOOL_UNLOCK(cpool);
  849. return need;
  850. }
  851. /* return fd_set info about the shutdown connections */
  852. void Curl_cpool_setfds(struct cpool *cpool,
  853. fd_set *read_fd_set, fd_set *write_fd_set,
  854. int *maxfd)
  855. {
  856. CPOOL_LOCK(cpool);
  857. if(Curl_llist_head(&cpool->shutdowns)) {
  858. struct Curl_llist_node *e;
  859. for(e = Curl_llist_head(&cpool->shutdowns); e;
  860. e = Curl_node_next(e)) {
  861. struct easy_pollset ps;
  862. unsigned int i;
  863. struct connectdata *conn = Curl_node_elem(e);
  864. memset(&ps, 0, sizeof(ps));
  865. Curl_attach_connection(cpool->idata, conn);
  866. Curl_conn_adjust_pollset(cpool->idata, &ps);
  867. Curl_detach_connection(cpool->idata);
  868. for(i = 0; i < ps.num; i++) {
  869. #if defined(__DJGPP__)
  870. #pragma GCC diagnostic push
  871. #pragma GCC diagnostic ignored "-Warith-conversion"
  872. #endif
  873. if(ps.actions[i] & CURL_POLL_IN)
  874. FD_SET(ps.sockets[i], read_fd_set);
  875. if(ps.actions[i] & CURL_POLL_OUT)
  876. FD_SET(ps.sockets[i], write_fd_set);
  877. #if defined(__DJGPP__)
  878. #pragma GCC diagnostic pop
  879. #endif
  880. if((ps.actions[i] & (CURL_POLL_OUT | CURL_POLL_IN)) &&
  881. ((int)ps.sockets[i] > *maxfd))
  882. *maxfd = (int)ps.sockets[i];
  883. }
  884. }
  885. }
  886. CPOOL_UNLOCK(cpool);
  887. }
  888. static void cpool_perform(struct cpool *cpool)
  889. {
  890. struct Curl_easy *data = cpool->idata;
  891. struct Curl_llist_node *e = Curl_llist_head(&cpool->shutdowns);
  892. struct Curl_llist_node *enext;
  893. struct connectdata *conn;
  894. struct curltime *nowp = NULL;
  895. struct curltime now;
  896. timediff_t next_from_now_ms = 0, ms;
  897. bool done;
  898. if(!e)
  899. return;
  900. DEBUGASSERT(data);
  901. DEBUGF(infof(data, "[CCACHE] perform, %zu connections being shutdown",
  902. Curl_llist_count(&cpool->shutdowns)));
  903. while(e) {
  904. enext = Curl_node_next(e);
  905. conn = Curl_node_elem(e);
  906. Curl_attach_connection(data, conn);
  907. cpool_run_conn_shutdown(data, conn, &done);
  908. DEBUGF(infof(data, "[CCACHE] shutdown #%" FMT_OFF_T ", done=%d",
  909. conn->connection_id, done));
  910. Curl_detach_connection(data);
  911. if(done) {
  912. Curl_node_remove(e);
  913. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  914. }
  915. else {
  916. /* Not done, when does this connection time out? */
  917. if(!nowp) {
  918. now = Curl_now();
  919. nowp = &now;
  920. }
  921. ms = Curl_conn_shutdown_timeleft(conn, nowp);
  922. if(ms && ms < next_from_now_ms)
  923. next_from_now_ms = ms;
  924. }
  925. e = enext;
  926. }
  927. if(next_from_now_ms)
  928. Curl_expire(data, next_from_now_ms, EXPIRE_RUN_NOW);
  929. }
  930. void Curl_cpool_multi_perform(struct Curl_multi *multi)
  931. {
  932. CPOOL_LOCK(&multi->cpool);
  933. cpool_perform(&multi->cpool);
  934. CPOOL_UNLOCK(&multi->cpool);
  935. }
  936. /*
  937. * Close and destroy the connection. Run the shutdown sequence once,
  938. * of so requested.
  939. */
  940. static void cpool_close_and_destroy(struct cpool *cpool,
  941. struct connectdata *conn,
  942. struct Curl_easy *data,
  943. bool do_shutdown)
  944. {
  945. bool done;
  946. /* there must be a connection to close */
  947. DEBUGASSERT(conn);
  948. /* it must be removed from the connection pool */
  949. DEBUGASSERT(!conn->bits.in_cpool);
  950. /* there must be an associated transfer */
  951. DEBUGASSERT(data || cpool);
  952. if(!data)
  953. data = cpool->idata;
  954. /* the transfer must be detached from the connection */
  955. DEBUGASSERT(data && !data->conn);
  956. Curl_attach_connection(data, conn);
  957. cpool_run_conn_shutdown_handler(data, conn);
  958. if(do_shutdown) {
  959. /* Make a last attempt to shutdown handlers and filters, if
  960. * not done so already. */
  961. cpool_run_conn_shutdown(data, conn, &done);
  962. }
  963. if(cpool)
  964. DEBUGF(infof(data, "[CCACHE] closing #%" FMT_OFF_T,
  965. conn->connection_id));
  966. else
  967. DEBUGF(infof(data, "closing connection #%" FMT_OFF_T,
  968. conn->connection_id));
  969. Curl_conn_close(data, SECONDARYSOCKET);
  970. Curl_conn_close(data, FIRSTSOCKET);
  971. Curl_detach_connection(data);
  972. Curl_conn_free(data, conn);
  973. if(cpool && cpool->multi) {
  974. DEBUGF(infof(data, "[CCACHE] trigger multi connchanged"));
  975. Curl_multi_connchanged(cpool->multi);
  976. }
  977. }
  978. static CURLMcode cpool_update_shutdown_ev(struct Curl_multi *multi,
  979. struct Curl_easy *data,
  980. struct connectdata *conn)
  981. {
  982. struct easy_pollset ps;
  983. CURLMcode mresult;
  984. DEBUGASSERT(data);
  985. DEBUGASSERT(multi);
  986. DEBUGASSERT(multi->socket_cb);
  987. memset(&ps, 0, sizeof(ps));
  988. Curl_attach_connection(data, conn);
  989. Curl_conn_adjust_pollset(data, &ps);
  990. Curl_detach_connection(data);
  991. mresult = Curl_multi_pollset_ev(multi, data, &ps, &conn->shutdown_poll);
  992. if(!mresult) /* Remember for next time */
  993. memcpy(&conn->shutdown_poll, &ps, sizeof(ps));
  994. return mresult;
  995. }
  996. void Curl_cpool_multi_socket(struct Curl_multi *multi,
  997. curl_socket_t s, int ev_bitmask)
  998. {
  999. struct cpool *cpool = &multi->cpool;
  1000. struct Curl_easy *data = cpool->idata;
  1001. struct Curl_llist_node *e;
  1002. struct connectdata *conn;
  1003. bool done;
  1004. (void)ev_bitmask;
  1005. DEBUGASSERT(multi->socket_cb);
  1006. CPOOL_LOCK(cpool);
  1007. e = Curl_llist_head(&cpool->shutdowns);
  1008. while(e) {
  1009. conn = Curl_node_elem(e);
  1010. if(s == conn->sock[FIRSTSOCKET] || s == conn->sock[SECONDARYSOCKET]) {
  1011. Curl_attach_connection(data, conn);
  1012. cpool_run_conn_shutdown(data, conn, &done);
  1013. DEBUGF(infof(data, "[CCACHE] shutdown #%" FMT_OFF_T ", done=%d",
  1014. conn->connection_id, done));
  1015. Curl_detach_connection(data);
  1016. if(done || cpool_update_shutdown_ev(multi, data, conn)) {
  1017. Curl_node_remove(e);
  1018. cpool_close_and_destroy(cpool, conn, NULL, FALSE);
  1019. }
  1020. break;
  1021. }
  1022. e = Curl_node_next(e);
  1023. }
  1024. CPOOL_UNLOCK(cpool);
  1025. }
  1026. #define NUM_POLLS_ON_STACK 10
  1027. static CURLcode cpool_shutdown_wait(struct cpool *cpool, int timeout_ms)
  1028. {
  1029. struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
  1030. struct curl_pollfds cpfds;
  1031. CURLcode result;
  1032. Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
  1033. result = cpool_add_pollfds(cpool, &cpfds);
  1034. if(result)
  1035. goto out;
  1036. Curl_poll(cpfds.pfds, cpfds.n, CURLMIN(timeout_ms, 1000));
  1037. out:
  1038. Curl_pollfds_cleanup(&cpfds);
  1039. return result;
  1040. }
  1041. static void cpool_shutdown_all(struct cpool *cpool,
  1042. struct Curl_easy *data, int timeout_ms)
  1043. {
  1044. struct connectdata *conn;
  1045. struct curltime started = Curl_now();
  1046. if(!data)
  1047. return;
  1048. (void)data;
  1049. DEBUGF(infof(data, "cpool shutdown all"));
  1050. /* Move all connections into the shutdown queue */
  1051. for(conn = cpool_get_live_conn(cpool); conn;
  1052. conn = cpool_get_live_conn(cpool)) {
  1053. /* Move conn from live set to shutdown or destroy right away */
  1054. DEBUGF(infof(data, "moving connection #%" FMT_OFF_T
  1055. " to shutdown queue", conn->connection_id));
  1056. cpool_remove_conn(cpool, conn);
  1057. cpool_discard_conn(cpool, data, conn, FALSE);
  1058. }
  1059. while(Curl_llist_head(&cpool->shutdowns)) {
  1060. timediff_t timespent;
  1061. int remain_ms;
  1062. cpool_perform(cpool);
  1063. if(!Curl_llist_head(&cpool->shutdowns)) {
  1064. DEBUGF(infof(data, "cpool shutdown ok"));
  1065. break;
  1066. }
  1067. /* wait for activity, timeout or "nothing" */
  1068. timespent = Curl_timediff(Curl_now(), started);
  1069. if(timespent >= (timediff_t)timeout_ms) {
  1070. DEBUGF(infof(data, "cpool shutdown %s",
  1071. (timeout_ms > 0) ? "timeout" : "best effort done"));
  1072. break;
  1073. }
  1074. remain_ms = timeout_ms - (int)timespent;
  1075. if(cpool_shutdown_wait(cpool, remain_ms)) {
  1076. DEBUGF(infof(data, "cpool shutdown all, abort"));
  1077. break;
  1078. }
  1079. }
  1080. /* Due to errors/timeout, we might come here without being done. */
  1081. cpool_shutdown_discard_all(cpool);
  1082. }
  1083. struct cpool_reaper_ctx {
  1084. struct curltime now;
  1085. };
  1086. static int cpool_reap_dead_cb(struct Curl_easy *data,
  1087. struct connectdata *conn, void *param)
  1088. {
  1089. struct cpool_reaper_ctx *rctx = param;
  1090. if(Curl_conn_seems_dead(conn, data, &rctx->now)) {
  1091. /* stop the iteration here, pass back the connection that was pruned */
  1092. Curl_cpool_disconnect(data, conn, FALSE);
  1093. return 1;
  1094. }
  1095. return 0; /* continue iteration */
  1096. }
  1097. /*
  1098. * This function scans the data's connection pool for half-open/dead
  1099. * connections, closes and removes them.
  1100. * The cleanup is done at most once per second.
  1101. *
  1102. * When called, this transfer has no connection attached.
  1103. */
  1104. void Curl_cpool_prune_dead(struct Curl_easy *data)
  1105. {
  1106. struct cpool *cpool = cpool_get_instance(data);
  1107. struct cpool_reaper_ctx rctx;
  1108. timediff_t elapsed;
  1109. if(!cpool)
  1110. return;
  1111. rctx.now = Curl_now();
  1112. CPOOL_LOCK(cpool);
  1113. elapsed = Curl_timediff(rctx.now, cpool->last_cleanup);
  1114. if(elapsed >= 1000L) {
  1115. while(cpool_foreach(data, cpool, &rctx, cpool_reap_dead_cb))
  1116. ;
  1117. cpool->last_cleanup = rctx.now;
  1118. }
  1119. CPOOL_UNLOCK(cpool);
  1120. }
  1121. static int conn_upkeep(struct Curl_easy *data,
  1122. struct connectdata *conn,
  1123. void *param)
  1124. {
  1125. struct curltime *now = param;
  1126. /* TODO, shall we reap connections that return an error here? */
  1127. Curl_conn_upkeep(data, conn, now);
  1128. return 0; /* continue iteration */
  1129. }
  1130. CURLcode Curl_cpool_upkeep(void *data)
  1131. {
  1132. struct cpool *cpool = cpool_get_instance(data);
  1133. struct curltime now = Curl_now();
  1134. if(!cpool)
  1135. return CURLE_OK;
  1136. CPOOL_LOCK(cpool);
  1137. cpool_foreach(data, cpool, &now, conn_upkeep);
  1138. CPOOL_UNLOCK(cpool);
  1139. return CURLE_OK;
  1140. }
  1141. struct cpool_find_ctx {
  1142. curl_off_t id;
  1143. struct connectdata *conn;
  1144. };
  1145. static int cpool_find_conn(struct Curl_easy *data,
  1146. struct connectdata *conn, void *param)
  1147. {
  1148. struct cpool_find_ctx *fctx = param;
  1149. (void)data;
  1150. if(conn->connection_id == fctx->id) {
  1151. fctx->conn = conn;
  1152. return 1;
  1153. }
  1154. return 0;
  1155. }
  1156. struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
  1157. curl_off_t conn_id)
  1158. {
  1159. struct cpool *cpool = cpool_get_instance(data);
  1160. struct cpool_find_ctx fctx;
  1161. if(!cpool)
  1162. return NULL;
  1163. fctx.id = conn_id;
  1164. fctx.conn = NULL;
  1165. CPOOL_LOCK(cpool);
  1166. cpool_foreach(cpool->idata, cpool, &fctx, cpool_find_conn);
  1167. CPOOL_UNLOCK(cpool);
  1168. return fctx.conn;
  1169. }
  1170. struct cpool_do_conn_ctx {
  1171. curl_off_t id;
  1172. Curl_cpool_conn_do_cb *cb;
  1173. void *cbdata;
  1174. };
  1175. static int cpool_do_conn(struct Curl_easy *data,
  1176. struct connectdata *conn, void *param)
  1177. {
  1178. struct cpool_do_conn_ctx *dctx = param;
  1179. (void)data;
  1180. if(conn->connection_id == dctx->id) {
  1181. dctx->cb(conn, data, dctx->cbdata);
  1182. return 1;
  1183. }
  1184. return 0;
  1185. }
  1186. void Curl_cpool_do_by_id(struct Curl_easy *data, curl_off_t conn_id,
  1187. Curl_cpool_conn_do_cb *cb, void *cbdata)
  1188. {
  1189. struct cpool *cpool = cpool_get_instance(data);
  1190. struct cpool_do_conn_ctx dctx;
  1191. if(!cpool)
  1192. return;
  1193. dctx.id = conn_id;
  1194. dctx.cb = cb;
  1195. dctx.cbdata = cbdata;
  1196. CPOOL_LOCK(cpool);
  1197. cpool_foreach(data, cpool, &dctx, cpool_do_conn);
  1198. CPOOL_UNLOCK(cpool);
  1199. }
  1200. void Curl_cpool_do_locked(struct Curl_easy *data,
  1201. struct connectdata *conn,
  1202. Curl_cpool_conn_do_cb *cb, void *cbdata)
  1203. {
  1204. struct cpool *cpool = cpool_get_instance(data);
  1205. if(cpool) {
  1206. CPOOL_LOCK(cpool);
  1207. cb(conn, data, cbdata);
  1208. CPOOL_UNLOCK(cpool);
  1209. }
  1210. else
  1211. cb(conn, data, cbdata);
  1212. }
  1213. #if 0
  1214. /* Useful for debugging the connection pool */
  1215. void Curl_cpool_print(struct cpool *cpool)
  1216. {
  1217. struct Curl_hash_iterator iter;
  1218. struct Curl_llist_node *curr;
  1219. struct Curl_hash_element *he;
  1220. if(!cpool)
  1221. return;
  1222. fprintf(stderr, "=Bundle cache=\n");
  1223. Curl_hash_start_iterate(cpool->dest2bundle, &iter);
  1224. he = Curl_hash_next_element(&iter);
  1225. while(he) {
  1226. struct cpool_bundle *bundle;
  1227. struct connectdata *conn;
  1228. bundle = he->ptr;
  1229. fprintf(stderr, "%s -", he->key);
  1230. curr = Curl_llist_head(bundle->conns);
  1231. while(curr) {
  1232. conn = Curl_node_elem(curr);
  1233. fprintf(stderr, " [%p %d]", (void *)conn, conn->refcount);
  1234. curr = Curl_node_next(curr);
  1235. }
  1236. fprintf(stderr, "\n");
  1237. he = Curl_hash_next_element(&iter);
  1238. }
  1239. }
  1240. #endif