Coverage Report

Created: 2026-09-04 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/conncache.c
Line
Count
Source
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
27
#include "urldata.h"
28
#include "url.h"
29
#include "cfilters.h"
30
#include "progress.h"
31
#include "multiif.h"
32
#include "curl_trc.h"
33
#include "cshutdn.h"
34
#include "conncache.h"
35
#include "curl_share.h"
36
#include "sigpipe.h"
37
38
39
3.15k
#define CPOOL_IS_LOCKED(c)    ((c) && (c)->locked)
40
41
#define CPOOL_LOCK(c, d)                                                \
42
20.6k
  do {                                                                  \
43
20.6k
    if(c) {                                                             \
44
20.6k
      if(CURL_SHARE_KEEP_CONNECT((c)->share))                           \
45
20.6k
        Curl_share_lock_share((c)->share, (d), CURL_LOCK_DATA_CONNECT,  \
46
0
                        CURL_LOCK_ACCESS_SINGLE);                       \
47
20.6k
      DEBUGASSERT(!(c)->locked);                                        \
48
20.6k
      (c)->locked = TRUE;                                               \
49
20.6k
    }                                                                   \
50
20.6k
  } while(0)
51
52
#define CPOOL_UNLOCK(c, d)                                              \
53
20.6k
  do {                                                                  \
54
20.6k
    if(c) {                                                             \
55
20.6k
      DEBUGASSERT((c)->locked);                                         \
56
20.6k
      (c)->locked = FALSE;                                              \
57
20.6k
      if(CURL_SHARE_KEEP_CONNECT((c)->share))                           \
58
20.6k
        Curl_share_unlock_share((c)->share, (d), CURL_LOCK_DATA_CONNECT); \
59
20.6k
    }                                                                   \
60
20.6k
  } while(0)
61
62
/* A list of connections to the same destination. */
63
struct cpool_bundle {
64
  struct Curl_llist conns; /* connections in the bundle */
65
  size_t dest_len; /* total length of destination, including NUL */
66
  char dest[1]; /* destination of bundle, allocated to keep dest_len bytes */
67
};
68
69
static struct cpool_bundle *cpool_bundle_create(const char *dest)
70
3.15k
{
71
3.15k
  struct cpool_bundle *bundle;
72
3.15k
  size_t dest_len = strlen(dest) + 1;
73
74
3.15k
  bundle = curlx_calloc(1, sizeof(*bundle) + dest_len - 1);
75
3.15k
  if(!bundle)
76
0
    return NULL;
77
3.15k
  Curl_llist_init(&bundle->conns, NULL);
78
3.15k
  bundle->dest_len = dest_len;
79
3.15k
  memcpy(bundle->dest, dest, bundle->dest_len);
80
3.15k
  return bundle;
81
3.15k
}
82
83
static void cpool_bundle_destroy(struct cpool_bundle *bundle)
84
3.15k
{
85
3.15k
  DEBUGASSERT(!Curl_llist_count(&bundle->conns));
86
3.15k
  curlx_free(bundle);
87
3.15k
}
88
89
/* Add a connection to a bundle */
90
static void cpool_bundle_add(struct cpool_bundle *bundle,
91
                             struct connectdata *conn)
92
3.15k
{
93
3.15k
  DEBUGASSERT(!Curl_node_llist(&conn->cpool_node));
94
3.15k
  Curl_llist_append(&bundle->conns, conn, &conn->cpool_node);
95
3.15k
  conn->bits.in_cpool = TRUE;
96
3.15k
}
97
98
/* Remove a connection from a bundle */
99
static void cpool_bundle_remove(struct cpool_bundle *bundle,
100
                                struct connectdata *conn)
101
3.15k
{
102
3.15k
  (void)bundle;
103
3.15k
  DEBUGASSERT(Curl_node_llist(&conn->cpool_node) == &bundle->conns);
104
3.15k
  Curl_node_remove(&conn->cpool_node);
105
3.15k
  conn->bits.in_cpool = FALSE;
106
3.15k
}
107
108
static void cpool_bundle_free_entry(void *freethis)
109
3.15k
{
110
3.15k
  cpool_bundle_destroy((struct cpool_bundle *)freethis);
111
3.15k
}
112
113
void Curl_cpool_init(struct cpool *cpool,
114
                     struct Curl_share *share,
115
                     size_t size)
116
4.00k
{
117
4.00k
  Curl_hash_init(&cpool->dest2bundle, size, Curl_hash_str,
118
4.00k
                 curlx_str_key_compare, cpool_bundle_free_entry);
119
120
4.00k
  cpool->share = share;
121
4.00k
  cpool->initialized = TRUE;
122
4.00k
}
123
124
/* Return the "first" connection in the pool or NULL. */
125
static struct connectdata *cpool_get_first(struct cpool *cpool)
126
4.00k
{
127
4.00k
  struct Curl_hash_iterator iter;
128
4.00k
  struct Curl_hash_element *he;
129
4.00k
  struct cpool_bundle *bundle;
130
4.00k
  struct Curl_llist_node *conn_node;
131
132
4.00k
  Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
133
4.00k
  for(he = Curl_hash_next_element(&iter); he;
134
4.00k
      he = Curl_hash_next_element(&iter)) {
135
0
    bundle = he->ptr;
136
0
    conn_node = Curl_llist_head(&bundle->conns);
137
0
    if(conn_node)
138
0
      return Curl_node_elem(conn_node);
139
0
  }
140
4.00k
  return NULL;
141
4.00k
}
142
143
static struct cpool_bundle *cpool_find_bundle(struct cpool *cpool,
144
                                              struct connectdata *conn)
