Coverage Report

Created: 2026-12-01 06:27

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
#include "uint-spbset.h"
38
39
40
7.73k
#define CPOOL_IS_LOCKED(c)    ((c) && (c)->locked)
41
42
#define CPOOL_LOCK(c, d)                                                \
43
49.7k
  do {                                                                  \
44
49.7k
    if(c) {                                                             \
45
49.7k
      if(CURL_SHARE_KEEP_CONNECT((c)->share))                           \
46
49.7k
        Curl_share_lock_share((c)->share, (d), CURL_LOCK_DATA_CONNECT,  \
47
0
                        CURL_LOCK_ACCESS_SINGLE);                       \
48
49.7k
      DEBUGASSERT(!(c)->locked);                                        \
49
49.7k
      (c)->locked = TRUE;                                               \
50
49.7k
    }                                                                   \
51
49.7k
  } while(0)
52
53
#define CPOOL_UNLOCK(c, d)                                              \
54
49.7k
  do {                                                                  \
55
49.7k
    if(c) {                                                             \
56
49.7k
      DEBUGASSERT((c)->locked);                                         \
57
49.7k
      (c)->locked = FALSE;                                              \
58
49.7k
      if(CURL_SHARE_KEEP_CONNECT((c)->share))                           \
59
49.7k
        Curl_share_unlock_share((c)->share, (d), CURL_LOCK_DATA_CONNECT); \
60
49.7k
    }                                                                   \
61
49.7k
  } while(0)
62
63
/* A set of connection pool ids to the same destination. */
64
struct cpool_bundle {
65
  struct uint32_spbset ids; /* bitset of connection `pool_id`s  */
66
  uint32_t refcount;
67
};
68
69
static void cpool_bundle_unlink(struct cpool_bundle **pbundle)
70
61.6k
{
71
61.6k
  if(*pbundle) {
72
31.3k
    struct cpool_bundle *bundle = *pbundle;
73
74
31.3k
    DEBUGASSERT(bundle->refcount);
75
31.3k
    *pbundle = NULL;
76
31.3k
    if(bundle->refcount)
77
31.3k
      bundle->refcount--;
78
31.3k
    if(!bundle->refcount) {
79
9.73k
      Curl_uint32_spbset_destroy(&bundle->ids);
80
9.73k
      curlx_free(bundle);
81
9.73k
    }
82
31.3k
  }
83
61.6k
}
84
85
static void cpool_bundle_link(struct cpool_bundle **pdest,
86
                              struct cpool_bundle *src)
87
39.9k
{
88
39.9k
  if(*pdest != src) {
89
21.6k
    cpool_bundle_unlink(pdest);
90
21.6k
    *pdest = src;
91
21.6k
    if(src) {
92
21.6k
      DEBUGASSERT(src->refcount < UINT32_MAX);
93
21.6k
      src->refcount++;
94
21.6k
    }
95
21.6k
  }
96
39.9k
}
97
98
static struct cpool_bundle *cpool_create_bundle(void)
99
9.73k
{
100
  /* created with refcount == 1 */
101
9.73k
  struct cpool_bundle *bundle = curlx_calloc(1, sizeof(*bundle));
102
9.73k
  if(!bundle)
103
3
    return NULL;
104
105
9.73k
  Curl_uint32_spbset_init(&bundle->ids);
106
9.73k
  bundle->refcount = 1;
107
9.73k
  return bundle;
108
9.73k
}
109
110
/* Add a connection to a bundle */
111
static bool cpool_bundle_add_id(struct cpool *cpool,
112
                                struct cpool_bundle *bundle,
113
                                const char *destination,
114
                                uint32_t pool_id)
115
10.0k
{
116
10.0k
  if(!Curl_uint32_spbset_add(&bundle->ids, pool_id))
117
0
    return FALSE;
118
10.0k
  if(Curl_uint32_spbset_count(&bundle->ids) == 1) {
119
    /* first one added, register bundle */
120
9.73k
    struct cpool_bundle *new_ref = NULL;
121
9.73k
    cpool_bundle_link(&new_ref, bundle);
122
9.73k
    if(!Curl_hash_add(&cpool->dest2bundle,
123
9.73k
                      destination, strlen(destination), new_ref)) {
124
3
      cpool_bundle_unlink(&new_ref);
125
3
      return FALSE;
126
3
    }
127
9.73k
  }
128
10.0k
  return TRUE;
129
10.0k
}
130
131
/* Remove a connection from a bundle */
132
static void cpool_bundle_remove_id(struct cpool *cpool,
133
                                   struct cpool_bundle *bundle,
134
                                   const char *destination,
135
                                   uint32_t pool_id)
136
10.0k
{
137
10.0k
  Curl_uint32_spbset_remove(&bundle->ids, pool_id);
138
10.0k
  if(!Curl_uint32_spbset_count(&bundle->ids)) {
139
    /* Now empty, unregister bundle */
140
9.73k
    Curl_hash_delete(&cpool->dest2bundle, destination, strlen(destination));
141
9.73k
  }
142
10.0k
}
143
144
static void cpool_bundle_hash_dtor(void *entry)
145
9.73k
{
146
9.73k
  struct cpool_bundle *bundle = entry;
147
9.73k
  cpool_bundle_unlink(&bundle);
148
9.73k
}
149
150
static struct cpool_bundle *cpool_get_bundle(struct cpool *cpool,
151
                                             const char *destination)
152
30.2k
{
153
30.2k
  struct cpool_bundle *bundle = NULL;
154
30.2k
  cpool_bundle_link(&bundle, Curl_hash_pick(
155
30.2k
    &cpool->dest2bundle, destination, strlen(destination)));
156
30.2k
  return bundle;
157
30.2k
}
158
159
static void cpool_remove_conn(struct cpool *cpool,
160
                              struct connectdata *conn)
