conncache.c 39 KB

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