145
6.31k
{
146
6.31k
  return Curl_hash_pick(&cpool->dest2bundle,
147
6.31k
                        conn->destination, strlen(conn->destination) + 1);
148
6.31k
}
149
150
static void cpool_remove_bundle(struct cpool *cpool,
151
                                struct cpool_bundle *bundle)
152
3.15k
{
153
3.15k
  if(!cpool)
154
0
    return;
155
3.15k
  Curl_hash_delete(&cpool->dest2bundle, bundle->dest, bundle->dest_len);
156
3.15k
}
157
158
static void cpool_remove_conn(struct cpool *cpool,
159
                              struct connectdata *conn)
160
3.15k
{
161
3.15k
  struct Curl_llist *list = Curl_node_llist(&conn->cpool_node);
162
3.15k
  DEBUGASSERT(cpool);
163
3.15k
  if(list) {
164
    /* The connection is certainly in the pool, but where? */
165
3.15k
    struct cpool_bundle *bundle = cpool_find_bundle(cpool, conn);
166
3.15k
    if(bundle && (list == &bundle->conns)) {
167
3.15k
      cpool_bundle_remove(bundle, conn);
168
3.15k
      if(!Curl_llist_count(&bundle->conns))
169
3.15k
        cpool_remove_bundle(cpool, bundle);
170
3.15k
      conn->bits.in_cpool = FALSE;
171
3.15k
      cpool->num_conn--;
172
3.15k
    }
173
0
    else {
174
      /* Should have been in the bundle list */
175
0
      DEBUGASSERT(NULL);
176
0
    }
177
3.15k
  }
178
3.15k
}
179
180
static void cpool_discard_conn(struct cpool *cpool,
181
                               struct Curl_easy *data,
182
                               struct connectdata *conn,
183
                               bool aborted)
184
3.15k
{
185
3.15k
  struct cshutdn *cshutdn;
186
3.15k
  struct Curl_easy *admin;
187
3.15k
  bool done = FALSE;
188
189
3.15k
  DEBUGASSERT(data);
190
3.15k
  DEBUGASSERT(!data->conn);
191
3.15k
  DEBUGASSERT(cpool);
192
3.15k
  DEBUGASSERT(!conn->bits.in_cpool);
193
194
3.15k
  admin = Curl_get_admin(data);
195
  /*
196
   * If this connection is not marked to force-close, leave it open if there
197
   * are other users of it
198
   */
199
3.15k
  if(CONN_INUSE(conn) && !aborted) {
200
0
    CURL_TRC_M(admin, "[CPOOL] not discarding #%" FMT_OFF_T
201
0
               " still in use by %u transfers", conn->connection_id,
202
0
               conn->attached_xfers);
203
0
    return;
204
0
  }
205
206
  /* treat the connection as aborted in CONNECT_ONLY situations, we do
207
   * not know what the APP did with it. */
208
3.15k
  if(conn->bits.connect_only)
209
0
    aborted = TRUE;
210
3.15k
  conn->bits.aborted = aborted;
211
212
  /* We do not shutdown dead connections. The term 'dead' can be misleading
213
   * here, as we also mark errored connections/transfers as 'dead'.
214
   * If we do a shutdown for an aborted transfer, the server might think
215
   * it was successful otherwise (for example an ftps: upload). This is
216
   * not what we want. */
217
3.15k
  if(aborted)
218
0
    done = TRUE;
219
3.15k
  if(!done) {
220
    /* Attempt to shutdown the connection right away. */
221
3.15k
    Curl_conn_shutdown_once(admin, conn, &done);
222
3.15k
  }
223
224
3.15k
  cshutdn = Curl_cshutdn_get(data);
225
3.15k
  if(done || !cshutdn)
226
3.15k
    Curl_conn_terminate(admin, conn, FALSE);
227
0
  else
228
0
    Curl_cshutdn_add(cshutdn, conn, cpool->num_conn);
229
3.15k
}
230
231
void Curl_cpool_destroy(struct cpool *cpool, struct Curl_easy *admin)
232
4.00k
{
233
4.00k
  if(cpool && cpool->initialized && admin) {
234
4.00k
    struct connectdata *conn;
235
4.00k
    struct Curl_sigpipe_ctx pipe_ctx;
236
237
4.00k
    CURL_TRC_M(admin, "%s[CPOOL] destroy, %zu connections",
238
4.00k
               cpool->share ? "[SHARE] " : "", cpool->num_conn);
239
    /* Move all connections to the shutdown list */
240
4.00k
    sigpipe_init(&pipe_ctx);
241
4.00k
    CPOOL_LOCK(cpool, admin);
242
4.00k
    conn = cpool_get_first(cpool);
243
4.00k
    if(conn)
244
0
      sigpipe_apply(admin, &pipe_ctx);
245
4.00k
    while(conn) {
246
0
      cpool_remove_conn(cpool, conn);
247
0
      cpool_discard_conn(cpool, admin, conn, FALSE);
248
0
      conn = cpool_get_first(cpool);
249
0
    }
250
4.00k
    CPOOL_UNLOCK(cpool, admin);
251
4.00k
    sigpipe_restore(&pipe_ctx);
252
4.00k
    Curl_hash_destroy(&cpool->dest2bundle);
253
4.00k
  }
254
4.00k
}
255
256
static struct cpool *cpool_get_instance(struct Curl_easy *data)
257
22.9k
{
258
  /* admin handles do not necessarily find the correct pool */
259
22.9k
  DEBUGASSERT(data->mid);
260
22.9k
  if(CURL_SHARE_KEEP_CONNECT(data->share))
261
0
    return &data->share->cpool;
262
22.9k
  else if(data->multi_easy)
263
0
    return &data->multi_easy->cpool;
264
22.9k
  else if(data->multi)
265
22.9k
    return &data->multi->cpool;
266
0
  return NULL;
267
22.9k
}
268
269
struct cpool *Curl_cpool_get_instance(struct Curl_easy *data)
270
3.15k
{
271
3.15k
  return cpool_get_instance(data);
272
3.15k
}
273
274
void Curl_cpool_xfer_init(struct Curl_easy *data)
275
4.00k
{
276
4.00k
  struct cpool *cpool = cpool_get_instance(data);
277
278
4.00k
  if(cpool) {
279
4.00k
    CPOOL_LOCK(cpool, data);
280
    /* the identifier inside the connection cache */
281
4.00k
    data->id = cpool->next_easy_id++;
282
4.00k
    if(cpool->next_easy_id == CURL_OFF_T_MAX)
283
0
      cpool->next_easy_id = 0;
284
4.00k
    data->state.lastconnect_id = -1;
285
286
4.00k
    CPOOL_UNLOCK(cpool, data);
287
4.00k
  }
288
0
  else {
289
    /* We should not get here, but in a non-debug build, do something */
290
0
    DEBUGASSERT(0);
291
0
    data->id = 0;
292
0
    data->state.lastconnect_id = -1;
293
0
  }
294
4.00k
}
295
296
static struct cpool_bundle *cpool_add_bundle(struct cpool *cpool,
297
                                             struct connectdata *conn)