161
10.0k
{
162
10.0k
  uint32_t cpid = conn->cpid;
163
164
10.0k
  DEBUGASSERT(cpool);
165
10.0k
  if(cpid != UINT32_MAX) {
166
10.0k
    struct cpool_bundle *bundle;
167
168
10.0k
#ifdef DEBUGBUILD
169
10.0k
    {
170
10.0k
      struct connectdata *pooled_conn =
171
10.0k
        Curl_uint32_tbl_get(&cpool->conns, cpid);
172
10.0k
      DEBUGASSERT(conn == pooled_conn);
173
10.0k
    }
174
10.0k
#endif
175
10.0k
    Curl_uint32_tbl_remove(&cpool->conns, cpid);
176
10.0k
    Curl_uint32_bset_remove(&cpool->idles, cpid);
177
10.0k
    conn->cpid = UINT32_MAX;
178
179
10.0k
    bundle = cpool_get_bundle(cpool, conn->destination);
180
10.0k
    DEBUGASSERT(bundle);
181
10.0k
    if(bundle)
182
10.0k
      cpool_bundle_remove_id(cpool, bundle, conn->destination, cpid);
183
10.0k
    cpool_bundle_unlink(&bundle);
184
10.0k
  }
185
10.0k
}
186
187
static void cpool_discard_conn(struct cpool *cpool,
188
                               struct Curl_easy *data,
189
                               struct connectdata *conn,
190
                               bool aborted)
191
10.0k
{
192
10.0k
  struct Curl_easy *admin;
193
10.0k
  bool done = FALSE;
194
195
10.0k
  DEBUGASSERT(data);
196
10.0k
  DEBUGASSERT(!data->conn);
197
10.0k
  DEBUGASSERT(cpool);
198
10.0k
  DEBUGASSERT(conn->cpid == UINT32_MAX);
199
200
10.0k
  admin = Curl_get_admin(data);
201
  /* We MUST NOT discard a connection that is still in use and would
202
   * trigger Use-After-Free. */
203
10.0k
  if(CONN_INUSE(conn)) {
204
0
    CURL_TRC_M(admin, "[CPOOL] not discarding #%" FMT_OFF_T
205
0
               " still in use by %u transfers", conn->connection_id,
206
0
               conn->attached_xfers);
207
0
    DEBUGASSERT(0);
208
0
    return;
209
0
  }
210
211
  /* treat the connection as aborted in CONNECT_ONLY situations, we do
212
   * not know what the APP did with it. */
213
10.0k
  if(conn->bits.connect_only)
214
0
    aborted = TRUE;
215
10.0k
  conn->bits.aborted = aborted;
216
217
  /* We do not shutdown dead connections. The term 'dead' can be misleading
218
   * here, as we also mark errored connections/transfers as 'dead'.
219
   * If we do a shutdown for an aborted transfer, the server might think
220
   * it was successful otherwise (for example an ftps: upload). This is
221
   * not what we want. */
222
10.0k
  if(aborted)
223
6.37k
    done = TRUE;
224
10.0k
  if(!done) {
225
    /* Attempt to shutdown the connection right away. */
226
3.70k
    Curl_cshutdn_try_once(admin, conn, &done);
227
3.70k
  }
228
229
10.0k
  if(done || !data->multi)
230
10.0k
    Curl_cshutdn_terminate(admin, conn, FALSE);
231
0
  else {
232
0
    struct Curl_multi *multi = data->multi;
233
0
    size_t max_shutdowns = multi->max_total_connections;
234
0
    uint32_t num_conns = Curl_uint32_tbl_count(&cpool->conns);
235
236
0
    if(num_conns < max_shutdowns)
237
0
      max_shutdowns -= num_conns;
238
0
    else if(max_shutdowns)
239
0
      max_shutdowns = 1;
240
0
    else /* no connection limit set, let's restrict growth nevertheless */
241
0
      max_shutdowns = CURLMAX(num_conns / 4, 128);
242
0
    Curl_cshutdn_add(&multi->cshutdn, multi, conn, max_shutdowns);
243
0
  }
244
10.0k
}
245
246
void Curl_cpool_init(struct cpool *cpool,
247
                     struct Curl_share *share,
248
                     size_t size)
249
8.97k
{
250
8.97k
  Curl_uint32_tbl_init(&cpool->conns);
251
8.97k
  Curl_uint32_bset_init(&cpool->idles);
252
8.97k
  Curl_hash_init(&cpool->dest2bundle, size, CURL_HASH_TYPE_BYTES,
253
8.97k
                 cpool_bundle_hash_dtor);
254
8.97k
  cpool->share = share;
255
8.97k
  cpool->initialized = TRUE;
256
8.97k
  cpool->in_shutdown = FALSE;
257
8.97k
  cpool->locked = FALSE;
258
8.97k
}
259
260
void Curl_cpool_destroy(struct cpool *cpool, struct Curl_easy *admin)
261
8.99k
{
262
8.99k
  if(cpool && cpool->initialized && admin) {
263
8.97k
    void *entry;
264
8.97k
    uint32_t cpid;
265
266
8.97k
    CURL_TRC_M(admin, "%s[CPOOL] destroy, %u connections",
267
8.97k
               cpool->share ? "[SHARE] " : "",
268
8.97k
               Curl_uint32_tbl_count(&cpool->conns));
269
    /* Move all connections to the shutdown list */
270
8.97k
    CPOOL_LOCK(cpool, admin);
271
8.97k
    if(Curl_uint32_tbl_first(&cpool->conns, &cpid, &entry)) {
272
2.22k
      struct Curl_sigpipe_ctx pipe_ctx;
273
274
2.22k
      sigpipe_init(&pipe_ctx);
275
2.22k
      sigpipe_apply(admin, &pipe_ctx);
276
2.34k
      do {
277
2.34k
        struct connectdata *conn = entry;
278
2.34k
        cpool_remove_conn(cpool, conn);
279
2.34k
        cpool_discard_conn(cpool, admin, conn, FALSE);
280
2.34k
      } while(Curl_uint32_tbl_next(&cpool->conns, cpid, &cpid, &entry));
281
2.22k
      sigpipe_restore(&pipe_ctx);
282
2.22k
    }
283
8.97k
    CPOOL_UNLOCK(cpool, admin);
284
8.97k
    Curl_hash_destroy(&cpool->dest2bundle);
285
8.97k
    Curl_uint32_bset_destroy(&cpool->idles);
286
8.97k
    Curl_uint32_tbl_destroy(&cpool->conns);
287
8.97k
  }
288
8.99k
}
289
290
static struct cpool *cpool_get_instance(struct Curl_easy *data)
291
66.5k
{
292
  /* admin handles do not necessarily find the correct pool */
293
66.5k
  DEBUGASSERT(data->mid);
294
66.5k
  if(CURL_SHARE_KEEP_CONNECT(data->share))
295
0
    return &data->share->cpool;
296
66.5k
  else if(data->multi_easy)
297
0
    return &data->multi_easy->cpool;
298
66.5k
  else if(data->multi)
299
57.5k
    return &data->multi->cpool;
300
9.04k
  return NULL;
301
66.5k
}
302
303
void Curl_cpool_xfer_init(struct Curl_easy *data)
304
10.5k
{
305
10.5k
  struct cpool *cpool = cpool_get_instance(data);
306
307
10.5k
  if(cpool) {
308
10.5k
    CPOOL_LOCK(cpool, data);
309
    /* the identifier inside the connection cache */
310
10.5k
    data->id = cpool->next_easy_id++;
311
10.5k
    if(cpool->next_easy_id == CURL_OFF_T_MAX)
312
0
      cpool->next_easy_id = 0;
313
10.5k
    data->state.last_conn_id = -1;
314
10.5k
    data->state.last_cpid = UINT32_MAX;
315
316
10.5k
    CPOOL_UNLOCK(cpool, data);
317
10.5k
  }
318
0
  else {
319
    /* We should not get here, but in a non-debug build, do something */
320
0
    DEBUGASSERT(0);
321
0
    data->id = 0;
322
0
    data->state.last_conn_id = -1;
323
0
    data->state.last_cpid = UINT32_MAX;
324
0
  }
325
10.5k
}
326
327
static bool cpool_conn_can_be_closed(struct connectdata *conn)
328
1.10k
{
329
  /* CONNECT_ONLY sockets remain in use by the application. */
330
1.10k
  return (conn && !CONN_INUSE(conn) && !conn->bits.connect_only);
331
1.10k
}
332
333
static struct connectdata *cpool_bundle_get_oldest_idle(
334
  struct cpool *cpool,
335
  struct cpool_bundle *bundle,
336
  const struct curltime *pnow)
