Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "proper background task handling" #113

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions src/extensions/api/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{sync::Arc, time::Duration};
use async_trait::async_trait;
use jsonrpsee::core::JsonValue;
use serde::Deserialize;
use tokio::{sync::watch, task::JoinHandle};
use tokio::sync::watch;

use crate::{
extension::Extension,
Expand All @@ -17,13 +17,6 @@ use crate::{
pub struct EthApi {
inner: BaseApi,
stale_timeout: Duration,
background_tasks: Vec<JoinHandle<()>>,
}

impl Drop for EthApi {
fn drop(&mut self) {
self.background_tasks.drain(..).for_each(|handle| handle.abort());
}
}

#[derive(Deserialize, Debug)]
Expand All @@ -47,10 +40,9 @@ impl EthApi {
let (head_tx, head_rx) = watch::channel::<Option<(JsonValue, u64)>>(None);
let (finalized_head_tx, finalized_head_rx) = watch::channel::<Option<(JsonValue, u64)>>(None);

let mut this = Self {
let this = Self {
inner: BaseApi::new(head_rx, finalized_head_rx),
stale_timeout,
background_tasks: Vec::new(),
};

this.start_background_task(client, head_tx, finalized_head_tx);
Expand All @@ -75,15 +67,15 @@ impl EthApi {
}

fn start_background_task(
&mut self,
&self,
client: Arc<Client>,
head_tx: watch::Sender<Option<(JsonValue, u64)>>,
finalized_head_tx: watch::Sender<Option<(JsonValue, u64)>>,
) {
let stale_timeout = self.stale_timeout;

let client2 = client.clone();
self.background_tasks.push(tokio::spawn(async move {
tokio::spawn(async move {
let mut interval = tokio::time::interval(stale_timeout);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

Expand Down Expand Up @@ -143,10 +135,10 @@ impl EthApi {
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
}));
});

let client = client.clone();
self.background_tasks.push(tokio::spawn(async move {
tokio::spawn(async move {
let client = client.clone();

loop {
Expand Down Expand Up @@ -196,6 +188,6 @@ impl EthApi {
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
}));
});
}
}
22 changes: 7 additions & 15 deletions src/extensions/api/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{sync::Arc, time::Duration};
use async_trait::async_trait;
use jsonrpsee::core::JsonValue;
use serde::Deserialize;
use tokio::{sync::watch, task::JoinHandle};
use tokio::sync::watch;

use crate::{
extension::Extension,
Expand All @@ -18,13 +18,6 @@ pub struct SubstrateApi {
client: Arc<Client>,
inner: BaseApi,
stale_timeout: Duration,
background_tasks: Vec<JoinHandle<()>>,
}

impl Drop for SubstrateApi {
fn drop(&mut self) {
self.background_tasks.drain(..).for_each(|handle| handle.abort());
}
}

#[derive(Deserialize, Debug)]
Expand All @@ -48,11 +41,10 @@ impl SubstrateApi {
let (head_tx, head_rx) = watch::channel::<Option<(JsonValue, u64)>>(None);
let (finalized_head_tx, finalized_head_rx) = watch::channel::<Option<(JsonValue, u64)>>(None);

let mut this = Self {
let this = Self {
client,
inner: BaseApi::new(head_rx, finalized_head_rx),
stale_timeout,
background_tasks: Vec::new(),
};

this.start_background_task(head_tx, finalized_head_tx);
Expand All @@ -69,14 +61,14 @@ impl SubstrateApi {
}

fn start_background_task(
&mut self,
&self,
head_tx: watch::Sender<Option<(JsonValue, u64)>>,
finalized_head_tx: watch::Sender<Option<(JsonValue, u64)>>,
) {
let client = self.client.clone();
let stale_timeout = self.stale_timeout;

self.background_tasks.push(tokio::spawn(async move {
tokio::spawn(async move {
let mut interval = tokio::time::interval(stale_timeout);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

Expand Down Expand Up @@ -129,11 +121,11 @@ impl SubstrateApi {
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
}));
});

let client = self.client.clone();

self.background_tasks.push(tokio::spawn(async move {
tokio::spawn(async move {
loop {
let run = async {
let mut sub = client
Expand Down Expand Up @@ -182,6 +174,6 @@ impl SubstrateApi {
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
}));
});
}
}
10 changes: 1 addition & 9 deletions src/extensions/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ const TRACER: utils::telemetry::Tracer = utils::telemetry::Tracer::new("client")
pub struct Client {
sender: tokio::sync::mpsc::Sender<Message>,
rotation_notify: Arc<Notify>,
background_task: tokio::task::JoinHandle<()>,
}

impl Drop for Client {
fn drop(&mut self) {
self.background_task.abort();
}
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -108,7 +101,7 @@ impl Client {
let rotation_notify = Arc::new(Notify::new());
let rotating = rotation_notify.clone();

let background_task = tokio::spawn(async move {
tokio::spawn(async move {
let tx = tx2;

let connect_backoff_counter = Arc::new(AtomicU32::new(0));
Expand Down Expand Up @@ -322,7 +315,6 @@ impl Client {
Ok(Self {
sender: tx,
rotation_notify,
background_task,
})
}

Expand Down
22 changes: 9 additions & 13 deletions src/middlewares/methods/inject_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod tests {
head_sink: Option<SubscriptionSink>,
}

async fn create_client() -> (ExecutionContext, Arc<SubstrateApi>) {
async fn create_client() -> (ExecutionContext, SubstrateApi) {
let mut builder = TestServerBuilder::new();

let head_rx =
Expand All @@ -186,14 +186,14 @@ mod tests {
block_hash_rx,
head_sink: None,
},
Arc::new(api),
api,
)
}

async fn create_inject_middleware(
inject_type: InjectType,
params: Vec<MethodParam>,
) -> (InjectParamsMiddleware, ExecutionContext, Arc<SubstrateApi>) {
) -> (InjectParamsMiddleware, ExecutionContext) {
let (mut context, api) = create_client().await;

let (_, head_sink) = context.head_rx.recv().await.unwrap();
Expand All @@ -209,17 +209,13 @@ mod tests {

context.head_sink = Some(head_sink);

(
InjectParamsMiddleware::new(api.clone(), inject_type, params),
context,
api,
)
(InjectParamsMiddleware::new(Arc::new(api), inject_type, params), context)
}

#[tokio::test]
async fn skip_inject_if_full_params() {
let params = vec![json!("0x1234"), json!("0x5678")];
let (middleware, _, _api) = create_inject_middleware(
let (middleware, _) = create_inject_middleware(
InjectType::BlockHashAt(1),
vec![
MethodParam {
Expand Down Expand Up @@ -256,7 +252,7 @@ mod tests {

#[tokio::test]
async fn inject_if_without_current_block_hash() {
let (middleware, _, _api) = create_inject_middleware(
let (middleware, _) = create_inject_middleware(
InjectType::BlockHashAt(1),
vec![
MethodParam {
Expand Down Expand Up @@ -293,7 +289,7 @@ mod tests {

#[tokio::test]
async fn inject_null_if_expected_optional_param() {
let (middleware, _, _api) = create_inject_middleware(
let (middleware, _) = create_inject_middleware(
InjectType::BlockHashAt(2),
vec![
MethodParam {
Expand Down Expand Up @@ -336,7 +332,7 @@ mod tests {

#[tokio::test]
async fn err_if_missing_param() {
let (middleware, _, _api) = create_inject_middleware(
let (middleware, _) = create_inject_middleware(
InjectType::BlockHashAt(2),
vec![
MethodParam {
Expand Down Expand Up @@ -383,7 +379,7 @@ mod tests {

#[tokio::test]
async fn inject_if_without_current_block_num() {
let (middleware, mut context, _api) = create_inject_middleware(
let (middleware, mut context) = create_inject_middleware(
InjectType::BlockNumberAt(1),
vec![
MethodParam {
Expand Down
Loading