298
3.15k
{
299
3.15k
  struct cpool_bundle *bundle;
300
301
3.15k
  bundle = cpool_bundle_create(conn->destination);
302
3.15k
  if(!bundle)
303
0
    return NULL;
304
305
3.15k
  if(!Curl_hash_add(&cpool->dest2bundle,
306
3.15k
                    bundle->dest, bundle->dest_len, bundle)) {
307
0
    cpool_bundle_destroy(bundle);
308
0
    return NULL;
309
0
  }
310
3.15k
  return bundle;
311
3.15k
}
312
313
static struct connectdata *cpool_bundle_get_oldest_idle(
314
  struct cpool_bundle *bundle,
315
  const struct curltime *pnow)
316
0
{
317
0
  struct Curl_llist_node *curr;
318
0
  struct connectdata *oldest_idle = NULL;
319
0
  timediff_t unused_ms;
320
0
  timediff_t oldest_ms = -1;
321
0
  struct connectdata *conn;
322
323
0
  curr = Curl_llist_head(&bundle->conns);
324
0
  while(curr) {
325
0
    conn = Curl_node_elem(curr);
326
327
0
    if(!CONN_INUSE(conn)) {
328
      /* Set higher score for the age passed since the connection was used */
329
0
      unused_ms = curlx_ptimediff_ms(pnow, &conn->created) - conn->lastused_ms;
330
0
      if(unused_ms > oldest_ms) {
331
0
        oldest_ms = unused_ms;
332
0
        oldest_idle = conn;
333
0
      }
334
0
    }
335
0
    curr = Curl_node_next(curr);
336
0
  }
337
0
  return oldest_idle;
338
0
}
339
340
static struct connectdata *cpool_get_oldest_idle(struct cpool *cpool,
341
                                                 const struct curltime *pnow,
342
                                                 timediff_t min_age_ms)
343
0
{
344
0
  struct Curl_hash_iterator iter;
345
0
  struct Curl_llist_node *curr;
346
0
  struct Curl_hash_element *he;
347
0
  struct cpool_bundle *bundle;
348
0
  struct connectdata *oldest_idle = NULL;
349
0
  timediff_t oldest_idle_ms = -1;
350
0
  timediff_t idle_ms;
351
352
0
  Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
353
354
0
  for(he = Curl_hash_next_element(&iter); he;
355
0
      he = Curl_hash_next_element(&iter)) {
356
0
    struct connectdata *conn;
357
0
    bundle = he->ptr;
358
359
0
    for(curr = Curl_llist_head(&bundle->conns); curr;
360
0
        curr = Curl_node_next(curr)) {
361
0
      conn = Curl_node_elem(curr);
362
0
      if(CONN_INUSE(conn) || conn->bits.close || conn->bits.connect_only)
363
0
        continue;
364
0
      idle_ms = curlx_ptimediff_ms(pnow, &conn->created) - conn->lastused_ms;
365
0
      if((idle_ms >= min_age_ms) && (idle_ms > oldest_idle_ms)) {
366
0
        oldest_idle_ms = idle_ms;
367
0
        oldest_idle = conn;
368
0
      }
369
0
    }
370
0
  }
371
0
  return oldest_idle;
372
0
}
373
374
static void cpool_conn_close(struct cpool *cpool,
375
                             struct Curl_easy *data,
376
                             struct connectdata *conn,
377
                             bool aborted)