337
0
{
338
0
  uint32_t cpid, oldest_id = UINT32_MAX;
339
0
  timediff_t unused_ms;
340
0
  timediff_t oldest_ms = -1;
341
342
0
  if(Curl_uint32_spbset_first(&bundle->ids, &cpid)) {
343
0
    do {
344
0
      struct connectdata *conn = Curl_uint32_tbl_get(&cpool->conns, cpid);
345
0
      if(cpool_conn_can_be_closed(conn)) {
346
        /* Set higher score for the age passed since the connection was used */
347
0
        unused_ms =
348
0
          curlx_ptimediff_ms(pnow, &conn->created) - conn->lastused_ms;
349
0
        if(unused_ms > oldest_ms) {
350
0
          oldest_ms = unused_ms;
351
0
          oldest_id = cpid;
352
0
        }
353
0
      }
354
0
      else if(!conn) {
355
0
        DEBUGASSERT(0);
356
0
        Curl_uint32_spbset_remove(&bundle->ids, cpid);
357
0
      }
358
0
    } while(Curl_uint32_spbset_next(&bundle->ids, cpid, &cpid));
359
0
  }
360
0
  return Curl_uint32_tbl_get(&cpool->conns, oldest_id);
361
0
}
362
363
static struct connectdata *cpool_get_oldest_idle(struct cpool *cpool,
364
                                                 const struct curltime *pnow,
365
                                                 timediff_t min_age_ms)
366
0
{
367
0
  uint32_t cpid, oldest_id = UINT32_MAX;
368
0
  timediff_t oldest_idle_ms = -1;
369
0
  timediff_t idle_ms;
370
371
  /* Iterate over all connections that once were idle. They may
372
   * no longer be, in which case we remove them from the set again. */
373
0
  if(Curl_uint32_bset_first(&cpool->idles, &cpid)) {
374
0
    do {
375
0
      struct connectdata *conn = Curl_uint32_tbl_get(&cpool->conns, cpid);
376
0
      if(cpool_conn_can_be_closed(conn)) {
377
0
        idle_ms = curlx_ptimediff_ms(pnow, &conn->created) - conn->lastused_ms;
378
0
        if((idle_ms >= min_age_ms) && (idle_ms > oldest_idle_ms)) {
379
0
          oldest_idle_ms = idle_ms;
380
0
          oldest_id = cpid;
381
0
        }
382
0
      }
383
0
      else {
384
0
        DEBUGASSERT(conn);
385
0
        Curl_uint32_bset_remove(&cpool->idles, cpid);
386
0
      }
387
0
    } while(Curl_uint32_bset_next(&cpool->idles, cpid, &cpid));
388
0
  }
389
0
  return Curl_uint32_tbl_get(&cpool->conns, oldest_id);
390
0
}
391
392
static void cpool_conn_close(struct cpool *cpool,
393
                             struct Curl_easy *data,
394
                             struct connectdata *conn,
395
                             bool aborted)
396
7.73k
{
397
7.73k
  struct Curl_easy *admin;
398
7.73k
  bool do_lock;
399
400
7.73k
  DEBUGASSERT(cpool);
401
7.73k
  DEBUGASSERT(data && !data->conn);
402
7.73k
  if(!cpool)
403
0
    return;
404
405
  /* If this connection is still used by transfers, we cannot close it. */
406
7.73k
  if(CONN_INUSE(conn)) {
407
0
    DEBUGASSERT(0); /* we'd like to know when this happens */
408
0
    DEBUGF(infof(data, "conn terminate when inuse: %u", conn->attached_xfers));
409
0
    return;
410
0
  }
411
412
  /* This method may be called while we are under lock, e.g. from a
413
   * user callback in find. */
414
7.73k
  admin = Curl_get_admin(data);
415
7.73k
  do_lock = !CPOOL_IS_LOCKED(cpool);
416
7.73k
  if(do_lock)
417
8
    CPOOL_LOCK(cpool, admin);
418
419
7.73k
  if(conn->cpid != UINT32_MAX) {
420
7.73k
    cpool_remove_conn(cpool, conn);
421
7.73k
    DEBUGASSERT(conn->cpid == UINT32_MAX);
422
7.73k
  }
423
424
  /* treat the connection as aborted in CONNECT_ONLY situations,
425
   * so no graceful shutdown is attempted. */
426
7.73k
  if(conn->bits.connect_only)
427
0
    aborted = TRUE;
428
429
7.73k
  if(data->multi) {
430
    /* Add it to the multi's cpool for shutdown handling */
431
7.73k
    infof(data, "%s connection #%" FMT_OFF_T,
432
7.73k
          aborted ? "closing" : "shutting down", conn->connection_id);
433
7.73k
    cpool_discard_conn(&data->multi->cpool, data, conn, aborted);
434
7.73k
  }
435
0
  else {
436
    /* No multi available, terminate */
437
0
    infof(data, "closing connection #%" FMT_OFF_T, conn->connection_id);
438
0
    Curl_cshutdn_terminate(admin, conn, !aborted);
439
0
  }
440
441
7.73k
  if(do_lock)
442
8
    CPOOL_UNLOCK(cpool, admin);
443
7.73k
}
444
445
void Curl_conn_close(struct Curl_easy *data,
446
                     struct connectdata *conn,
447
                     bool aborted)
