From 6bc7de398f19b5a99bc9ce509033483ffa71f15a Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 31 Jan 2025 12:12:24 +0000 Subject: [PATCH] mirrors: Evaluate the status code With some errors, there is no point at all to keep trying. Signed-off-by: Michael Tremer --- src/pakfire/mirror.c | 31 ++++++++++++++++++++++++++++--- src/pakfire/mirror.h | 3 ++- src/pakfire/xfer.c | 5 ++++- src/pakfire/xfer.h | 10 +++++----- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/pakfire/mirror.c b/src/pakfire/mirror.c index 006ae321d..7e2f9263e 100644 --- a/src/pakfire/mirror.c +++ b/src/pakfire/mirror.c @@ -118,7 +118,32 @@ void pakfire_mirror_mark_as_broken(struct pakfire_mirror* mirror) { mirror->is_broken = 1; } -void pakfire_mirror_xfer_failed(struct pakfire_mirror* mirror) { - if (!mirror->retries_left--) - pakfire_mirror_mark_as_broken(mirror); +/* + This function is called when a transfer has failed and will automatically + disable the mirror. +*/ +int pakfire_mirror_xfer_failed(struct pakfire_mirror* self, pakfire_xfer_error_code_t code) { + switch (code) { + // We should never be called with this + case PAKFIRE_XFER_OK: + return -EINVAL; + + /* + The mirror seems to have responded with something + like 502 or 503, so there is no point in tryin again. + */ + case PAKFIRE_XFER_TRANSPORT_ERROR: + pakfire_mirror_mark_as_broken(self); + break; + + /* + For all other errors, we will just count down. + */ + default: + if (!self->retries_left--) + pakfire_mirror_mark_as_broken(self); + break; + } + + return 0; } diff --git a/src/pakfire/mirror.h b/src/pakfire/mirror.h index ab2d4615d..abc44318f 100644 --- a/src/pakfire/mirror.h +++ b/src/pakfire/mirror.h @@ -24,6 +24,7 @@ struct pakfire_mirror; #include +#include int pakfire_mirror_create(struct pakfire_mirror** mirror, struct pakfire_ctx* ctx, const char* url); @@ -38,6 +39,6 @@ const char* pakfire_mirror_get_url(struct pakfire_mirror* mirror); int pakfire_mirror_is_broken(struct pakfire_mirror* mirror); void pakfire_mirror_mark_as_broken(struct pakfire_mirror* mirror); -void pakfire_mirror_xfer_failed(struct pakfire_mirror* mirror); +int pakfire_mirror_xfer_failed(struct pakfire_mirror* self, pakfire_xfer_error_code_t code); #endif /* PAKFIRE_MIRROR_H */ diff --git a/src/pakfire/xfer.c b/src/pakfire/xfer.c index cf2911648..f2582c68e 100644 --- a/src/pakfire/xfer.c +++ b/src/pakfire/xfer.c @@ -972,7 +972,10 @@ static int pakfire_xfer_fail(struct pakfire_xfer* xfer, int code) { // Did we use a mirror? if (xfer->mirror) { - pakfire_mirror_xfer_failed(xfer->mirror); + // Tell the mirror about the failure + r = pakfire_mirror_xfer_failed(xfer->mirror, code); + if (r < 0) + return r; // Select the next mirror r = pakfire_xfer_next_mirror(xfer); diff --git a/src/pakfire/xfer.h b/src/pakfire/xfer.h index 1b0bf86fc..a1746a6e9 100644 --- a/src/pakfire/xfer.h +++ b/src/pakfire/xfer.h @@ -29,11 +29,6 @@ struct pakfire_xfer; -#include -#include -#include -#include - typedef enum pakfire_xfer_error_code { PAKFIRE_XFER_OK = 0, @@ -71,6 +66,11 @@ typedef enum pakfire_xfer_error_code { PAKFIRE_XFER_DIGEST_MISMATCH, } pakfire_xfer_error_code_t; +#include +#include +#include +#include + typedef enum pakfire_xfer_flags { PAKFIRE_XFER_NO_PROGRESS = (1 << 0), } pakfire_xfer_flags_t; -- 2.39.5