378
3.15k
{
379
3.15k
  struct Curl_easy *admin;
380
3.15k
  bool do_lock;
381
382
3.15k
  DEBUGASSERT(cpool);
383
3.15k
  DEBUGASSERT(data && !data->conn);
384
3.15k
  if(!cpool)
385
0
    return;
386
387
  /* If this connection is not marked to force-close, leave it open if there
388
   * are other users of it */
389
3.15k
  if(CONN_INUSE(conn) && !aborted) {
390
0
    DEBUGASSERT(0); /* does this ever happen? */
391
0
    DEBUGF(infof(data, "conn terminate when inuse: %u", conn->attached_xfers));
392
0
    return;
393
0
  }
394
395
  /* This method may be called while we are under lock, e.g. from a
396
   * user callback in find. */
397
3.15k
  admin = Curl_get_admin(data);
398
3.15k
  do_lock = !CPOOL_IS_LOCKED(cpool);
399
3.15k
  if(do_lock)
400
0
    CPOOL_LOCK(cpool, admin);
401
402
3.15k
  if(conn->bits.in_cpool) {
403
3.15k
    cpool_remove_conn(cpool, conn);
404
3.15k
    DEBUGASSERT(!conn->bits.in_cpool);
405
3.15k
  }
406
407
  /* treat the connection as aborted in CONNECT_ONLY situations,
408
   * so no graceful shutdown is attempted. */
409
3.15k
  if(conn->bits.connect_only)
410
0
    aborted = TRUE;
411
412
3.15k
  if(data->multi) {
413
    /* Add it to the multi's cpool for shutdown handling */
414
3.15k
    infof(data, "%s connection #%" FMT_OFF_T,
415
3.15k
          aborted ? "closing" : "shutting down", conn->connection_id);
416
3.15k
    cpool_discard_conn(&data->multi->cpool, data, conn, aborted);
417
3.15k
  }
418
0
  else {
419
    /* No multi available, terminate */
420
0
    infof(data, "closing connection #%" FMT_OFF_T, conn->connection_id);
421
0
    Curl_conn_terminate(admin, conn, !aborted);
422
0
  }
423
424
3.15k
  if(do_lock)
425
0
    CPOOL_UNLOCK(cpool, admin);
426
3.15k
}
427
428
void Curl_conn_close(struct Curl_easy *data,
429
                     struct connectdata *conn,
430
                     bool aborted)
431
3.15k
{
432
3.15k
  struct cpool *cpool = cpool_get_instance(data);
433
3.15k
  cpool_conn_close(cpool, data, conn, aborted);
434
3.15k
}
435
436
/* Evict an idle connection to make room in the pool. A pool owned by
437
 * a share has no multi that could perform a controlled shutdown of the
438
 * connection; terminate it right away. Otherwise, hand it to the
439
 * transfer's multi for shutdown. Expects the pool to be locked. */
440
static void cpool_evict_conn(struct cpool *cpool,
441
                             struct Curl_easy *admin,
442
                             struct connectdata *conn)
443
0
{
444
0
  if(cpool->share) {
445
0
    cpool_remove_conn(cpool, conn);
446
0
    Curl_conn_terminate(admin, conn, TRUE);
447
0
  }
448
0
  else
449
0
    cpool_conn_close(cpool, admin, conn, FALSE);
450
0
}
451
452
int Curl_cpool_check_limits(struct Curl_easy *data,
453
                            struct connectdata *conn,
454
                            const struct curltime *pnow)
455
3.15k
{
456
3.15k
  struct cpool *cpool = cpool_get_instance(data);
457
3.15k
  struct cshutdn *cshutdn = Curl_cshutdn_get(data);
458
3.15k
  struct Curl_easy *admin;
459
3.15k
  struct cpool_bundle *bundle;
460
3.15k
  size_t dest_limit = 0;
461
3.15k
  size_t total_limit = 0;
462
3.15k
  size_t shutdowns;
463
3.15k
  int res = CPOOL_LIMIT_OK;
464
465
3.15k
  if(!cpool)
466
0
    return CPOOL_LIMIT_OK;
467
468
  /* multi determines the limits, no matter who owns the pool */
469
3.15k
  if(data->multi) {
470
3.15k
    dest_limit = data->multi->max_host_connections;
471
3.15k
    total_limit = data->multi->max_total_connections;
472
3.15k
  }
473
474
3.15k
  if(!dest_limit && !total_limit)
475
3.15k
    return CPOOL_LIMIT_OK;
476
477
0
  admin = Curl_get_admin(data);
478
0
  CPOOL_LOCK(cpool, admin);
479
0
  if(dest_limit) {
480
0
    size_t live;
481
482
0
    bundle = cpool_find_bundle(cpool, conn);
483
0
    live = bundle ? Curl_llist_count(&bundle->conns) : 0;
484
0
    shutdowns = Curl_cshutdn_dest_count(cshutdn, conn->destination);
485
0
    while((live + shutdowns) >= dest_limit) {
486
0
      if(shutdowns) {
487
        /* close one connection in shutdown right away, if we can */
488
0
        if(!Curl_cshutdn_close_oldest(cshutdn, conn->destination))
489
0
          break;
490
0
      }
491
0
      else if(!bundle)
492
0
        break;
493
0
      else {
494
0
        struct connectdata *oldest_idle = NULL;
495
        /* The bundle is full. Extract the oldest connection that may
496
         * be removed now, if there is one. */
497
0
        oldest_idle = cpool_bundle_get_oldest_idle(bundle, pnow);
498
0
        if(!oldest_idle)
499
0
          break;
500
        /* disconnect the old conn and continue */
501
0
        CURL_TRC_M(admin, "Discarding connection #%" FMT_OFF_T
502
0
                   " from %zu to reach destination limit of %zu",
503
0
                   oldest_idle->connection_id,
504
0
                   Curl_llist_count(&bundle->conns), dest_limit);
505
0
        cpool_evict_conn(cpool, admin, oldest_idle);
506
507
        /* in case the bundle was destroyed in disconnect, look it up again */
508
0
        bundle = cpool_find_bundle(cpool, conn);
509
0
        live = bundle ? Curl_llist_count(&bundle->conns) : 0;
510
0
      }
511
0
      shutdowns = Curl_cshutdn_dest_count(cshutdn, conn->destination);
512
0
    }
513
0
    if((live + shutdowns) >= dest_limit) {
514
0
      res = CPOOL_LIMIT_DEST;
515
0
      goto out;
516
0
    }
517
0
  }
518
519
0
  if(total_limit) {
520
0
    shutdowns = Curl_cshutdn_count(cshutdn);
521
0
    while((cpool->num_conn + shutdowns) >= total_limit) {
522
0
      if(shutdowns) {
523
        /* close one connection in shutdown right away, if we can */
524
0
        if(!Curl_cshutdn_close_oldest(cshutdn, NULL))
525
0
          break;
526
0
      }
527
0
      else {
528
0
        struct connectdata *oldest_idle =
529
0
          cpool_get_oldest_idle(cpool, pnow, 0);
530
0
        if(!oldest_idle)
531
0
          break;
532
        /* disconnect the old conn and continue */
533
0
        CURL_TRC_M(admin, "Discarding connection #%"
534
0
                   FMT_OFF_T " from %zu to reach total "
535
0
                   "limit of %zu",
536
0
                   oldest_idle->connection_id, cpool->num_conn, total_limit);
537
0
        cpool_evict_conn(cpool, admin, oldest_idle);
538
0
      }
539
0
      shutdowns = Curl_cshutdn_count(cshutdn);
540
0
    }
541
0
    if((cpool->num_conn + shutdowns) >= total_limit) {
542
0
      res = CPOOL_LIMIT_TOTAL;
543
0
      goto out;
544
0
    }
545
0
  }
546
547
0
out:
548
0
  CPOOL_UNLOCK(cpool, admin);
549
0
  return res;
550
0
}
551
552
CURLcode Curl_cpool_add(struct Curl_easy *data,
553
                        struct connectdata *conn)
