fix: move events writer to individual thread (#669)

release-notes-05
Meng Zhang 2023-10-29 18:31:41 -07:00 committed by GitHub
parent c55e4481ba
commit 88d2617a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 25 deletions

View File

@ -1,29 +1,55 @@
use std::{ use std::{
fs, fs,
io::{BufWriter, Write}, io::{BufWriter, Write},
sync::Mutex, time::Duration,
}; };
use chrono::Utc; use chrono::Utc;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde::Serialize; use serde::Serialize;
use tokio::{
sync::mpsc::{unbounded_channel, UnboundedSender},
time::{self},
};
lazy_static! { lazy_static! {
static ref WRITER: Mutex<BufWriter<fs::File>> = { static ref WRITER: UnboundedSender<String> = {
let events_dir = crate::path::events_dir(); let (tx, mut rx) = unbounded_channel::<String>();
std::fs::create_dir_all(events_dir.as_path()).ok();
let now = Utc::now(); tokio::spawn(async move {
let fname = now.format("%Y-%m-%d.json"); let events_dir = crate::path::events_dir();
let file = fs::OpenOptions::new() std::fs::create_dir_all(events_dir.as_path()).ok();
.create(true)
.append(true)
.write(true)
.open(events_dir.join(fname.to_string()))
.ok()
.unwrap();
Mutex::new(BufWriter::new(file)) let now = Utc::now();
let fname = now.format("%Y-%m-%d.json");
let file = fs::OpenOptions::new()
.create(true)
.append(true)
.write(true)
.open(events_dir.join(fname.to_string()))
.ok()
.unwrap();
let mut writer = BufWriter::new(file);
let mut interval = time::interval(Duration::from_secs(5));
loop {
tokio::select! {
content = rx.recv() => {
if let Some(content) = content {
writeln!(&mut writer, "{}", content).unwrap();
} else {
break;
}
}
_ = interval.tick() => {
writer.flush().unwrap();
}
}
}
});
tx
}; };
} }
@ -75,18 +101,13 @@ struct Log<'a> {
impl Event<'_> { impl Event<'_> {
pub fn log(&self) { pub fn log(&self) {
let mut writer = WRITER.lock().unwrap(); let content = serdeconv::to_json_string(&Log {
ts: timestamp(),
serdeconv::to_json_writer( event: self,
&Log { })
ts: timestamp(),
event: self,
},
writer.by_ref(),
)
.unwrap(); .unwrap();
writeln!(writer).unwrap();
writer.flush().unwrap(); WRITER.send(content).unwrap();
} }
} }