Skip to content

Commit

Permalink
feat(services/persy): change blocking_x in async_x call to tokio::tas…
Browse files Browse the repository at this point in the history
…k::blocking_spawn (#3221)

* feat(services/persy): change blocking_x in async_x call to tokio::task::blocking_spawn

Signed-off-by: Manjusaka <[email protected]>

* Update code

Signed-off-by: Manjusaka <[email protected]>

* Update code

Signed-off-by: Manjusaka <[email protected]>

---------

Signed-off-by: Manjusaka <[email protected]>
  • Loading branch information
Zheaoli authored Oct 13, 2023
1 parent 7f524a9 commit 3836074
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions core/src/services/persy/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ use std::str;

use async_trait::async_trait;
use persy;
use tokio::task;

use crate::raw::adapters::kv;
use crate::raw::*;
use crate::Builder;
use crate::Error;
use crate::ErrorKind;
Expand Down Expand Up @@ -175,7 +177,12 @@ impl kv::Adapter for Adapter {
}

async fn get(&self, path: &str) -> Result<Option<Vec<u8>>> {
self.blocking_get(path)
let cloned_self = self.clone();
let cloned_path = path.to_string();
task::spawn_blocking(move || cloned_self.blocking_get(cloned_path.as_str()))
.await
.map_err(new_task_join_error)
.and_then(|inner_result| inner_result)
}

fn blocking_get(&self, path: &str) -> Result<Option<Vec<u8>>> {
Expand All @@ -192,7 +199,14 @@ impl kv::Adapter for Adapter {
}

async fn set(&self, path: &str, value: &[u8]) -> Result<()> {
self.blocking_set(path, value)
let cloned_path = path.to_string();
let cloned_value = value.to_vec();
let cloned_self = self.clone();

task::spawn_blocking(move || cloned_self.blocking_set(cloned_path.as_str(), &cloned_value))
.await
.map_err(new_task_join_error)
.and_then(|inner_result| inner_result)
}

fn blocking_set(&self, path: &str, value: &[u8]) -> Result<()> {
Expand All @@ -208,7 +222,13 @@ impl kv::Adapter for Adapter {
}

async fn delete(&self, path: &str) -> Result<()> {
self.blocking_delete(path)
let cloned_path = path.to_string();
let cloned_self = self.clone();

task::spawn_blocking(move || cloned_self.blocking_delete(cloned_path.as_str()))
.await
.map_err(new_task_join_error)
.and_then(|inner_result| inner_result)
}

fn blocking_delete(&self, path: &str) -> Result<()> {
Expand Down

0 comments on commit 3836074

Please sign in to comment.