554
3.15k
{
555
3.15k
  CURLcode result = CURLE_OK;
556
3.15k
  struct cpool_bundle *bundle = NULL;
557
3.15k
  struct cpool *cpool = cpool_get_instance(data);
558
3.15k
  DEBUGASSERT(conn);
559
560
3.15k
  DEBUGASSERT(cpool);
561
3.15k
  if(!cpool)
562
0
    return CURLE_FAILED_INIT;
563
564
3.15k
  CPOOL_LOCK(cpool, data);
565
3.15k
  bundle = cpool_find_bundle(cpool, conn);
566
3.15k
  if(!bundle) {
567
3.15k
    bundle = cpool_add_bundle(cpool, conn);
568
3.15k
    if(!bundle) {
569
0
      result = CURLE_OUT_OF_MEMORY;
570
0
      goto out;
571
0
    }
572
3.15k
  }
573
574
3.15k
  cpool_bundle_add(bundle, conn);
575
3.15k
  conn->connection_id = cpool->next_connection_id++;
576
3.15k
  cpool->num_conn++;
577
3.15k
  CURL_TRC_M(data, "[CPOOL] added connection %" FMT_OFF_T ". "
578
3.15k
             "The cache now contains %zu members",
579
3.15k
             conn->connection_id, cpool->num_conn);
580
3.15k
out:
581
3.15k
  CPOOL_UNLOCK(cpool, data);
582
583
3.15k
  return result;
584
3.15k
}
585
586
/* This function iterates the entire connection pool and calls the function
587
   func() with the connection pointer as the first argument and the supplied
588
   'param' argument as the other.
589
590
   The cpool lock is still held when the callback is called. It needs it,
591
   so that it can safely continue traversing the lists once the callback
592
   returns.
593
594
   Returns TRUE if the loop was aborted due to the callback's return code.
595
596
   Return 0 from func() to continue the loop, return 1 to abort it.
597
 */
598
static bool cpool_foreach(struct Curl_easy *data,
599
                          struct cpool *cpool,
600
                          void *param,
601
                          int (*func)(struct cpool *cpool,
602
                                      struct Curl_easy *data,
603
                                      struct connectdata *conn, void *param))
604
3.15k
{
605
3.15k
  struct Curl_hash_iterator iter;
606
3.15k
  struct Curl_hash_element *he;
607
608
3.15k
  if(!cpool)
609
0
    return FALSE;
610
611
3.15k
  Curl_hash_start_iterate(&cpool->dest2bundle, &iter);
612
613
3.15k
  he = Curl_hash_next_element(&iter);
614
3.15k
  while(he) {
615
0
    struct Curl_llist_node *curr;
616
0
    struct cpool_bundle *bundle = he->ptr;
617
0
    he = Curl_hash_next_element(&iter);
618
619
0
    curr = Curl_llist_head(&bundle->conns);
620
0
    while(curr) {
621
      /* Yes, we need to update curr before calling func(), because func()
622
         might decide to remove the connection */
623
0
      struct connectdata *conn = Curl_node_elem(curr);
624
0
      curr = Curl_node_next(curr);
625
626
0
      if(func(cpool, data, conn, param) == 1) {
627
0
        return TRUE;
628
0
      }
629
0
    }
630
0
  }
631
3.15k
  return FALSE;
632
3.15k
}
633
634
/*
635
 * A connection (already in the pool) has become idle. Do any
636
 * cleanups in regard to the pool's limits.
637
 *
638
 * Return TRUE if idle connection kept in pool, FALSE if closed.
639
 */
