refactor: get rid of ctx1 variables using rebinding

This commit is contained in:
Alexander Krotov
2021-01-12 21:28:49 +03:00
committed by link2xt
parent 5e8e77dfb6
commit 3c387a3cb3
2 changed files with 86 additions and 80 deletions

View File

@@ -592,13 +592,14 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
t.set_config(Config::ConfiguredAddr, Some("alice@example.com")) t.set_config(Config::ConfiguredAddr, Some("alice@example.com"))
.await .await
.unwrap(); .unwrap();
let ctx = t.clone(); let thr0 = {
let ctx0 = ctx.clone(); let ctx = t.clone();
let thr0 = thread::spawn(move || async_std::task::block_on(SignedPublicKey::load_self(&ctx)))
thread::spawn(move || async_std::task::block_on(SignedPublicKey::load_self(&ctx0))); };
let ctx1 = ctx; let thr1 = {
let thr1 = let ctx = t.clone();
thread::spawn(move || async_std::task::block_on(SignedPublicKey::load_self(&ctx1))); thread::spawn(move || async_std::task::block_on(SignedPublicKey::load_self(&ctx)))
};
let res0 = thr0.join().unwrap(); let res0 = thr0.join().unwrap();
let res1 = thr1.join().unwrap(); let res1 = thr1.join().unwrap();
assert_eq!(res0.unwrap(), res1.unwrap()); assert_eq!(res0.unwrap(), res1.unwrap());
@@ -617,10 +618,9 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
let t = TestContext::new().await; let t = TestContext::new().await;
let ctx = Arc::new(t); let ctx = Arc::new(t);
let ctx1 = ctx.clone();
let nrows = || async { let nrows = || async {
ctx1.sql ctx.sql
.query_get_value::<u32>(&ctx1, "SELECT COUNT(*) FROM keypairs;", paramsv![]) .query_get_value::<u32>(&ctx, "SELECT COUNT(*) FROM keypairs;", paramsv![])
.await .await
.unwrap() .unwrap()
}; };

View File

