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
410@item reconnect_streamed410@item reconnect_streamed
411If set then even streamed/non seekable streams will be reconnected on errors.411If set then even streamed/non seekable streams will be reconnected on errors.
412 412
413@item reconnect_on_network_error
414Reconnect automatically in case of TCP/TLS errors during connect.
415
416@item reconnect_on_http_error
417A comma separated list of HTTP status codes to reconnect on. The list can
418include specific status codes (e.g. '503') or the strings '4xx' / '5xx'.
419
413@item reconnect_delay_max420@item reconnect_delay_max
414Sets the maximum delay in seconds after which to give up reconnecting421Sets the maximum delay in seconds after which to give up reconnecting
415 422
libavformat/http.c+58−2
@@ -119,8 +119,10 @@ typedef struct HTTPContext { · 118 unmodified lines
119 char *method;119 char *method;
120 int reconnect;120 int reconnect;
121 int reconnect_at_eof;121 int reconnect_at_eof;
122 int reconnect_on_network_error;
122 int reconnect_streamed;123 int reconnect_streamed;
123 int reconnect_delay_max;124 int reconnect_delay_max;
125 char *reconnect_on_http_error;
124 int listen;126 int listen;
125 char *resource;127 char *resource;
126 int reply_code;128 int reply_code;
@@ -164,6 +166,8 @@ static const AVOption options[] = { · 37 unmodified lines
164 { "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 },166 { "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 },
165 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },167 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
166 { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },168 { "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 },
167 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },171 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
168 { "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 },172 { "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 },
169 { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },173 { "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
258 return location_changed;262 return location_changed;
259}263}
260 264
265static 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
261/* return non zero if error */298/* return non zero if error */
262static int http_open_cnx(URLContext *h, AVDictionary **options)299static int http_open_cnx(URLContext *h, AVDictionary **options)
263{300{
264 HTTPAuthType cur_auth_type, cur_proxy_auth_type;301 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
265 HTTPContext *s = h->priv_data;302 HTTPContext *s = h->priv_data;
266 int location_changed, attempts = 0, redirects = 0;303 int location_changed, attempts = 0, redirects = 0;
304 int reconnect_delay = 0;
305 uint64_t off;
306
267redo:307redo:
268 av_dict_copy(options, s->chained_options, 0);308 av_dict_copy(options, s->chained_options, 0);
269 309
270 cur_auth_type = s->auth_state.auth_type;310 cur_auth_type = s->auth_state.auth_type;
271 cur_proxy_auth_type = s->auth_state.auth_type;311 cur_proxy_auth_type = s->auth_state.auth_type;
272 312
313 off = s->off;
273 location_changed = http_open_cnx_internal(h, options);314 location_changed = http_open_cnx_internal(h, options);
274 if (location_changed < 0)315 if (location_changed < 0) {
275 goto fail;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 }
276 332
277 attempts++;333 attempts++;
278 if (s->http_code == 401) {334 if (s->http_code == 401) {
libavformat/version.h+1−1
@@ -33,7 +33,7 @@ · 32 unmodified lines
33// Also please add any ticket numbers that you believe might be affected here33// Also please add any ticket numbers that you believe might be affected here
34#define LIBAVFORMAT_VERSION_MAJOR 5834#define LIBAVFORMAT_VERSION_MAJOR 58
35#define LIBAVFORMAT_VERSION_MINOR 6535#define LIBAVFORMAT_VERSION_MINOR 65
36#define LIBAVFORMAT_VERSION_MICRO 10036#define LIBAVFORMAT_VERSION_MICRO 101
37 37
38#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \38#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
39 LIBAVFORMAT_VERSION_MINOR, \39 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