640
bool Curl_cpool_conn_now_idle(struct Curl_easy *data,
641
                              struct connectdata *conn)
642
0
{
643
0
  struct cpool *cpool = cpool_get_instance(data);
644
0
  struct connectdata *oldest_idle = NULL;
645
0
  const struct curltime *pnow = NULL;
646
0
  struct Curl_easy *admin;
647
0
  unsigned int maxconnects;
648
0
  bool kept = TRUE;
649
0
  timediff_t min_age_ms = 0;
650
651
0
  if(!data || !data->multi)
652
0
    return kept;
653
654
0
  if(!data->multi->maxconnects) {
655
    /* Attached transfers is a weak indicator of business. */
656
0
    uint32_t attached = Curl_multi_xfers_attached(data->multi);
657
0
    maxconnects = (attached <= UINT_MAX / 2) ? attached * 2 : UINT_MAX;
658
    /* We are guessing. So, only evict a "seemingly superfluous" connection
659
     * when has not been used for this long, */
660
0
    min_age_ms = 1000;
661
0
  }
662
0
  else {
663
0
    maxconnects = data->multi->maxconnects;
664
0
  }
665
666
  /* remember times, connection had been used just before */
667
0
  pnow = Curl_pgrs_now(data);
668
0
  conn->lastchecked_ms = conn->lastupkeep_ms = conn->lastused_ms =
669
0
    curlx_ptimediff_ms(pnow, &conn->created);
670
0
  if(cpool && maxconnects) {
671
    /* may be called form a callback already under lock */
672
0
    bool do_lock = !CPOOL_IS_LOCKED(cpool);
673
674
0
    admin = Curl_get_admin(data);
675
0
    if(do_lock)
676
0
      CPOOL_LOCK(cpool, admin);
677
0
    if(cpool->num_conn > maxconnects) {
678
0
      infof(data, "Connection pool is full, closing the oldest of %zu/%u",
679
0
            cpool->num_conn, maxconnects);
680
681
0
      oldest_idle = cpool_get_oldest_idle(cpool, pnow, min_age_ms);
682
0
      kept = (oldest_idle != conn);
683
0
      if(oldest_idle) {
684
0
        cpool_evict_conn(cpool, admin, oldest_idle);
685
0
      }
686
0
    }
687
0
    if(do_lock)
688
0
      CPOOL_UNLOCK(cpool, admin);
689
0
  }
690
691
0
  return kept;
692
0
}
693
694
bool Curl_cpool_find(struct Curl_easy *data,
695
                     const char *destination,
696
                     Curl_cpool_conn_match_cb *conn_cb,
697
                     Curl_cpool_done_match_cb *done_cb,
698
                     void *userdata)
699
3.15k
{
700
3.15k
  struct cpool *cpool = cpool_get_instance(data);
701
3.15k
  struct cpool_bundle *bundle;
702
3.15k
  bool found = FALSE;
703
704
3.15k
  DEBUGASSERT(cpool);
705
3.15k
  DEBUGASSERT(conn_cb);
706
3.15k
  if(!cpool)
707
0
    return FALSE;
708
709
3.15k
  CPOOL_LOCK(cpool, data);
710
3.15k
  bundle = Curl_hash_pick(&cpool->dest2bundle,
711
3.15k
                          CURL_UNCONST(destination),
712
3.15k
                          strlen(destination) + 1);
713
3.15k
  if(bundle) {
714
0
    struct Curl_llist_node *curr = Curl_llist_head(&bundle->conns);
715
0
    while(curr) {
716
0
      struct connectdata *conn = Curl_node_elem(curr);
717
      /* Get next node now. callback might discard current */
718
0
      curr = Curl_node_next(curr);
719
720
0
      if(conn_cb(conn, userdata)) {
721
0
        found = TRUE;
722
0
        break;
723
0
      }
724
0
    }
725
0
  }
726
727
3.15k
  if(done_cb) {
728
3.15k
    found = done_cb(userdata);
729
3.15k
  }
730
3.15k
  CPOOL_UNLOCK(cpool, data);
731
3.15k
  return found;
732
3.15k
}
733
734
struct cpool_reaper_ctx {
735
  size_t reaped;
736
  struct curltime now;
737
};
738
739
static int cpool_reap_dead_cb(struct cpool *cpool,
740
                              struct Curl_easy *admin,
741
                              struct connectdata *conn, void *param)
742
0
{
743
0
  struct cpool_reaper_ctx *reaper = param;
744
745
0
  if(!CONN_INUSE(conn)) {
746
0
    if(conn->bits.no_reuse || conn->bits.close ||
747
0
       !Curl_cpool_conn_seems_healthy(conn, admin, &reaper->now)) {
748
      /* terminate conn and stop the iteration */
749
0
      reaper->reaped++;
750
0
      cpool_conn_close(cpool, admin, conn, FALSE);
751
0
      return 1;
752
0
    }
753
0
  }
754
0
  return 0; /* continue iteration */
755
0
}
756
757
/*
758
 * This function scans the data's connection pool for half-open/dead
759
 * connections, closes and removes them.
760
 * The cleanup is done at most once per second.
761
 *
762
 * When called, this transfer has no connection attached.
763
 */