@@ -52,45 +52,46 @@ async fn inbox_loop(ctx: Context, started: Sender<()>, inbox_handlers: ImapConne
shutdown_sender, shutdown_sender,
} = inbox_handlers; } = inbox_handlers;
let ctx1 = ctx.clone(); let fut = {
let fut = async move { let ctx = ctx.clone();
started.send(()).await; async move {
let ctx = ctx1; started.send(()).await;
// track number of continously executed jobs // track number of continously executed jobs
let mut jobs_loaded = 0; let mut jobs_loaded = 0;
let mut info = InterruptInfo::default(); let mut info = InterruptInfo::default();
loop { loop {
match job::load_next(&ctx, Thread::Imap, &info).await { match job::load_next(&ctx, Thread::Imap, &info).await {
Some(job) if jobs_loaded <= 20 => { Some(job) if jobs_loaded <= 20 => {
jobs_loaded += 1; jobs_loaded += 1;
job::perform_job(&ctx, job::Connection::Inbox(&mut connection), job).await; job::perform_job(&ctx, job::Connection::Inbox(&mut connection), job).await;
info = Default::default(); info = Default::default();
}
Some(job) => {
// Let the fetch run, but return back to the job afterwards.
jobs_loaded = 0;
if ctx.get_config_bool(Config::InboxWatch).await {
info!(ctx, "postponing imap-job {} to run fetch...", job);
fetch(&ctx, &mut connection).await;
} }
} Some(job) => {
None => { // Let the fetch run, but return back to the job afterwards.
jobs_loaded = 0; jobs_loaded = 0;
if ctx.get_config_bool(Config::InboxWatch).await {
// Expunge folder if needed, e.g. if some jobs have info!(ctx, "postponing imap-job {} to run fetch...", job);
// deleted messages on the server. fetch(&ctx, &mut connection).await;
if let Err(err) = connection.maybe_close_folder(&ctx).await { }
warn!(ctx, "failed to close folder: {:?}", err);
} }
None => {
jobs_loaded = 0;
maybe_add_time_based_warnings(&ctx).await; // Expunge folder if needed, e.g. if some jobs have
// deleted messages on the server.
if let Err(err) = connection.maybe_close_folder(&ctx).await {
warn!(ctx, "failed to close folder: {:?}", err);
}
info = if ctx.get_config_bool(Config::InboxWatch).await { maybe_add_time_based_warnings(&ctx).await;
fetch_idle(&ctx, &mut connection, Config::ConfiguredInboxFolder).await
} else { info = if ctx.get_config_bool(Config::InboxWatch).await {
connection.fake_idle(&ctx, None).await fetch_idle(&ctx, &mut connection, Config::ConfiguredInboxFolder).await
}; } else {
connection.fake_idle(&ctx, None).await
};
}
} }
} }
} }
@@ -179,14 +180,14 @@ async fn simple_imap_loop(
shutdown_sender, shutdown_sender,
} = inbox_handlers; } = inbox_handlers;
let ctx1 = ctx.clone(); let fut = {
let ctx = ctx.clone();
async move {
started.send(()).await;
let fut = async move { loop {
started.send(()).await; fetch_idle(&ctx, &mut connection, folder).await;
let ctx = ctx1; }
loop {
fetch_idle(&ctx, &mut connection, folder).await;
} }
}; };
@@ -211,24 +212,25 @@ async fn smtp_loop(ctx: Context, started: Sender<()>, smtp_handlers: SmtpConnect
idle_interrupt_receiver, idle_interrupt_receiver,
} = smtp_handlers; } = smtp_handlers;
let ctx1 = ctx.clone(); let fut = {
let fut = async move { let ctx = ctx.clone();
started.send(()).await; async move {
let ctx = ctx1; started.send(()).await;
let mut interrupt_info = Default::default(); let mut interrupt_info = Default::default();
loop { loop {
match job::load_next(&ctx, Thread::Smtp, &interrupt_info).await { match job::load_next(&ctx, Thread::Smtp, &interrupt_info).await {
Some(job) => { Some(job) => {
info!(ctx, "executing smtp job"); info!(ctx, "executing smtp job");
job::perform_job(&ctx, job::Connection::Smtp(&mut connection), job).await; job::perform_job(&ctx, job::Connection::Smtp(&mut connection), job).await;
interrupt_info = Default::default(); interrupt_info = Default::default();
} }
None => { None => {
// Fake Idle // Fake Idle
info!(ctx, "smtp fake idle - started"); info!(ctx, "smtp fake idle - started");
interrupt_info = idle_interrupt_receiver.recv().await.unwrap_or_default(); interrupt_info = idle_interrupt_receiver.recv().await.unwrap_or_default();
info!(ctx, "smtp fake idle - interrupted") info!(ctx, "smtp fake idle - interrupted")
}
} }
} }
} }
@@ -259,16 +261,18 @@ impl Scheduler {
let mut sentbox_handle = None; let mut sentbox_handle = None;
let (smtp_start_send, smtp_start_recv) = channel(1); let (smtp_start_send, smtp_start_recv) = channel(1);
let ctx1 = ctx.clone(); let inbox_handle = {
let inbox_handle = Some(task::spawn(async move { let ctx = ctx.clone();
inbox_loop(ctx1, inbox_start_send, inbox_handlers).await Some(task::spawn(async move {
})); inbox_loop(ctx, inbox_start_send, inbox_handlers).await
}))
};
if ctx.get_config_bool(Config::MvboxWatch).await { if ctx.get_config_bool(Config::MvboxWatch).await {
let ctx1 = ctx.clone(); let ctx = ctx.clone();
mvbox_handle = Some(task::spawn(async move { mvbox_handle = Some(task::spawn(async move {
simple_imap_loop( simple_imap_loop(
ctx1, ctx,
mvbox_start_send, mvbox_start_send,
mvbox_handlers, mvbox_handlers,
Config::ConfiguredMvboxFolder, Config::ConfiguredMvboxFolder,
@@ -280,10 +284,10 @@ impl Scheduler {
} }
if ctx.get_config_bool(Config::SentboxWatch).await { if ctx.get_config_bool(Config::SentboxWatch).await {
let ctx1 = ctx.clone(); let ctx = ctx.clone();
sentbox_handle = Some(task::spawn(async move { sentbox_handle = Some(task::spawn(async move {
simple_imap_loop( simple_imap_loop(
ctx1, ctx,
sentbox_start_send, sentbox_start_send,
sentbox_handlers, sentbox_handlers,
Config::ConfiguredSentboxFolder, Config::ConfiguredSentboxFolder,
@@ -294,10 +298,12 @@ impl Scheduler {
sentbox_start_send.send(()).await; sentbox_start_send.send(()).await;
} }
let ctx1 = ctx.clone(); let smtp_handle = {
let smtp_handle = Some(task::spawn(async move { let ctx = ctx.clone();
smtp_loop(ctx1, smtp_start_send, smtp_handlers).await Some(task::spawn(async move {
})); smtp_loop(ctx, smtp_start_send, smtp_handlers).await
}))
};
*self = Scheduler::Running { *self = Scheduler::Running {
inbox, inbox,