Skip to content

Commit bb8487e

Browse files
authored
Set max thread priority for local-sync (#104)
Set max thread priority for local-sync. localsync thread is important and should run at exact intervals. To ensure this its priority is set to max. Priority setting if fails is ignored for now which means if thread_priority can't set it to max then all can be done is log it. Scheduling behaviour is dependant on the underlying OS. It is user's responsiblity to make sure that program can set thread/process priority. For example on linux you'd have to allow `cpu cpuset` cgroup for current user. Fixes #103
1 parent e70534b commit bb8487e

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ serde_json = "^1.0.8"
4040
structopt = { version = "0.3.25" }
4141
sysinfo = "0.20.5"
4242
thiserror = "1"
43+
thread-priority = "0.9.2"
4344
tokio-stream = "0.1.8"
4445
tokio = { version = "1.13.1", default-features = false, features=["sync", "macros"] }
4546
clokwerk = "0.4.0-rc1"

server/src/main.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use clokwerk::{AsyncScheduler, Scheduler, TimeUnits};
2727
use filetime::FileTime;
2828
use log::warn;
2929
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
30+
use thread_priority::{ThreadBuilder, ThreadPriority};
3031

3132
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
3233

@@ -211,35 +212,43 @@ fn run_local_sync() -> (JoinHandle<()>, oneshot::Receiver<()>, oneshot::Sender<(
211212
let (outbox_tx, outbox_rx) = oneshot::channel::<()>();
212213
let (inbox_tx, inbox_rx) = oneshot::channel::<()>();
213214
let mut inbox_rx = AssertUnwindSafe(inbox_rx);
214-
let handle = thread::spawn(move || {
215-
let res = catch_unwind(move || {
216-
let mut scheduler = Scheduler::new();
217-
scheduler
218-
.every((storage::LOCAL_SYNC_INTERVAL as u32).seconds())
219-
.run(move || {
220-
if let Err(e) = S3::new().local_sync() {
221-
warn!("failed to sync local data. {:?}", e);
222-
}
223-
});
224-
225-
loop {
226-
thread::sleep(Duration::from_millis(50));
227-
scheduler.run_pending();
228-
match AssertUnwindSafe(|| inbox_rx.try_recv())() {
229-
Ok(_) => break,
230-
Err(TryRecvError::Empty) => continue,
231-
Err(TryRecvError::Closed) => {
232-
// should be unreachable but breaking anyways
233-
break;
215+
216+
let handle = ThreadBuilder::default()
217+
.name("local-sync")
218+
.priority(ThreadPriority::Max)
219+
.spawn(move |priority_result| {
220+
if priority_result.is_err() {
221+
log::warn!("Max priority cannot be set for sync thread. Make sure that user/program is allowed to set thread priority.")
222+
}
223+
let res = catch_unwind(move || {
224+
let mut scheduler = Scheduler::new();
225+
scheduler
226+
.every((storage::LOCAL_SYNC_INTERVAL as u32).seconds())
227+
.run(move || {
228+
if let Err(e) = S3::new().local_sync() {
229+
warn!("failed to sync local data. {:?}", e);
230+
}
231+
});
232+
233+
loop {
234+
thread::sleep(Duration::from_millis(50));
235+
scheduler.run_pending();
236+
match AssertUnwindSafe(|| inbox_rx.try_recv())() {
237+
Ok(_) => break,
238+
Err(TryRecvError::Empty) => continue,
239+
Err(TryRecvError::Closed) => {
240+
// should be unreachable but breaking anyways
241+
break;
242+
}
234243
}
235244
}
236-
}
237-
});
245+
});
238246

239-
if res.is_err() {
240-
outbox_tx.send(()).unwrap();
241-
}
242-
});
247+
if res.is_err() {
248+
outbox_tx.send(()).unwrap();
249+
}
250+
})
251+
.unwrap();
243252

244253
(handle, outbox_rx, inbox_tx)
245254
}

0 commit comments

Comments
 (0)