From patchwork Mon Apr 22 14:25:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derek Buitenhuis X-Patchwork-Id: 48221 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c906:b0:1a9:af23:56c1 with SMTP id gx6csp2325921pzb; Mon, 22 Apr 2024 07:27:21 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXyjEkN3W+1GDEvDmYI61UPtt0hdy3O6PDf5qkCR6Zdj8vXbwDvsnVaNGyapTVdRW3y8fNXmQbD4fV8S7PtgqU3lQVA0y4UiuFINg== X-Google-Smtp-Source: AGHT+IFcmHtoHFlTtxUgUB73/RjJQEoKKdy6WHEfpn+UYuOXBmtVwrIT+3zMyJaZB93hdqHp4r09 X-Received: by 2002:a17:906:38b:b0:a55:6135:7ab7 with SMTP id b11-20020a170906038b00b00a5561357ab7mr9289480eja.6.1713796041705; Mon, 22 Apr 2024 07:27:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1713796041; cv=none; d=google.com; s=arc-20160816; b=nMe+KCFFI55FOsyq2oJU6NgN4hNwOKsKXS6uaWXUje/EVtUhluLmD2qMiKQ97tKf73 KVlSocbz0X1gzFs1VIE9YBCMlt1nzlYfK1nPcJGKokB/ZPuvvs+5Agta76CO35R0bdaR bWq30d7vG34ZYVRyH65UcDW8paX23bdgv1WKA19n7mSXjlVMf35URjLi/Rcfa7kLEHMP qoO/KNbRWOOXWkTD/XEBFKp+3s1ugF2FsyU1KCJi13Ww8Aw1y0lzeVossJljKI8Ch9U8 6xkVK+8Pbr+2XjEuV+NbJLTqr77tA7l23CkRAVklen5MXyeCL7/wjDwD+wNPH5RwQKIM hOIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=NOoCVnODc6Tt3bCPDvs08wsnh22EnLVzEpEGLmvUSNs=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=tGiP4w/FwXm0aBjfuNWZue9gcxFd4UkPAhxwJ6irSZ3UKV7MrvAGKZjD74P3rVSOui XxffSXg2afUUaUtj7aN9bRfjoa6mRbUQZQw5z+BmIWy29Awo1jc682iJdTUf9u7gf8Mm wQK2eJ+xx+7AssW/feU4d1jEdvrEn8w08JNmb08bV7tCC2s3cKiT1nRDC9rdzO8JX9O/ bmH69s/NAj+/QdIeEZ+uz5YtUNy2hxgxO/DgKo/6dvr1lmF4xkFUfZGeVdZ/N45H0Iw/ fxsa5i105WtHyDYuAAkelgDc2CdMwW0ETsPQPh0ghBt70ecGIXbWUzsDWYqG205ljbCf 5gLQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=fNSKqeTd; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ho12-20020a1709070e8c00b00a55bbc83796si993666ejc.388.2024.04.22.07.27.21; Mon, 22 Apr 2024 07:27:21 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=fNSKqeTd; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BE78268D385; Mon, 22 Apr 2024 17:26:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AE26C68D32A for ; Mon, 22 Apr 2024 17:26:05 +0300 (EEST) Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-416a8ec0239so25063635e9.0 for ; Mon, 22 Apr 2024 07:26:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713795964; x=1714400764; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=7avWcIstWjoAfFNuEIrWVZvuRfk6gppMQ1X05ezhCoA=; b=fNSKqeTdFVSqXVB49PzwotDPg4UjQ3iRAjVmaTUzQABQ6yRB3CYypiorUYhu6qRk1W OJVYSQ54jTQus3QdQFLj8otDP3w0YiGgtQQsucPeRw9He+hTloGARfkhlEkyOXuwIGD8 0EltGD74My5fIiQ50KfNHeKkq1u3B48r7XY9dd/JY0vd6N7wqJmmR8m4CgzhyrWMzM/H azoYxZYqmUv7OtaqHEKOXB1Y5inqyFLs/9ndyvkivb6pbowwP4DO+sLw62DgdlGMa8ts 36zl5Z5P8zjd7j5AIvIJB7SMNre5/MvtESOsgNjNKvXYKmEz+BCbDuTPf1Wqj060W3PU fi6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713795964; x=1714400764; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7avWcIstWjoAfFNuEIrWVZvuRfk6gppMQ1X05ezhCoA=; b=JYHz4+IOltYLtTcz3IB9KebEoZp7XwXRkHhBgeZQ1IaJNqIdWWKCJtgJ1eUkYrFT2L eymr7XMqlmqvqmZtiAp1sU3J9H4uEArK9s44Sqh8znhGis/hhnyqItUEDMacv+24oq68 1iGT1QiQRjxejAyJ5mfpzqjPNttkpuCil8ocV8XwjufuQIke4w7RAcPyrfL5c/drA6eC K//kHnQ8cOmJtcRKBgwD9V9/4uriqFly0eMCVifP6ZWzYn/x79F8skUVi4cyKqSGO9RV uCJ0QytqN6nI6K5sLx0/SQrEEopxqmHEyn/GcFjf0nUC8T9Dtlj6rwnLeF/WQQpsgMdl fFRw== X-Gm-Message-State: AOJu0YyHcIChMVL4436homIo24FV6pEnOkHWFrYhD7JSAKVA3FCTqbKG 96cn/68fiu45kIGp0P37sTbUSElPVbU9d9HFLTld7y32Dphv9wCjjklBZQ== X-Received: by 2002:a05:600c:1c85:b0:418:7ec1:7bdb with SMTP id k5-20020a05600c1c8500b004187ec17bdbmr10754320wms.5.1713795963980; Mon, 22 Apr 2024 07:26:03 -0700 (PDT) Received: from localhost.localdomain (33bf3d9d.skybroadband.com. [51.191.61.157]) by smtp.gmail.com with ESMTPSA id d11-20020a05600c3acb00b0041aa14b4646sm67111wms.8.2024.04.22.07.26.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 07:26:03 -0700 (PDT) From: Derek Buitenhuis To: ffmpeg-devel@ffmpeg.org Date: Mon, 22 Apr 2024 15:25:44 +0100 Message-ID: <20240422142547.281064-7-derek.buitenhuis@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240422142547.281064-1-derek.buitenhuis@gmail.com> References: <20240422142547.281064-1-derek.buitenhuis@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 6/9] avformat/http: Add options to set the max number of connection retries X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jBBRbfLGKNoj Not every use case benefits from setting retries in terms of the backoff. Signed-off-by: Derek Buitenhuis --- 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; 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, \