[pbs-devel] [PATCH proxmox-backup 4/4] api: backup: wait for semaphore if one exists

Hannes Laimer h.laimer at proxmox.com
Thu Sep 4 16:37:35 CEST 2025


Backups will start in the order they started waiting, so
basically a FIFO queue.

Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
 src/api2/backup/mod.rs | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/api2/backup/mod.rs b/src/api2/backup/mod.rs
index ae61ff69..a52a736e 100644
--- a/src/api2/backup/mod.rs
+++ b/src/api2/backup/mod.rs
@@ -19,6 +19,8 @@ use proxmox_router::{
 };
 use proxmox_schema::*;
 use proxmox_sortable_macro::sortable;
+use proxmox_sys::error::SysError;
+use proxmox_sys::semaphore::PosixSemaphore;
 
 use pbs_api_types::{
     ArchiveType, Authid, BackupNamespace, BackupType, Operation, VerifyState,
@@ -226,6 +228,20 @@ fn upgrade_to_backup_protocol(
                     Some(ip) => format!(" from {ip}"),
                     None => "".into(),
                 };
+                let sem = match PosixSemaphore::open(&format!("/{}_concurrent", &store)) {
+                    Ok(_) if benchmark => None,
+                    Ok(sem) => Some(sem),
+                    Err(ref err) if err.not_found() => None,
+                    Err(err) => bail!("could not open semaphore: {err}"),
+                };
+
+                if let Some(ref sem) = sem {
+                    if let Ok(false) = sem.try_wait() {
+                        log::info!("max amount of concurrent tasks reached, waiting for one to finish...");
+                        sem.wait()?;
+                    }
+                }
+
                 env.log(format!(
                     "starting new {worker_type} on datastore '{store}'{origin}: {path:?}",
                 ));
@@ -295,7 +311,7 @@ fn upgrade_to_backup_protocol(
                     }
                 };
 
-                match (res, env.ensure_finished()) {
+                let r = match (res, env.ensure_finished()) {
                     (Ok(_), Ok(())) => {
                         env.log("backup finished successfully");
                         verify(env);
@@ -319,7 +335,11 @@ fn upgrade_to_backup_protocol(
                         proxmox_async::runtime::block_in_place(|| env.remove_backup())?;
                         Err(err)
                     }
-                }
+                };
+                if let Some(ref sem) = sem {
+                    let _ = sem.post();
+                };
+                r
             },
         )?;
 
-- 
2.47.2





More information about the pbs-devel mailing list