448
6.62k
{
449
6.62k
  struct cpool *cpool = cpool_get_instance(data);
450
6.62k
  cpool_conn_close(cpool, data, conn, aborted);
451
6.62k
}
452
453
/* Evict an idle connection to make room in the pool. A pool owned by
454
 * a share has no multi that could perform a controlled shutdown of the
455
 * connection; terminate it right away. Otherwise, hand it to the
456
 * transfer's multi for shutdown. Expects the pool to be locked. */
457
static void cpool_evict_conn(struct cpool *cpool,
458
                             struct Curl_easy *admin,
459
                             struct connectdata *conn)
460
0
{
461
0
  if(cpool->share) {
462
0
    cpool_remove_conn(cpool, conn);
463
0
    Curl_cshutdn_terminate(admin, conn, TRUE);
464
0
  }
465
0
  else
466
0
    cpool_conn_close(cpool, admin, conn, FALSE);
467
0
}
468
469
10.0k
#define CPOOL_LIMIT_OK     0
470
0
#define CPOOL_LIMIT_DEST   1
471
0
#define CPOOL_LIMIT_TOTAL  2
472
473
static int cpool_check_limits(struct Curl_easy *data,
474
                              uint32_t max_total,
475
                              struct connectdata *to_add,
476
                              uint32_t max_host,
477
                              struct cpool_bundle *bundle,
478
                              const struct curltime *pnow)
479
10.0k
{
480
10.0k
  struct cpool *cpool = cpool_get_instance(data);
481
10.0k
  struct cshutdn *cshutdn = data->multi ? &data->multi->cshutdn : NULL;
482
10.0k
  struct Curl_easy *admin = NULL;
483
484
10.0k
  if(!cpool)
485
0
    return CPOOL_LIMIT_OK;
486
487
10.0k
  if(!max_total && !max_host)
488
10.0k
    return CPOOL_LIMIT_OK;
489
490
0
  if(max_host) {
491
0
    size_t live = 0, shutdowns = 0;
492
    /* if we are at or above `dest_limit`, try to get rid of connections
493
     * in shutdown and, if that does not lower it, evict idle connections
494
     * from the pool. */
495
0
    admin = Curl_get_admin(data);
496
0
    if(bundle) {
497
0
      live = Curl_uint32_spbset_count(&bundle->ids);
498
0
      if(live >= max_host) {
499
0
        size_t over = live - max_host + 1;
500
0
        for(; over; --over) {
501
0
          struct connectdata *oldest_idle =
502
0
            cpool_bundle_get_oldest_idle(cpool, bundle, pnow);
503
0
          if(!oldest_idle)
504
0
            break;
505
          /* disconnect the old conn and continue */
506
0
          CURL_TRC_M(admin, "Shutting down connection #%" FMT_OFF_T
507
0
                     " to '%s' due to destination limit of %u",
508
0
                     oldest_idle->connection_id, oldest_idle->destination,
509
0
                     max_host);
510
0
          cpool_evict_conn(cpool, admin, oldest_idle);
511
0
        }
512
0
        live = Curl_uint32_spbset_count(&bundle->ids);
513
0
      }
514
0
    }
515
0
    if(cshutdn) {
516
0
      shutdowns = Curl_cshutdn_dest_count(cshutdn, to_add->destination);
517
0
      if(shutdowns && ((live + shutdowns) >= max_host)) {
518
0
        size_t over = live + shutdowns - max_host + 1;
519
0
        shutdowns -= Curl_cshutdn_close_oldest(cshutdn, admin,
520
0
                                               to_add->destination, over);
521
0
      }
522
0
    }
523
    /* We may not have gotten rid of as many connections as we wanted to */
524
0
    if((live + shutdowns) >= max_host)
525
0
      return CPOOL_LIMIT_DEST;
526
0
  }
527
528
  /* Internal transfers are outside the total limit */
529
0
  if(max_total && !data->state.internal) {
530
0
    size_t shutdowns = 0;
531
0
    uint32_t num_conns = Curl_uint32_tbl_count(&cpool->conns);
532
533
0
    if(!admin)
534
0
      admin = Curl_get_admin(data);
535
0
    if(num_conns >= max_total) {
536
0
      size_t over = num_conns - max_total + 1;
537
0
      for(; over; --over) {
538
0
        struct connectdata *oldest_idle =
539
0
          cpool_get_oldest_idle(cpool, pnow, 0);
540
0
        if(!oldest_idle)
541
0
          break;
542
        /* disconnect the old conn and continue */
543
0
        CURL_TRC_M(admin, "Shutting down idle connection #%"
544
0
                   FMT_OFF_T " to '%s' due to total limit of %u",
545
0
                   oldest_idle->connection_id, oldest_idle->destination,
546
0
                   max_total);
547
0
        cpool_evict_conn(cpool, admin, oldest_idle);
548
0
      }
549
0
    }
550
551
0
    num_conns = Curl_uint32_tbl_count(&cpool->conns);
552
0
    if(cshutdn) {
553
0
      shutdowns = Curl_cshutdn_count(cshutdn);
554
0
      if(shutdowns && (num_conns + shutdowns) >= max_total) {
555
0
        size_t over = num_conns + shutdowns - max_total + 1;
556
0
        shutdowns -= Curl_cshutdn_close_oldest(cshutdn, admin, NULL, over);
557
0
      }
558
0
    }
559
0
    if((num_conns + shutdowns) >= max_total)
560
0
      return CPOOL_LIMIT_TOTAL;
561
0
  }
562
563
0
  return CPOOL_LIMIT_OK;
564
0
}
565
566
CURLcode Curl_cpool_add(struct Curl_easy *data,
567
                        struct connectdata *conn,
568
                        uint32_t max_total,
569
                        uint32_t max_host,
570
                        const struct curltime *pnow)
