diff mbox series

[FFmpeg-devel,v2,6/9] avformat/http: Add options to set the max number of connection retries

Message ID 20240422142547.281064-7-derek.buitenhuis@gmail.com
State New
Headers show
Series HTTP rate limiting and retry improvements | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Derek Buitenhuis April 22, 2024, 2:25 p.m. UTC
Not every use case benefits from setting retries in terms of the backoff.

Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
---
 libavformat/http.c    | 12 +++++++++---
 libavformat/version.h |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)

Comments

Martin Storsjö April 24, 2024, 11:08 a.m. UTC | #1
On Mon, 22 Apr 2024, Derek Buitenhuis wrote:

> Not every use case benefits from setting retries in terms of the backoff.
>
> Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
> ---
> libavformat/http.c    | 12 +++++++++---
> libavformat/version.h |  2 +-
> 2 files changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/libavformat/http.c b/libavformat/http.c
> index 6927fea2fb..06bd3e340e 100644
> --- a/libavformat/http.c
> +++ b/libavformat/http.c
> @@ -140,6 +140,7 @@ typedef struct HTTPContext {
>     uint64_t filesize_from_content_range;
>     int respect_retry_after;
>     unsigned int retry_after;
> +    int reconnect_max_retries;
> } HTTPContext;
>
> #define OFFSET(x) offsetof(HTTPContext, x)
> @@ -178,6 +179,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 },
>     { "respect_retry_after", "respect the Retry-After header when retrying connections", OFFSET(respect_retry_after), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, 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 },
> @@ -359,7 +361,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;
> @@ -386,7 +388,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;
>
>         if (s->respect_retry_after && s->retry_after > 0) {
> @@ -401,6 +404,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;
> @@ -1706,6 +1710,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_attempt = 1;

Minor inconsistency; the corresponding variable in the other function was 
called conn_attempts, as a plural.

// Martin
Derek Buitenhuis April 24, 2024, 7:42 p.m. UTC | #2
On 4/24/2024 12:08 PM, Martin Storsjö wrote:
> Minor inconsistency; the corresponding variable in the other function was 
> called conn_attempts, as a plural.

Renamed to the plural version.

- Derek
diff mbox series

Patch

diff --git a/libavformat/http.c b/libavformat/http.c
index 6927fea2fb..06bd3e340e 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -140,6 +140,7 @@  typedef struct HTTPContext {
     uint64_t filesize_from_content_range;
     int respect_retry_after;
     unsigned int retry_after;
+    int reconnect_max_retries;
 } HTTPContext;
 
 #define OFFSET(x) offsetof(HTTPContext, x)
@@ -178,6 +179,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 },
     { "respect_retry_after", "respect the Retry-After header when retrying connections", OFFSET(respect_retry_after), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, 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 },
@@ -359,7 +361,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;
@@ -386,7 +388,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;
 
         if (s->respect_retry_after && s->retry_after > 0) {
@@ -401,6 +404,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;
@@ -1706,6 +1710,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_attempt = 1;
 
     if (!s->hd)
         return AVERROR_EOF;
@@ -1734,7 +1739,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_attempt > 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));
@@ -1742,6 +1747,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_attempt++;
         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);
diff --git a/libavformat/version.h b/libavformat/version.h
index ee91990360..41dbd4ad01 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@ 
 #include "version_major.h"
 
 #define LIBAVFORMAT_VERSION_MINOR   3
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MICRO 102
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \