Add custom timeouts to events

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

@ -10,6 +10,7 @@ use async_trait::async_trait;
use bromine::context::{PoolGuard, PooledContext}; use bromine::context::{PoolGuard, PooledContext};
use bromine::payload::BytePayload; use bromine::payload::BytePayload;
use bromine::prelude::*; use bromine::prelude::*;
use tokio::time::Duration;
pub struct FileApi { pub struct FileApi {
ctx: PooledContext, ctx: PooledContext,
@ -43,12 +44,12 @@ impl FileApi {
/// Returns all known files /// Returns all known files
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn all_files(&self) -> ApiResult<Vec<FileMetadataResponse>> { 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 /// Returns a file by identifier
pub async fn get_file(&self, id: FileIdentifier) -> ApiResult<FileMetadataResponse> { 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 /// Searches for a file by a list of tags
@ -64,6 +65,7 @@ impl FileApi {
filters, filters,
sort_expression, sort_expression,
}, },
Some(Duration::from_secs(20))
) )
.await .await
} }
@ -72,7 +74,7 @@ impl FileApi {
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn read_file(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> { pub async fn read_file(&self, id: FileIdentifier) -> ApiResult<Vec<u8>> {
let payload: BytePayload = self let payload: BytePayload = self
.emit_and_get("read_file", ReadFileRequest { id }) .emit_and_get("read_file", ReadFileRequest { id }, Some(Duration::from_secs(10)))
.await?; .await?;
Ok(payload.into_inner()) Ok(payload.into_inner())
@ -84,7 +86,7 @@ impl FileApi {
&self, &self,
id: FileIdentifier, id: FileIdentifier,
) -> ApiResult<Vec<ThumbnailMetadataResponse>> { ) -> 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 .await
} }
@ -104,6 +106,7 @@ impl FileApi {
min_size, min_size,
max_size, max_size,
}, },
Some(Duration::from_secs(2))
) )
.await?; .await?;
let (metadata, bytes) = payload.into_inner(); let (metadata, bytes) = payload.into_inner();
@ -118,7 +121,7 @@ impl FileApi {
file_id: FileIdentifier, file_id: FileIdentifier,
name: String, name: String,
) -> ApiResult<FileMetadataResponse> { ) -> 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 .await
} }
@ -135,7 +138,7 @@ impl FileApi {
BytePayload::new(bytes), 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 /// 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 async_trait::async_trait;
use bromine::ipc::stream_emitter::EmitMetadata; use bromine::ipc::stream_emitter::EmitMetadata;
use bromine::prelude::*; use bromine::prelude::*;
use std::time::Duration; use tokio::time::Duration;
#[async_trait] #[async_trait]
pub trait IPCApi { pub trait IPCApi {
@ -34,8 +34,13 @@ pub trait IPCApi {
&self, &self,
event_name: &str, event_name: &str,
data: T, data: T,
timeout: Option<Duration>,
) -> ApiResult<R> { ) -> 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?; let response = meta.await_reply(&self.ctx()).await?;
Ok(response.payload()?) Ok(response.payload()?)
@ -77,7 +82,7 @@ impl ApiClient {
) -> ApiResult<Self> { ) -> ApiResult<Self> {
let ctx = IPCBuilder::<L>::new() let ctx = IPCBuilder::<L>::new()
.address(address) .address(address)
.timeout(Duration::from_secs(30)) .timeout(Duration::from_secs(10))
.build_pooled_client(8) .build_pooled_client(8)
.await?; .await?;
let client = Self::new(ctx); let client = Self::new(ctx);

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

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

Loading…
Cancel
Save