571
10.0k
{
572
10.0k
  CURLcode result = CURLE_OK;
573
10.0k
  struct cpool_bundle *bundle = NULL;
574
10.0k
  struct cpool *cpool = cpool_get_instance(data);
575
10.0k
  uint32_t capacity, cpid;
576
10.0k
  DEBUGASSERT(conn);
577
578
10.0k
  DEBUGASSERT(cpool);
579
10.0k
  if(!cpool)
580
0
    return CURLE_FAILED_INIT;
581
582
10.0k
  DEBUGASSERT(conn->cpid == UINT32_MAX);
583
10.0k
  conn->created = *pnow;
584
10.0k
  conn->connect_start_ms = -1;
585
586
10.0k
  CPOOL_LOCK(cpool, data);
587
  /* Find the bundle, should it exist, and check the limits */
588
10.0k
  bundle = cpool_get_bundle(cpool, conn->destination);
589
590
10.0k
  switch(cpool_check_limits(data, max_total, conn, max_host, bundle, pnow)) {
591
0
  case CPOOL_LIMIT_DEST:
592
0
    infof(data, "No more connections allowed to host");
593
0
    result = CURLE_NO_CONNECTION_AVAILABLE;
594
0
    goto out;
595
0
  case CPOOL_LIMIT_TOTAL:
596
0
    infof(data, "No connections available, total of %u reached.",
597
0
          data->multi->max_total_connections);
598
0
    result = CURLE_NO_CONNECTION_AVAILABLE;
599
0
    goto out;
600
10.0k
  default:
601
10.0k
    break;
602
10.0k
  }
603
604
  /* We add the connection */
605
10.0k
  capacity = Curl_uint32_tbl_capacity(&cpool->conns);
606
10.0k
  if(capacity == UINT32_MAX) {
607
0
    infof(data, "No connections available, pool at max capacity");
608
0
    result = CURLE_NO_CONNECTION_AVAILABLE;
609
0
    goto out;
610
0
  }
611
10.0k
  if(Curl_uint32_tbl_count(&cpool->conns) >= capacity) {
612
8.54k
    uint32_t growth, ncapacity;
613
614
8.54k
    growth = CURLMAX(capacity, 16);
615
8.54k
    if((UINT32_MAX - growth) >= capacity)
616
8.54k
      ncapacity = capacity + growth;
617
0
    else
618
0
      ncapacity = UINT32_MAX;
619
    /* Grow bitset first, so table resize failure does not leave a too
620
     * short bitset should that one fail. */
621
8.54k
    if(Curl_uint32_bset_resize(&cpool->idles, ncapacity) ||
622
8.53k
       Curl_uint32_tbl_resize(&cpool->conns, ncapacity)) {
623
6
      result = CURLE_OUT_OF_MEMORY;
624
6
      goto out;
625
6
    }
626
8.54k
  }
627
628
10.0k
  if(!bundle) {
629
9.73k
    bundle = cpool_create_bundle();
630
9.73k
    if(!bundle) {
631
3
      result = CURLE_OUT_OF_MEMORY;
632
3
      goto out;
633
3
    }
634
9.73k
  }
635
636
10.0k
  if(!Curl_uint32_tbl_add(&cpool->conns, conn, &cpid)) {
637
0
    DEBUGASSERT(0);
638
0
    result = CURLE_OUT_OF_MEMORY;
639
0
    goto out;
640
0
  }
641
10.0k
  conn->cpid = cpid;
642
643
10.0k
  if(!cpool_bundle_add_id(cpool, bundle, conn->destination, cpid)) {
644
3
    Curl_uint32_tbl_remove(&cpool->conns, cpid);
645
3
    conn->cpid = UINT32_MAX;
646
3
    result = CURLE_OUT_OF_MEMORY;
647
3
    goto out;
648
3
  }
649
650
10.0k
  conn->connection_id = cpool->next_connection_id++;
651
10.0k
  CURL_TRC_M(data, "[CPOOL] added connection %" FMT_OFF_T ". "
652
10.0k
             "The cache now contains %u members",
653
10.0k
             conn->connection_id, Curl_uint32_tbl_count(&cpool->conns));
654
10.0k
out:
655
10.0k
  cpool_bundle_unlink(&bundle);
656
10.0k
  CPOOL_UNLOCK(cpool, data);
657
658
10.0k
  return result;
659
10.0k
}
660
661
/* This function iterates the entire connection pool and calls the function
662
   func() with the connection pointer as the first argument and the supplied
663
   'param' argument as the other.
664
665
   The cpool lock is still held when the callback is called. It needs it,
666
   so that it can safely continue traversing the lists once the callback
667
   returns.
668
669
   Returns TRUE if the loop was aborted due to the callback's return code.
670
671
   Return 0 from func() to continue the loop, return 1 to abort it.
672
 */
673
static bool cpool_foreach(struct Curl_easy *data,
674
                          struct cpool *cpool,
675
                          void *param,
676
                          int (*func)(struct cpool *cpool,
677
                                      struct Curl_easy *data,
678
                                      struct connectdata *conn, void *param))
679
8.54k
{
680
8.54k
  void *entry;
681
8.54k
  uint32_t cpid;
682
683
8.54k
  if(!cpool)
684
0
    return FALSE;
685
686
8.54k
  if(Curl_uint32_tbl_first(&cpool->conns, &cpid, &entry)) {
687
0
    do {
688
0
      struct connectdata *conn = entry;
689
0
      if(conn && func(cpool, data, conn, param) == 1)
690
0
        return TRUE;
691
0
    } while(Curl_uint32_tbl_next(&cpool->conns, cpid, &cpid, &entry));
692
0
  }
693
8.54k
  return FALSE;
694
8.54k
}
695
696
/*
697
 * A connection (already in the pool) has become idle. Do any
698
 * cleanups in regard to the pool's limits.
699
 *
700
 * Return TRUE if idle connection kept in pool, FALSE if closed.
701
 */
