aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/http.c
diff options
context:
space:
mode:
authorDerek Buitenhuis <derek.buitenhuis@gmail.com>2024-04-22 15:25:44 +0100
committerDerek Buitenhuis <derek.buitenhuis@gmail.com>2024-04-25 14:17:33 +0100
commit10374ab5eddd8bccaa1b809e5cd8782cf1dc28e8 (patch)
tree3f308ac170f68300e8958ac589cab28d2bbb734c /libavformat/http.c
parenta776d524e1fcc4395d66ad102c1d499fa95d0cce (diff)
downloadffmpeg-10374ab5eddd8bccaa1b809e5cd8782cf1dc28e8.tar.gz
avformat/http: Add options to set the max number of connection retries
Not every use case benefits from setting retries in terms of the backoff. Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
Diffstat (limited to 'libavformat/http.c')
-rw-r--r--libavformat/http.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/libavformat/http.c b/libavformat/http.c
index 8592ef5264..288d27de20 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -138,6 +138,7 @@ typedef struct HTTPContext {
char *new_location;
AVDictionary *redirect_cache;
uint64_t filesize_from_content_range;
+ int reconnect_max_retries;
} HTTPContext;
#define OFFSET(x) offsetof(HTTPContext, x)
@@ -176,6 +177,7 @@ static const AVOption options[] = {
{ "reconnect_on_http_error", "list of http status codes to reconnect on", OFFSET(reconnect_on_http_error), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
{ "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
{ "reconnect_delay_max", "max reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_max), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D },
+ { "reconnect_max_retries", "the max number of times to retry a connection", OFFSET(reconnect_max_retries), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D },
{ "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
{ "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
{ "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
@@ -356,7 +358,7 @@ static int http_open_cnx(URLContext *h, AVDictionary **options)
{
HTTPAuthType cur_auth_type, cur_proxy_auth_type;
HTTPContext *s = h->priv_data;
- int ret, auth_attempts = 0, redirects = 0;
+ int ret, conn_attempts = 1, auth_attempts = 0, redirects = 0;
int reconnect_delay = 0;
uint64_t off;
char *cached;
@@ -383,7 +385,8 @@ redo:
ret = http_open_cnx_internal(h, options);
if (ret < 0) {
if (!http_should_reconnect(s, ret) ||
- reconnect_delay > s->reconnect_delay_max)
+ reconnect_delay > s->reconnect_delay_max ||
+ (s->reconnect_max_retries >= 0 && conn_attempts > s->reconnect_max_retries))
goto fail;
av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s).\n", off, reconnect_delay);
@@ -391,6 +394,7 @@ redo:
if (ret != AVERROR(ETIMEDOUT))
goto fail;
reconnect_delay = 1 + 2 * reconnect_delay;
+ conn_attempts++;
/* restore the offset (http_connect resets it) */
s->off = off;
@@ -1695,6 +1699,7 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
int err, read_ret;
int64_t seek_ret;
int reconnect_delay = 0;
+ int conn_attempts = 1;
if (!s->hd)
return AVERROR_EOF;
@@ -1723,7 +1728,7 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
!(s->reconnect_at_eof && read_ret == AVERROR_EOF))
break;
- if (reconnect_delay > s->reconnect_delay_max)
+ if (reconnect_delay > s->reconnect_delay_max || (s->reconnect_max_retries >= 0 && conn_attempts > s->reconnect_max_retries))
return AVERROR(EIO);
av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s), error=%s.\n", s->off, reconnect_delay, av_err2str(read_ret));
@@ -1731,6 +1736,7 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
if (err != AVERROR(ETIMEDOUT))
return err;
reconnect_delay = 1 + 2*reconnect_delay;
+ conn_attempts++;
seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
if (seek_ret >= 0 && seek_ret != target) {
av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRIu64".\n", target);