Add custom timeouts to events

Signed-off-by: Trivernis <trivernis@protonmail.com>
pull/4/head
Trivernis 2 years ago
parent 445df0962f
commit a723ebab95

@ -10,6 +10,7 @@ use async_trait::async_trait;
use bromine::context::{PoolGuard, PooledContext};
use bromine::payload::BytePayload;
use bromine::prelude::*;
use tokio::time::Duration;
pub struct FileApi {
ctx: PooledContext,
@ -43,12 +44,12 @@ impl FileApi {
/// Returns all known files
#[tracing::instrument(level = "debug", skip(self))]
pub async fn all_files(&self) -> ApiResult<Vec<FileMetadataResponse>> {
self.emit_and_get("all_files", ()).await
self.emit_and_get("all_files", (), Some(Duration::from_secs(30))).await
}
/// Returns a file by identifier
pub async fn get_file(&self, id: FileIdentifier) -> ApiResult<FileMetadataResponse> {
self.emit_and_get("get_file", id).await
self.emit_and_get("get_file", id, Some(Duration::from_secs(2))).await
}
/// Searches for a file by a list of tags
@ -64,6 +65,7 @@ impl FileApi {
filters,
sort_expression,
},
Some(Duration::from_secs(20))
)
.await
}
@ -72,7 +74,7 @@ impl FileApi {
#[tracing::instrument(level = "debug", skip(self))]
pub async fn read_file(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> {
let payload: BytePayload = self
.emit_and_get("read_file", ReadFileRequest { id })
.emit_and_get("read_file", ReadFileRequest { id }, Some(Duration::from_secs(10)))
.await?;
Ok(payload.into_inner())
@ -84,7 +86,7 @@ impl FileApi {
&self,
id: FileIdentifier,
) -> ApiResult<Vec<ThumbnailMetadataResponse>> {
self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id })
self.emit_and_get("get_thumbnails", GetFileThumbnailsRequest { id }, Some(Duration::from_secs(2)))
.await
}
@ -104,6 +106,7 @@ impl FileApi {
min_size,
max_size,
},
Some(Duration::from_secs(2))
)
.await?;
let (metadata, bytes) = payload.into_inner();
@ -118,7 +121,7 @@ impl FileApi {
file_id: FileIdentifier,
name: String,
) -> ApiResult<FileMetadataResponse> {
self.emit_and_get("update_file_name", UpdateFileNameRequest { file_id, name })
self.emit_and_get("update_file_name", UpdateFileNameRequest { file_id, name }, Some(Duration::from_secs(1)))
.await
}
@ -135,7 +138,7 @@ impl FileApi {
BytePayload::new(bytes),
);
self.emit_and_get("add_file", payload).await
self.emit_and_get("add_file", payload, Some(Duration::from_secs(5))).await
}
/// Deletes all thumbnails of a file to regenerate them when requested

@ -12,7 +12,7 @@ use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse};
use async_trait::async_trait;
use bromine::ipc::stream_emitter::EmitMetadata;
use bromine::prelude::*;
use std::time::Duration;
use tokio::time::Duration;
#[async_trait]
pub trait IPCApi {
@ -34,8 +34,13 @@ pub trait IPCApi {
&self,
event_name: &str,
data: T,
timeout: Option<Duration>,
) -> ApiResult<R> {
let meta = self.emit(event_name, data).await?;
let mut meta = self.emit(event_name, data).await?;
if let Some(timeout) = timeout {
meta = meta.with_timeout(timeout);
}
let response = meta.await_reply(&self.ctx()).await?;
Ok(response.payload()?)
@ -77,7 +82,7 @@ impl ApiClient {
) -> ApiResult<Self> {
let ctx = IPCBuilder::<L>::new()
.address(address)
.timeout(Duration::from_secs(30))
.timeout(Duration::from_secs(10))
.build_pooled_client(8)
.await?;
let client = Self::new(ctx);

@ -2,6 +2,7 @@ use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi;
use crate::types::repo::FrontendState;
use bromine::prelude::*;
use tokio::time::Duration;
#[derive(Clone)]
pub struct RepoApi {
@ -26,7 +27,7 @@ impl RepoApi {
/// Returns the state of the frontend that is stored in the repo
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_frontend_state(&self) -> ApiResult<FrontendState> {
let state = self.emit_and_get("frontend_state", ()).await?;
let state = self.emit_and_get("frontend_state", (), Some(Duration::from_secs(5))).await?;
Ok(state)
}

@ -1,3 +1,4 @@
use std::time::Duration;
use crate::client_api::error::ApiResult;
use crate::client_api::IPCApi;
use crate::types::files::{GetFileTagsRequest, GetFilesTagsRequest};
@ -38,27 +39,27 @@ impl TagApi {
/// Returns a list of all tags stored in the repo
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_all_tags(&self) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get("all_tags", ()).await
self.emit_and_get("all_tags", (), Some(Duration::from_secs(2))).await
}
/// Returns a list of all tags for a file
#[tracing::instrument(level = "debug", skip(self))]
pub async fn get_tags_for_file(&self, id: FileIdentifier) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get("tags_for_file", GetFileTagsRequest { id })
self.emit_and_get("tags_for_file", GetFileTagsRequest { id }, Some(Duration::from_secs(1)))
.await
}
/// Returns a list of all tags that are assigned to the list of files
#[tracing::instrument(level = "debug", skip_all)]
pub async fn get_tags_for_files(&self, hashes: Vec<String>) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes })
self.emit_and_get("tags_for_files", GetFilesTagsRequest { hashes }, Some(Duration::from_secs(10)))
.await
}
/// Creates a new tag and returns the created tag object
#[tracing::instrument(level = "debug", skip(self))]
pub async fn create_tags(&self, tags: Vec<String>) -> ApiResult<Vec<TagResponse>> {
self.emit_and_get("create_tags", tags).await
self.emit_and_get("create_tags", tags, Some(Duration::from_secs(10))).await
}
/// Changes the tags of a file
@ -76,6 +77,7 @@ impl TagApi {
added_tags,
removed_tags,
},
Some(Duration::from_secs(10))
)
.await
}

Loading…
Cancel
Save