702
static bool cpool_conn_now_idle(struct cpool *cpool,
703
                                struct Curl_easy *data,
704
                                struct connectdata *conn,
705
                                const struct curltime *pnow)
706
3.45k
{
707
3.45k
  struct connectdata *oldest_idle = NULL;
708
3.45k
  struct Curl_easy *admin;
709
3.45k
  uint32_t maxconnects;
710
3.45k
  bool kept = TRUE;
711
3.45k
  timediff_t min_age_ms = 0;
712
713
3.45k
  if(!data || !data->multi)
714
0
    return kept;
715
716
3.45k
  Curl_uint32_bset_add(&cpool->idles, conn->cpid);
717
718
3.45k
  if(!data->multi->maxconnects) {
719
    /* Attached transfers is a weak indicator of business. */
720
3.45k
    uint32_t attached = Curl_multi_xfers_attached(data->multi);
721
3.45k
    maxconnects = (attached <= UINT32_MAX / 2) ? attached * 2 : UINT32_MAX;
722
    /* We are guessing. So, only evict a "seemingly superfluous" connection
723
     * when has not been used for this long, */
724
3.45k
    min_age_ms = 1000;
725
3.45k
  }
726
0
  else {
727
0
    maxconnects = data->multi->maxconnects;
728
0
  }
729
730
3.45k
  if(cpool && maxconnects && (maxconnects < UINT32_MAX)) {
731
3.45k
    uint32_t num_conns = Curl_uint32_tbl_count(&cpool->conns);
732
3.45k
    admin = Curl_get_admin(data);
733
3.45k
    if(num_conns > maxconnects) {
734
0
      infof(data, "Connection pool is full, closing the oldest of %u/%u",
735
0
            num_conns, maxconnects);
736
0
      oldest_idle = cpool_get_oldest_idle(cpool, pnow, min_age_ms);
737
0
      kept = (oldest_idle != conn);
738
0
      if(oldest_idle) {
739
0
        cpool_evict_conn(cpool, admin, oldest_idle);
740
0
      }
741
0
    }
742
3.45k
  }
743
744
3.45k
  return kept;
745
3.45k
}
746
747
static void cpool_prune_dead(struct cpool *cpool,
748
                             struct Curl_easy *data,
749
                             const struct curltime *pnow);
750
751
static bool cpool_find_act(struct cpool *cpool,
752
                           struct Curl_easy *data,
753
                           struct connectdata *conn,
754
                           cpool_match_result match)
755
1.45k
{
756
1.45k
  switch(match) {
757
0
  case CPOOL_MATCH_FOUND:
758
0
    return TRUE;
759
0
  case CPOOL_MATCH_TOO_OLD:
760
0
    if(!cpool_conn_can_be_closed(conn))
761
0
      return FALSE;
762
0
    FALLTHROUGH();
763
1.10k
  case CPOOL_MATCH_CLOSE:
764
1.10k
    cpool_conn_close(cpool, data, conn, FALSE);
765
1.10k
    break;
766
349
  default:
767
349
    break;
768
1.45k
  }
769
1.45k
  return FALSE;
770
1.45k
}
771
772
bool Curl_cpool_find(struct Curl_easy *data,
773
                     const char *destination,
774
                     bool prune_dead,
775
                     const struct curltime *pnow,
776
                     Curl_cpool_conn_match_cb *conn_cb,
777
                     Curl_cpool_done_match_cb *done_cb,
778
                     void *userdata)
779
10.0k
{
780
10.0k
  struct cpool *cpool = cpool_get_instance(data);
781
10.0k
  struct cpool_bundle *bundle = NULL;
782
10.0k
  bool found = FALSE;
783
784
10.0k
  DEBUGASSERT(cpool);
785
10.0k
  DEBUGASSERT(conn_cb);
786
10.0k
  if(!cpool)
787
0
    return FALSE;
788
789
10.0k
  CPOOL_LOCK(cpool, data);
790
791
10.0k
  if(prune_dead)
792
10.0k
    cpool_prune_dead(cpool, data, pnow);
793
794
10.0k
  if(data->state.last_cpid != UINT32_MAX) {
795
0
    struct connectdata *conn =
796
0
      Curl_uint32_tbl_get(&cpool->conns, data->state.last_cpid);
797
0
    if(conn && (data->state.last_conn_id == conn->connection_id)) {
798
      /* Indeed the last connection used by `data` still exists.
799
       * Check that first, so that connection use gets "sticky". */
800
0
      found = cpool_find_act(cpool, data, conn, conn_cb(conn, userdata));
801
0
      if(found)
802
0
        goto out;
803
0
    }
804
0
    else {
805
      /* No longer found here. */
806
0
      data->state.last_conn_id = -1;
807
0
      data->state.last_cpid = UINT32_MAX;
808
0
    }
809
0
  }
810
811
10.0k
  bundle = cpool_get_bundle(cpool, destination);
812
10.0k
  if(bundle) {
813
1.45k
    uint32_t cpid;
814
1.45k
    if(Curl_uint32_spbset_first(&bundle->ids, &cpid)) {
815
1.45k
      do {
816
1.45k
        struct connectdata *conn = Curl_uint32_tbl_get(&cpool->conns, cpid);
817
1.45k
        if(conn) {
818
1.45k
          if(data->state.last_conn_id != conn->connection_id) {
819
1.45k
            found = cpool_find_act(cpool, data, conn, conn_cb(conn, userdata));
820
1.45k
            if(found)
821
0
              goto out;
822
1.45k
          }
823
1.45k
        }
824
0
        else {
825
0
          DEBUGASSERT(0);
826
0
          Curl_uint32_spbset_remove(&bundle->ids, cpid);
827
0
        }
828
1.45k
      } while(Curl_uint32_spbset_next(&bundle->ids, cpid, &cpid));
829
1.45k
    }
830
1.45k
  }
831
832
10.0k
out:
833
10.0k
  if(done_cb) {
834
10.0k
    found = done_cb(userdata);
835
10.0k
  }
836
10.0k
  cpool_bundle_unlink(&bundle);
837
10.0k
  CPOOL_UNLOCK(cpool, data);
838
10.0k
  return found;
839
10.0k
}
840
841
struct cpool_reaper_ctx {
842
  size_t reaped;
843
  const struct curltime *pnow;
844
};
845
846
static int cpool_reap_dead_cb(struct cpool *cpool,
847
                              struct Curl_easy *admin,
848
                              struct connectdata *conn, void *param)
