diff options
author | Martin Storsjö <martin@martin.st> | 2018-08-10 10:25:59 +0300 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2018-08-31 12:26:51 +0300 |
commit | 9b4c3f5aadf54ffd2a6e15746b1fd736379883c4 (patch) | |
tree | 826c8a9c283c79a7e468a6bf59a491d2c4c10f05 /libavformat/network.h | |
parent | 69caad8959982580504643d36aef22528e4aa6ce (diff) | |
download | ffmpeg-9b4c3f5aadf54ffd2a6e15746b1fd736379883c4.tar.gz |
network: Add RFC 8305 style "Happy Eyeballs"/"Fast Fallback" helper function
For cases with dual stack (IPv4 + IPv6) connectivity, but where one
stack potentially is less reliable, strive to trying to connect over
both protocols in parallel, using whichever address connected first.
In cases with a hostname resolving to multiple IPv4 and IPv6
addresses, the current connection mechanism would try all addresses
in the order returned by getaddrinfo (with all IPv6 addresses ordered
before the IPv4 addresses normally). If connection attempts to the
IPv6 addresses return quickly with an error, this was no problem, but
if they were unsuccessful leading up to timeouts, the connection process
would have to wait for timeouts on all IPv6 target addresses before
attempting any IPv4 address.
Similar to what RFC 8305 suggests, reorder the list of addresses to
try connecting to, interleaving address families. After starting one
connection attempt, start another one in parallel after a small delay
(200 ms as suggested by the RFC).
For cases with unreliable IPv6 but reliable IPv4, this should make
connection attempts work as reliably as with plain IPv4, with only an
extra 200 ms of connection delay.
Signed-off-by: Martin Storsjö <martin@martin.st>
Diffstat (limited to 'libavformat/network.h')
-rw-r--r-- | libavformat/network.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/libavformat/network.h b/libavformat/network.h index 09cee58a5c..f4cb59e5ff 100644 --- a/libavformat/network.h +++ b/libavformat/network.h @@ -256,4 +256,32 @@ int ff_http_match_no_proxy(const char *no_proxy, const char *hostname); int ff_socket(int domain, int type, int protocol); +/** + * Connect to any of the given addrinfo addresses, with multiple attempts + * running in parallel. + * + * @param addrs The list of addresses to try to connect to. + * This list will be mutated internally, but the list head + * will remain as such, so this doesn't affect the caller + * freeing the list afterwards. + * @param timeout_ms_per_address The number of milliseconds to wait for each + * connection attempt. Since multiple addresses are tried, + * some of them in parallel, the total run time will at most + * be timeout_ms_per_address*ceil(nb_addrs/parallel) + + * (parallel - 1) * NEXT_ATTEMPT_DELAY_MS. + * @param parallel The maximum number of connections to attempt in parallel. + * This is limited to an internal maximum capacity. + * @param h URLContext providing interrupt check + * callback and logging context. + * @param fd If successful, the connected socket is returned here. + * @param customize_fd Function that will be called for each socket created, + * to allow the caller to set socket options before calling + * connect() on it, may be NULL. + * @param customize_ctx Context parameter passed to customize_fd. + * @return 0 on success, AVERROR on failure. + */ +int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address, + int parallel, URLContext *h, int *fd, + void (*customize_fd)(void *, int), void *customize_ctx); + #endif /* AVFORMAT_NETWORK_H */ |