764
void Curl_cpool_prune_dead(struct cpool *cpool,
765
                           struct Curl_easy *data)
766
3.15k
{
767
3.15k
  struct Curl_easy *admin;
768
3.15k
  timediff_t elapsed;
769
770
3.15k
  if(!cpool)
771
0
    return;
772
773
3.15k
  admin = Curl_get_admin(data);
774
3.15k
  CPOOL_LOCK(cpool, admin);
775
3.15k
  elapsed = curlx_ptimediff_ms(Curl_pgrs_now(admin), &cpool->last_cleanup);
776
777
3.15k
  if(elapsed >= 1000L) {
778
3.15k
    struct cpool_reaper_ctx reaper;
779
780
3.15k
    memset(&reaper, 0, sizeof(reaper));
781
3.15k
    reaper.now = *Curl_pgrs_now(admin);
782
3.15k
    while(cpool_foreach(admin, cpool, &reaper, cpool_reap_dead_cb))
783
0
      ;
784
3.15k
    cpool->last_cleanup = *Curl_pgrs_now(admin);
785
3.15k
  }
786
3.15k
  CPOOL_UNLOCK(cpool, admin);
787
3.15k
}
788
789
static int conn_upkeep(struct cpool *cpool,
790
                       struct Curl_easy *admin,
791
                       struct connectdata *conn,
792
                       void *param)
793
0
{
794
0
  const struct curltime *pnow = Curl_pgrs_now(admin);
795
796
0
  (void)param;
797
0
  if((curlx_ptimediff_ms(pnow, &conn->created) - conn->lastupkeep_ms) >=
798
0
     admin->set.upkeep_interval_ms) {
799
0
    CURLcode result;
800
801
0
    conn->lastupkeep_ms = curlx_ptimediff_ms(pnow, &conn->created);
802
    /* briefly attach for action */
803
0
    Curl_attach_connection(admin, conn, FALSE);
804
0
    result = Curl_conn_keep_alive(admin, conn);
805
0
    Curl_detach_connection(admin);
806
807
0
    if(result && !CONN_INUSE(conn)) {
808
0
      cpool_conn_close(cpool, admin, conn, FALSE);
809
0
      return 1;
810
0
    }
811
0
  }
812
0
  return 0; /* continue iteration */
813
0
}
814
815
CURLcode Curl_cpool_upkeep(struct Curl_easy *data)
816
0
{
817
0
  struct cpool *cpool = cpool_get_instance(data);
818
0
  struct Curl_easy *admin = Curl_get_admin(data);
819
820
0
  if(!cpool)
821
0
    return CURLE_OK;
822
823
0
  CPOOL_LOCK(cpool, admin);
824
0
  while(cpool_foreach(admin, cpool, NULL, conn_upkeep))
825
0
    ;
826
0
  CPOOL_UNLOCK(cpool, admin);
827
0
  return CURLE_OK;
828
0
}
829
830
struct cpool_find_ctx {
831
  curl_off_t id;
832
  struct connectdata *conn;
833
};
834
835
static int cpool_find_conn(struct cpool *cpool,
836
                           struct Curl_easy *data,
837
                           struct connectdata *conn, void *param)
838
0
{
839
0
  struct cpool_find_ctx *fctx = param;
840
0
  (void)cpool;
841
0
  (void)data;
842
0
  if(conn->connection_id == fctx->id) {
843
0
    fctx->conn = conn;
844
0
    return 1;
845
0
  }
846
0
  return 0;
847
0
}
848
849
struct connectdata *Curl_cpool_get_conn(struct Curl_easy *data,
850
                                        curl_off_t conn_id)
851
0
{
852
0
  struct cpool *cpool = cpool_get_instance(data);
853
0
  struct cpool_find_ctx fctx;
854
855
0
  if(!cpool)
856
0
    return NULL;
857
0
  fctx.id = conn_id;
858
0
  fctx.conn = NULL;
859
0
  CPOOL_LOCK(cpool, data);
860
0
  cpool_foreach(data, cpool, &fctx, cpool_find_conn);
861
0
  CPOOL_UNLOCK(cpool, data);
862
0
  return fctx.conn;
863
0
}
864
865
void Curl_cpool_do_locked(struct Curl_easy *data,
866
                          struct connectdata *conn,
867
                          Curl_cpool_conn_do_cb *cb, void *cbdata)
868
3.15k
{
869
3.15k
  struct cpool *cpool = cpool_get_instance(data);
870
3.15k
  if(cpool) {
871
3.15k
    CPOOL_LOCK(cpool, data);
872
3.15k
    cb(conn, data, cbdata);
873
3.15k
    CPOOL_UNLOCK(cpool, data);
874
3.15k
  }
875
0
  else
876
0
    cb(conn, data, cbdata);
877
3.15k
}
878
879
static int cpool_mark_stale(struct cpool *cpool,
880
                            struct Curl_easy *admin,
881
                            struct connectdata *conn, void *param)
882
0
{
883
0
  (void)cpool;
884
0
  (void)admin;
885
0
  (void)param;
886
0
  conn->bits.no_reuse = TRUE;
887
0
  return 0;
888
0
}
889
890
static int cpool_reap_no_reuse(struct cpool *cpool,
891
                               struct Curl_easy *admin,
892
                               struct connectdata *conn, void *param)