849
0
{
850
0
  struct cpool_reaper_ctx *reaper = param;
851
852
0
  if(cpool_conn_can_be_closed(conn)) {
853
0
    if(conn->bits.no_reuse || conn->bits.close ||
854
0
       !Curl_cpool_conn_seems_healthy(conn, admin, reaper->pnow)) {
855
      /* terminate conn and stop the iteration */
856
0
      reaper->reaped++;
857
0
      cpool_conn_close(cpool, admin, conn, FALSE);
858
0
    }
859
0
  }
860
0
  return 0; /* continue iteration */
861
0
}
862
863
/*
864
 * This function scans the data's connection pool for half-open/dead
865
 * connections, closes and removes them.
866
 * The cleanup is done at most once per second.
867
 */
868
static void cpool_prune_dead(struct cpool *cpool,
869
                             struct Curl_easy *data,
870
                             const struct curltime *pnow)
871
10.0k
{
872
10.0k
  timediff_t elapsed_ms = curlx_ptimediff_ms(pnow, &cpool->last_cleanup);
873
874
10.0k
  if(elapsed_ms >= 1000L) {
875
8.54k
    struct Curl_easy *admin = Curl_get_admin(data);
876
8.54k
    struct cpool_reaper_ctx reaper;
877
878
8.54k
    memset(&reaper, 0, sizeof(reaper));
879
8.54k
    reaper.pnow = pnow;
880
8.54k
    cpool_foreach(admin, cpool, &reaper, cpool_reap_dead_cb);
881
8.54k
    cpool->last_cleanup = *pnow;
882
8.54k
  }
883
10.0k
}
884
885
static int conn_upkeep(struct cpool *cpool,
886
                       struct Curl_easy *admin,
887
                       struct connectdata *conn,
888
                       void *param)
889
0
{
890
0
  const struct curltime *pnow = Curl_pgrs_now(admin);
891
892
0
  (void)param;
893
0
  if((curlx_ptimediff_ms(pnow, &conn->created) - conn->lastupkeep_ms) >=
894
0
     admin->set.upkeep_interval_ms) {
895
0
    CURLcode result;
896
897
0
    conn->lastupkeep_ms = curlx_ptimediff_ms(pnow, &conn->created);
898
    /* briefly attach for action */
899
0
    Curl_attach_connection(admin, conn, FALSE);
900
0
    result = Curl_conn_keep_alive(admin, conn);
901
0
    Curl_detach_connection(admin);
902
903
0
    if(result && cpool_conn_can_be_closed(conn)) {
904
0
      cpool_conn_close(cpool, admin, conn, FALSE);
905
0
    }
906
0
  }
907
0
  return 0; /* continue iteration */
908
0
}
909
910
CURLcode Curl_cpool_upkeep(struct Curl_easy *data)
911
0
{
912
0
  struct cpool *cpool = cpool_get_instance(data);
913
0
  struct Curl_easy *admin = Curl_get_admin(data);
914
915
0
  if(!cpool)
916
0
    return CURLE_OK;
917
918
0
  CPOOL_LOCK(cpool, admin);
919
0
  cpool_foreach(admin, cpool, NULL, conn_upkeep);
920
0
  CPOOL_UNLOCK(cpool, admin);
921
0
  return CURLE_OK;
922
0
}
923
924
struct connectdata *Curl_cpool_get_last_conn(struct Curl_easy *data)
925
9.04k
{
926
9.04k
  struct cpool *cpool = cpool_get_instance(data);
927
9.04k
  if(cpool && (data->state.last_cpid != UINT32_MAX)) {
928
0
    struct connectdata *conn;
929
930
0
    CPOOL_LOCK(cpool, data);
931
0
    conn = Curl_uint32_tbl_get(&cpool->conns, data->state.last_cpid);
932
0
    CPOOL_UNLOCK(cpool, data);
933
0
    if(conn && (data->state.last_conn_id == conn->connection_id))
934
0
      return conn;
935
0
    data->state.last_cpid = UINT32_MAX;
936
0
    data->state.last_conn_id = -1;
937
0
  }
938
9.04k
  return NULL;
939
9.04k
}
940
941
void Curl_cpool_return(struct Curl_easy *data,
942
                       struct connectdata *conn,
943
                       Curl_cpool_return_cb *cb, void *cbdata,
944
                       const struct curltime *pnow)
945
10.0k
{
946
10.0k
  struct cpool *cpool = cpool_get_instance(data);
947
948
10.0k
  CPOOL_LOCK(cpool, data);
949
950
  /* remember times, connection had been used just before */
951
10.0k
  conn->lastchecked_ms = conn->lastupkeep_ms = conn->lastused_ms =
952
10.0k
    curlx_ptimediff_ms(pnow, &conn->created);
953
954
10.0k
  switch(cb(data, conn, cbdata, pnow)) {
955
0
  case CPOOL_DO_KEEP:
956
0
    break;
957
3.45k
  case CPOOL_DO_IDLE:
958
    /* the connection is no longer in use by any transfer */
959
3.45k
    if(cpool_conn_now_idle(cpool, data, conn, pnow)) {
960
      /* connection kept in the cpool */
961
3.45k
      infof(data, "Connection #%" FMT_OFF_T " to host %s:%u left intact",
962
3.45k
            conn->connection_id, conn->origin->user_hostname,
963
3.45k
            conn->origin->port);
964
3.45k
    }
965
0
    else { /* connection was removed from the cpool and destroyed. */
966
0
      data->state.last_conn_id = -1;
967
0
      data->state.last_cpid = UINT32_MAX;
968
0
    }
969
3.45k
    break;
970
252
  case CPOOL_DO_CLOSE:
971
252
    Curl_conn_close(data, conn, FALSE);
972
252
    break;
973
6.36k
  case CPOOL_DO_TERMINATE:
974
6.36k
    Curl_conn_close(data, conn, TRUE);
975
6.36k
    break;
976
10.0k
  }
977
978
10.0k
  CPOOL_UNLOCK(cpool, data);
979
10.0k
}
980
981
static int cpool_reap_no_reuse(struct cpool *cpool,
982
                               struct Curl_easy *admin,
983
                               struct connectdata *conn, void *param)
