curl-curl/lib/thrdqueue.h
Stefan Eissing 2b3dfb4ad4
lib: make resolving HTTPS DNS records reliable:
- allow to specify when they are wanted on starting a resolve
- match dns cache entries accordingly. An entry which never
  tried to get HTTPS-RRs is no answer for a resolve that wants
  it.
- fix late arrivals of resolve answers to match the "async"
  records that started them - if it still exists.
- provide for multiple "async" resolves in a transfer at the
  same time. We may need to resolve an IP interface while the
  main connection resolve has not finished yet.
- allow lookup of HTTPS-RR information as soon as it is
  available, even if A/AAAA queries are still ongoing.

For this, the "async" infrastructure is changed:

- Defined bits for DNS queries `CURL_DNSQ_A`, `CURL_DNSQ_AAAA`
  and `CURL_DNSQ_HTTPS`. These replace `ip_version` which says
  nothing about HTTPS.
  Use them in dns cache entries for matching.
- enhance the `async->id` to be a unique `uint32_t` for
  resolves inside one multi. This is weak, as the id may
  wrap around. However it is combined with the `mid` of
  the easy handle, making collisions highly unlikely.
  `data->state.async` is only accessed in few places where
  the mid/async-id match is performed.
- vtls: for ECH supporting TLS backends (openssl, rustls, wolfssl),
  retrieve the HTTPS-RR information from the dns connection filter.
  Delay the connect if the HTTPS-RR is needed, but has not
  been resolved yet.

The implementation of all this is complete for the threaded
resolver. c-ares resolver and DoH do not take advantage of
all new async features yet. To be done in separate PRs.

Details:

c-ares: cleanup settings and initialisation. Any ares channel
is only being created on starting a resolve and propagating
operations in setopt.c to the channel are not helpful.

Changed threaded+ares pollset handling so that they do not
overwrite each others `ASYNC_NAME` timeouts.

Add trace name 'threads' for tracing thread queue and
pool used by threaded resolver.

Closes #21175
2026-04-01 15:36:31 +02:00

119 lines
4.6 KiB
C

#ifndef HEADER_CURL_THRDQUEUE_H
#define HEADER_CURL_THRDQUEUE_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"
#include "curlx/timediff.h"
#ifdef USE_THREADS
struct Curl_easy;
struct curl_thrdq;
typedef enum {
CURL_THRDQ_EV_ITEM_DONE /* an item has been processed and is ready */
} Curl_thrdq_event;
/* Notification callback when "events" happen in the queue. May be
* call from any thread, queue is not locked. */
typedef void Curl_thrdq_ev_cb(const struct curl_thrdq *tqueue,
Curl_thrdq_event ev,
void *user_data);
/* Process a queued item. Maybe call from any thread. Queue is
* not locked. */
typedef void Curl_thrdq_item_process_cb(void *item);
/* Free an item. May be called from any thread at any time for an
* item that is in the queue (either before or after processing). */
typedef void Curl_thrdq_item_free_cb(void *item);
/* Create a new queue processing "items" by a thread pool.
*/
CURLcode Curl_thrdq_create(struct curl_thrdq **ptqueue,
const char *name,
uint32_t max_len, /* 0 for unlimited */
uint32_t min_threads,
uint32_t max_threads,
uint32_t idle_time_ms,
Curl_thrdq_item_free_cb *fn_free,
Curl_thrdq_item_process_cb *fn_process,
Curl_thrdq_ev_cb *fn_event, /* optional */
void *user_data);
/* Destroy the queue, free all queued items unprocessed and destroy
* the thread pool used.
* @param join TRUE when thread pool shall be joined. FALSE for
* detaching any running threads.
*/
void Curl_thrdq_destroy(struct curl_thrdq *tqueue, bool join);
/* Send "item" onto the queue. The caller needs to clear any reference
* to "item" on success, e.g. the queue takes ownership.
* `description` is an optional string describing the item for tracing
* purposes. It needs to have the same lifetime as `item`.
* Returns CURLE_AGAIN when the queue has already been full.
*
* With`timeout_ms` != 0, items that get stuck that long in the send
* queue are removed and added to the receive queue right away.
*/
CURLcode Curl_thrdq_send(struct curl_thrdq *tqueue, void *item,
const char *description, timediff_t timeout_ms);
/* Receive the oldest, processed item from the queue again, if there is one.
* The caller takes ownership of the item received, e.g. the queue
* relinquishes all references to item.
* Returns CURLE_AGAIN when there is no processed item, setting `pitem`
* to NULL.
*/
CURLcode Curl_thrdq_recv(struct curl_thrdq *tqueue, void **pitem);
/* Return TRUE if the passed "item" matches. */
typedef bool Curl_thrdq_item_match_cb(void *item, void *match_data);
/* Clear all scheduled/processed items that match from the queue. This
* will *not* be able to clear items that are being processed.
*/
void Curl_thrdq_clear(struct curl_thrdq *tqueue,
Curl_thrdq_item_match_cb *fn_match,
void *match_data);
CURLcode Curl_thrdq_await_done(struct curl_thrdq *tqueue,
uint32_t timeout_ms);
CURLcode Curl_thrdq_set_props(struct curl_thrdq *tqueue,
uint32_t max_len, /* 0 for unlimited */
uint32_t min_threads,
uint32_t max_threads,
uint32_t idle_time_ms);
#ifdef CURLVERBOSE
void Curl_thrdq_trace(struct curl_thrdq *tqueue,
struct Curl_easy *data);
#endif
#endif /* USE_THREADS */
#endif /* HEADER_CURL_THRDQUEUE_H */