893
0
{
894
0
  (void)param;
895
0
  if(!CONN_INUSE(conn) && conn->bits.no_reuse) {
896
0
    cpool_conn_close(cpool, admin, conn, FALSE);
897
0
    return 1;
898
0
  }
899
0
  return 0; /* continue iteration */
900
0
}
901
902
void Curl_cpool_nw_changed(struct cpool *cpool, struct Curl_easy *admin)
903
0
{
904
0
  if(cpool && admin) {
905
0
    CPOOL_LOCK(cpool, admin);
906
0
    cpool_foreach(admin, cpool, NULL, cpool_mark_stale);
907
0
    while(cpool_foreach(admin, cpool, NULL, cpool_reap_no_reuse))
908
0
      ;
909
0
    CPOOL_UNLOCK(cpool, admin);
910
0
  }
911
0
}
912
913
/* A connection has to have been idle for less than 'conn_max_idle_ms'
914
   (the success rate is too low after this), or created less than
915
   'conn_max_age_ms' ago, to be subject for reuse. */
916
static bool cpool_conn_maxage(struct Curl_easy *data,
917
                              struct connectdata *conn,
918
                              const struct curltime *pnow)
919
0
{
920
0
  timediff_t age_ms;
921
922
0
  if(data->set.conn_max_idle_ms) {
923
0
    age_ms = curlx_ptimediff_ms(pnow, &conn->created) - conn->lastused_ms;
924
0
    if(age_ms > data->set.conn_max_idle_ms) {
925
0
      infof(data, "Too old connection (%" FMT_TIMEDIFF_T
926
0
            " ms idle, max idle is %" FMT_TIMEDIFF_T " ms), disconnect it",
927
0
            age_ms, data->set.conn_max_idle_ms);
928
0
      return TRUE;
929
0
    }
930
0
  }
931
932
0
  if(data->set.conn_max_age_ms) {
933
0
    age_ms = curlx_ptimediff_ms(pnow, &conn->created);
934
0
    if(age_ms > data->set.conn_max_age_ms) {
935
0
      infof(data,
936
0
            "Too old connection (created %" FMT_TIMEDIFF_T
937
0
            " ms ago, max lifetime is %" FMT_TIMEDIFF_T " ms), disconnect it",
938
0
            age_ms, data->set.conn_max_age_ms);
939
0
      return TRUE;
940
0
    }
941
0
  }
942
943
0
  return FALSE;
944
0
}
945
946
bool Curl_cpool_conn_seems_healthy(struct connectdata *conn,
947
                                   struct Curl_easy *data,
948
                                   const struct curltime *pnow)
949
0
{
950
0
  struct Curl_easy *admin;
951
0
  bool healthy = TRUE;
952
953
0
  DEBUGASSERT(!data->conn);
954
0
  if(!CONN_INUSE(conn) && cpool_conn_maxage(data, conn, pnow)) /* too old? */
955
0
    return FALSE;
956
0
  if((curlx_ptimediff_ms(pnow, &conn->created) - conn->lastchecked_ms) < 1000)
957
0
    return TRUE;
958
959
0
  admin = Curl_get_admin(data);
960
0
  if(conn->scheme->run->connection_is_dead) {
961
0
    Curl_attach_connection(admin, conn, FALSE);
962
0
    healthy = !conn->scheme->run->connection_is_dead(admin, conn);
963
0
    Curl_detach_connection(admin);
964
0
  }
965
0
  else {
966
0
    bool input_pending = FALSE;
967
968
0
    Curl_attach_connection(admin, conn, FALSE);
969
0
    healthy = Curl_conn_is_alive(admin, conn, &input_pending);
970
0
    Curl_detach_connection(admin);
971
0
    if(healthy && input_pending &&
972
0
       !CONN_INUSE(conn) && !Curl_conn_is_multiplex(conn, FIRSTSOCKET)) {
973
      /* Non-multiplexed connections without attached transfers should
974
       * not have input pending. The input might be a TLS Notify Close,
975
       * for all we know. */
976
0
      DEBUGF(infof(data, "connection has no transfer but input, not healthy"));
977
0
      healthy = FALSE;
978
0
    }
979
0
  }
980
981
0
  if(healthy)
982
0
    conn->lastchecked_ms = curlx_ptimediff_ms(pnow, &conn->created);
983
0
  return healthy;
984
0
}
985
986
#if 0
987
/* Useful for debugging the connection pool */
988
void Curl_cpool_print(struct cpool *cpool)
989
{
990
  struct Curl_hash_iterator iter;
991
  struct Curl_llist_node *curr;
992
  struct Curl_hash_element *he;
993
994
  if(!cpool)
995
    return;
996
997
  curl_mfprintf(stderr, "=Bundle cache=\n");
998
999
  Curl_hash_start_iterate(cpool->dest2bundle, &iter);
1000
1001
  he = Curl_hash_next_element(&iter);
1002
  while(he) {
1003
    struct cpool_bundle *bundle;
1004
    struct connectdata *conn;
1005
1006
    bundle = he->ptr;
1007
1008
    curl_mfprintf(stderr, "%s -", he->key);
1009
    curr = Curl_llist_head(bundle->conns);
1010
    while(curr) {
1011
      conn = Curl_node_elem(curr);
1012
1013
      curl_mfprintf(stderr, " [%p %d]", (void *)conn, conn->refcount);
1014
      curr = Curl_node_next(curr);
1015
    }
1016
    curl_mfprintf(stderr, "\n");
1017
1018
    he = Curl_hash_next_element(&iter);
1019
  }
1020
}
1021
#endif