use RateLimitConfig for HttpClient and pull

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer
2021-11-22 06:26:55 +01:00
parent 6eb756bcab
commit 2d5287fbbc
6 changed files with 77 additions and 42 deletions

View File

@ -12,7 +12,7 @@ use pbs_client::{HttpClient, HttpClientOptions};
use pbs_api_types::{
REMOTE_ID_SCHEMA, REMOTE_PASSWORD_SCHEMA, Remote, RemoteConfig, RemoteConfigUpdater,
Authid, PROXMOX_CONFIG_DIGEST_SCHEMA, DATASTORE_SCHEMA, GroupListItem,
DataStoreListItem, SyncJobConfig, PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY,
DataStoreListItem, RateLimitConfig, SyncJobConfig, PRIV_REMOTE_AUDIT, PRIV_REMOTE_MODIFY,
};
use pbs_config::sync;
@ -280,8 +280,15 @@ pub fn delete_remote(name: String, digest: Option<String>) -> Result<(), Error>
}
/// Helper to get client for remote.cfg entry
pub async fn remote_client(remote: &Remote) -> Result<HttpClient, Error> {
let options = HttpClientOptions::new_non_interactive(remote.password.clone(), remote.config.fingerprint.clone());
pub async fn remote_client(
remote: &Remote,
limit: Option<RateLimitConfig>,
) -> Result<HttpClient, Error> {
let mut options = HttpClientOptions::new_non_interactive(remote.password.clone(), remote.config.fingerprint.clone());
if let Some(limit) = limit {
options = options.rate_limit(limit);
}
let client = HttpClient::new(
&remote.config.host,
@ -325,7 +332,7 @@ pub async fn scan_remote_datastores(name: String) -> Result<Vec<DataStoreListIte
api_err)
};
let client = remote_client(&remote)
let client = remote_client(&remote, None)
.await
.map_err(map_remote_err)?;
let api_res = client
@ -375,7 +382,7 @@ pub async fn scan_remote_groups(name: String, store: String) -> Result<Vec<Group
api_err)
};
let client = remote_client(&remote)
let client = remote_client(&remote, None)
.await
.map_err(map_remote_err)?;
let api_res = client

View File

@ -9,7 +9,7 @@ use proxmox_router::{ApiMethod, Router, RpcEnvironment, Permission};
use proxmox_sys::task_log;
use pbs_api_types::{
Authid, SyncJobConfig, GroupFilter, GROUP_FILTER_LIST_SCHEMA,
Authid, SyncJobConfig, GroupFilter, RateLimitConfig, GROUP_FILTER_LIST_SCHEMA,
DATASTORE_SCHEMA, REMOTE_ID_SCHEMA, REMOVE_VANISHED_BACKUPS_SCHEMA,
PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_PRUNE, PRIV_REMOTE_READ,
};
@ -51,6 +51,7 @@ impl TryFrom<&SyncJobConfig> for PullParameters {
sync_job.owner.as_ref().unwrap_or_else(|| Authid::root_auth_id()).clone(),
sync_job.remove_vanished,
sync_job.group_filter.clone(),
sync_job.limit.clone(),
)
}
}
@ -156,6 +157,10 @@ pub fn do_sync_job(
schema: GROUP_FILTER_LIST_SCHEMA,
optional: true,
},
limit: {
type: RateLimitConfig,
flatten: true,
}
},
},
access: {
@ -174,6 +179,7 @@ async fn pull (
remote_store: String,
remove_vanished: Option<bool>,
group_filter: Option<Vec<GroupFilter>>,
limit: RateLimitConfig,
_info: &ApiMethod,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<String, Error> {
@ -190,6 +196,7 @@ async fn pull (
auth_id.clone(),
remove_vanished,
group_filter,
limit,
)?;
let client = pull_params.client().await?;

View File

@ -14,7 +14,10 @@ use http::StatusCode;
use proxmox_router::HttpError;
use proxmox_sys::task_log;
use pbs_api_types::{Authid, GroupFilter, GroupListItem, Remote, SnapshotListItem};
use pbs_api_types::{
Authid, GroupFilter, GroupListItem, RateLimitConfig, Remote,
SnapshotListItem,
};
use pbs_datastore::{BackupDir, BackupInfo, BackupGroup, DataStore, StoreProgress};
use pbs_datastore::data_blob::DataBlob;
@ -41,6 +44,7 @@ pub struct PullParameters {
owner: Authid,
remove_vanished: bool,
group_filter: Option<Vec<GroupFilter>>,
limit: RateLimitConfig,
}
impl PullParameters {
@ -51,6 +55,7 @@ impl PullParameters {
owner: Authid,
remove_vanished: Option<bool>,
group_filter: Option<Vec<GroupFilter>>,
limit: RateLimitConfig,
) -> Result<Self, Error> {
let store = DataStore::lookup_datastore(store)?;
@ -66,11 +71,11 @@ impl PullParameters {
remote_store.to_string(),
);
Ok(Self { remote, source, store, owner, remove_vanished, group_filter })
Ok(Self { remote, source, store, owner, remove_vanished, group_filter, limit })
}
pub async fn client(&self) -> Result<HttpClient, Error> {
crate::api2::config::remote::remote_client(&self.remote).await
crate::api2::config::remote::remote_client(&self.remote, Some(self.limit.clone())).await
}
}