History

avformat/http: support retry on connection error

Add 2 new options: - reconnect_on_http_error - a list of http status codes that should be retried. the list can contain explicit status codes / the strings 4xx/5xx. - reconnect_on_network_error - reconnects on arbitrary errors during connect, e.g. ECONNRESET/ETIMEDOUT the retry employs the same exponential backoff logic as the existing reconnect/reconnect_at_eof flags. related tickets: https://trac.ffmpeg.org/ticket/6066 https://trac.ffmpeg.org/ticket/7768

erankor committed 5 years ago

Commit fde55931 parent f26b5ac3 files · +66 −3Browse files

Unified · Split

3 files
  1. doc/protocols.texi +7 −0
  2. libavformat/http.c +58 −2
  3. libavformat/version.h +1 −1
doc/protocols.texi+7−0
@@ -410,6 +410,13 @@ for live / endless streams. · 409 unmodified lines
410410 @item reconnect_streamed
411411 If set then even streamed/non seekable streams will be reconnected on errors.
412412
413+@item reconnect_on_network_error
414+Reconnect automatically in case of TCP/TLS errors during connect.
415+
416+@item reconnect_on_http_error
417+A comma separated list of HTTP status codes to reconnect on. The list can
418+include specific status codes (e.g. '503') or the strings '4xx' / '5xx'.
419+
413420 @item reconnect_delay_max
414421 Sets the maximum delay in seconds after which to give up reconnecting
415422
libavformat/http.c+58−2
@@ -119,8 +119,10 @@ typedef struct HTTPContext { · 118 unmodified lines
119119 char *method;
120120 int reconnect;
121121 int reconnect_at_eof;
122+ int reconnect_on_network_error;
122123 int reconnect_streamed;
123124 int reconnect_delay_max;
125+ char *reconnect_on_http_error;
124126 int listen;
125127 char *resource;
126128 int reply_code;
@@ -164,6 +166,8 @@ static const AVOption options[] = { · 37 unmodified lines
164166 { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
165167 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
166168 { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
169+ { "reconnect_on_network_error", "auto reconnect in case of tcp/tls error during connect", OFFSET(reconnect_on_network_error), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
170+ { "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 },
167171 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
168172 { "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 },
169173 { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
@@ -258,21 +262,73 @@ static int http_open_cnx_internal(URLContext *h, AVDictionary **options) · 88 unmodified lines
258262 return location_changed;
259263 }
260264
265+static int http_should_reconnect(HTTPContext *s, int err)
266+{
267+ const char *status_group;
268+ char http_code[4];
269+
270+ switch (err) {
271+ case AVERROR_HTTP_BAD_REQUEST:
272+ case AVERROR_HTTP_UNAUTHORIZED:
273+ case AVERROR_HTTP_FORBIDDEN:
274+ case AVERROR_HTTP_NOT_FOUND:
275+ case AVERROR_HTTP_OTHER_4XX:
276+ status_group = "4xx";
277+ break;
278+
279+ case AVERROR_HTTP_SERVER_ERROR:
280+ status_group = "5xx";
281+ break;
282+
283+ default:
284+ return s->reconnect_on_network_error;
285+ }
286+
287+ if (!s->reconnect_on_http_error)
288+ return 0;
289+
290+ if (av_match_list(status_group, s->reconnect_on_http_error, ',') > 0)
291+ return 1;
292+
293+ snprintf(http_code, sizeof(http_code), "%d", s->http_code);
294+
295+ return av_match_list(http_code, s->reconnect_on_http_error, ',') > 0;
296+}
297+
261298 /* return non zero if error */
262299 static int http_open_cnx(URLContext *h, AVDictionary **options)
263300 {
264301 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
265302 HTTPContext *s = h->priv_data;
266303 int location_changed, attempts = 0, redirects = 0;
304+ int reconnect_delay = 0;
305+ uint64_t off;
306+
267307 redo:
268308 av_dict_copy(options, s->chained_options, 0);
269309
270310 cur_auth_type = s->auth_state.auth_type;
271311 cur_proxy_auth_type = s->auth_state.auth_type;
272312
313+ off = s->off;
273314 location_changed = http_open_cnx_internal(h, options);
274− if (location_changed < 0)
275− goto fail;
315+ if (location_changed < 0) {
316+ if (!http_should_reconnect(s, location_changed) ||
317+ reconnect_delay > s->reconnect_delay_max)
318+ goto fail;
319+
320+ av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s).\n", off, reconnect_delay);
321+ location_changed = ff_network_sleep_interruptible(1000U * 1000 * reconnect_delay, &h->interrupt_callback);
322+ if (location_changed != AVERROR(ETIMEDOUT))
323+ goto fail;
324+ reconnect_delay = 1 + 2 * reconnect_delay;
325+
326+ /* restore the offset (http_connect resets it) */
327+ s->off = off;
328+
329+ ffurl_closep(&s->hd);
330+ goto redo;
331+ }
276332
277333 attempts++;
278334 if (s->http_code == 401) {
libavformat/version.h+1−1
@@ -33,7 +33,7 @@ · 32 unmodified lines
3333 // Also please add any ticket numbers that you believe might be affected here
3434 #define LIBAVFORMAT_VERSION_MAJOR 58
3535 #define LIBAVFORMAT_VERSION_MINOR 65
36−#define LIBAVFORMAT_VERSION_MICRO 100
36+#define LIBAVFORMAT_VERSION_MICRO 101
3737
3838 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
3939 LIBAVFORMAT_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t