984
0
{
985
0
  (void)param;
986
0
  conn->bits.no_reuse = TRUE;
987
0
  if(cpool_conn_can_be_closed(conn))
988
0
    cpool_conn_close(cpool, admin, conn, FALSE);
989
0
  return 0; /* continue iteration */
990
0
}
991
992
void Curl_cpool_nw_changed(struct cpool *cpool, struct Curl_easy *admin)
993
0
{
994
0
  if(cpool && admin) {
995
0
    CPOOL_LOCK(cpool, admin);
996
0
    cpool_foreach(admin, cpool, NULL, cpool_reap_no_reuse);
997
0
    CPOOL_UNLOCK(cpool, admin);
998
0
  }
999
0
}
1000
1001
/* A connection has to have been idle for less than 'conn_max_idle_ms'
1002
   (the success rate is too low after this), or created less than
1003
   'conn_max_age_ms' ago, to be subject for reuse. */
1004
static bool cpool_conn_too_old(struct Curl_easy *data,
1005
                               struct connectdata *conn,
1006
                               const struct curltime *pnow)
1007
1.10k
{
1008
1.10k
  timediff_t age_ms;
1009
1010
1.10k
  if(data->set.conn_max_idle_ms) {
1011
1.10k
    age_ms = curlx_ptimediff_ms(pnow, &conn->created) - conn->lastused_ms;
1012
1.10k
    if(age_ms > data->set.conn_max_idle_ms) {
1013
0
      infof(data, "Too old connection (%" FMT_TIMEDIFF_T
1014
0
            " ms idle, max idle is %" FMT_TIMEDIFF_T " ms), disconnect it",
1015
0
            age_ms, data->set.conn_max_idle_ms);
1016
0
      return TRUE;
1017
0
    }
1018
1.10k
  }
1019
1020
1.10k
  if(data->set.conn_max_age_ms) {
1021
1.10k
    age_ms = curlx_ptimediff_ms(pnow, &conn->created);
1022
1.10k
    if(age_ms > data->set.conn_max_age_ms) {
1023
0
      infof(data,
1024
0
            "Too old connection (created %" FMT_TIMEDIFF_T
1025
0
            " ms ago, max lifetime is %" FMT_TIMEDIFF_T " ms), disconnect it",
1026
0
            age_ms, data->set.conn_max_age_ms);
1027
0
      return TRUE;
1028
0
    }
1029
1.10k
  }
1030
1031
1.10k
  return FALSE;
1032
1.10k
}
1033
1034
bool Curl_cpool_conn_seems_healthy(struct connectdata *conn,
1035
                                   struct Curl_easy *data,
1036
                                   const struct curltime *pnow)
1037
1.10k
{
1038
1.10k
  struct Curl_easy *admin;
1039
1.10k
  bool healthy = TRUE;
1040
1041
1.10k
  DEBUGASSERT(!data->conn);
1042
1.10k
  if(cpool_conn_can_be_closed(conn) && cpool_conn_too_old(data, conn, pnow))
1043
0
    return FALSE;
1044
1045
  /* An idle, non-multiplexed connection must not be reused when it has
1046
   * input pending (RFC 9112, section 9.2). Check this:
1047
   * - on a connection that is not in use
1048
   * - or a connection having its own "is dead" callback
1049
   * - or the last check was more than a second ago */
1050
1.10k
  if(CONN_INUSE(conn) &&
1051
0
     !conn->scheme->run->connection_is_dead &&
1052
0
     (curlx_ptimediff_ms(pnow, &conn->created) - conn->lastchecked_ms) < 1000)
1053
0
    return TRUE;
1054
1055
1.10k
  admin = Curl_get_admin(data);
1056
1.10k
  if(conn->scheme->run->connection_is_dead) {
1057
1.10k
    Curl_attach_connection(admin, conn, FALSE);
1058
1.10k
    healthy = !conn->scheme->run->connection_is_dead(admin, conn);
1059
1.10k
    Curl_detach_connection(admin);
1060
1.10k
  }
1061
0
  else {
1062
0
    bool input_pending = FALSE;
1063
1064
0
    Curl_attach_connection(admin, conn, FALSE);
1065
0
    healthy = Curl_conn_is_alive(admin, conn, &input_pending);
1066
0
    Curl_detach_connection(admin);
1067
0
    if(healthy && input_pending && !CONN_INUSE(conn) &&
1068
0
       !Curl_conn_is_multiplex(conn)) {
1069
      /* Non-multiplexed connections without attached transfers should
1070
       * not have input pending. The input might be a TLS Notify Close,
1071
       * for all we know. */
1072
0
      DEBUGF(infof(data, "connection has no transfer but input, not healthy"));
1073
0
      healthy = FALSE;
1074
0
    }
1075
0
  }
1076
1077
1.10k
  if(healthy)
1078
0
    conn->lastchecked_ms = curlx_ptimediff_ms(pnow, &conn->created);
1079
1.10k
  return healthy;
1080
1.10k
}
1081
1082
void Curl_cpool_conn_was_used(struct Curl_easy *data,
1083
                              struct connectdata *conn,
1084
                              const struct curltime *pnow)
1085
9.88k
{
1086
9.88k
  (void)data;
1087
9.88k
  conn->lastupkeep_ms = curlx_ptimediff_ms(pnow, &conn->created);
1088
9.88k
}
1089
1090
timediff_t Curl_cpool_conn_age_ms(struct Curl_easy *data,
1091
                                  struct connectdata *conn,
1092
                                  const struct curltime *pnow)
1093
1.10k
{
1094
1.10k
  (void)data;
1095
1.10k
  return curlx_ptimediff_ms(pnow, &conn->created);
1096
1.10k
}