Skip to content

Commit 96a610b

Browse files
committed
fix: refactor Logger initialization and improve writer pinning
1 parent e637a06 commit 96a610b

3 files changed

Lines changed: 19 additions & 29 deletions

File tree

crates/http-service/src/executor/http.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ mod tests {
249249
type BackendConnector = FastEdgeConnector;
250250

251251
fn make_logger(&self, _app_name: SmolStr, _wrk: &App) -> Logger {
252-
Logger::new()
252+
Logger::default()
253253
}
254254

255255
fn backend(&self) -> Backend<FastEdgeConnector> {

crates/runtime/src/logger.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ impl AsyncWrite for MultiWriter {
6666

6767
// First writer determines how many bytes are accepted.
6868
if this.write_current == 0 {
69-
// SAFETY: writers are heap-allocated (Box) and won't move.
70-
match unsafe { Pin::new_unchecked(&mut *this.writers[0]) }.poll_write(cx, buf) {
69+
match pin_writer(&mut *this.writers[0]).poll_write(cx, buf) {
7170
Poll::Ready(Ok(n)) => {
7271
this.write_n = n;
7372
this.write_current = 1;
@@ -85,8 +84,7 @@ impl AsyncWrite for MultiWriter {
8584
while this.write_current < this.writers.len() {
8685
let idx = this.write_current;
8786
let n = this.write_n;
88-
// SAFETY: writers are heap-allocated (Box) and won't move.
89-
match unsafe { Pin::new_unchecked(&mut *this.writers[idx]) }.poll_write(cx, &buf[..n]) {
87+
match pin_writer(&mut *this.writers[idx]).poll_write(cx, &buf[..n]) {
9088
Poll::Ready(Ok(_)) => this.write_current += 1,
9189
Poll::Ready(Err(e)) => {
9290
tracing::warn!(cause=?e, "MultiWriter: appender {idx} write error");
@@ -104,19 +102,13 @@ impl AsyncWrite for MultiWriter {
104102
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
105103
let this = self.get_mut();
106104

107-
if this.flush_time.is_none() {
108-
let time = Utc::now();
109-
this.flush_time = Some(time);
110-
}
105+
let time = *this.flush_time.get_or_insert_with(Utc::now);
106+
111107
while this.flush_current < this.writers.len() {
112108
let idx = this.flush_current;
113-
// SAFETY: writers are heap-allocated (Box) and won't move.
114-
115-
if let Some(time) = this.flush_time.clone() {
116-
unsafe { Pin::new_unchecked(&mut *this.writers[idx]) }.flush_event_datetime(time);
117-
}
109+
pin_writer(&mut *this.writers[idx]).flush_event_datetime(time);
118110

119-
match unsafe { Pin::new_unchecked(&mut *this.writers[idx]) }.poll_flush(cx) {
111+
match pin_writer(&mut *this.writers[idx]).poll_flush(cx) {
120112
Poll::Ready(_) => this.flush_current += 1,
121113
Poll::Pending => return Poll::Pending,
122114
}
@@ -135,8 +127,7 @@ impl AsyncWrite for MultiWriter {
135127

136128
while this.shutdown_current < this.writers.len() {
137129
let idx = this.shutdown_current;
138-
// SAFETY: writers are heap-allocated (Box) and won't move.
139-
match unsafe { Pin::new_unchecked(&mut *this.writers[idx]) }.poll_shutdown(cx) {
130+
match pin_writer(&mut *this.writers[idx]).poll_shutdown(cx) {
140131
Poll::Ready(_) => this.shutdown_current += 1,
141132
Poll::Pending => return Poll::Pending,
142133
}
@@ -147,6 +138,11 @@ impl AsyncWrite for MultiWriter {
147138
}
148139
}
149140

141+
fn pin_writer(w: &mut dyn Appender) -> Pin<&mut dyn Appender> {
142+
// SAFETY: Box contents are heap-allocated and will not move.
143+
unsafe { Pin::new_unchecked(w) }
144+
}
145+
150146
impl IsTerminal for Logger {
151147
fn is_terminal(&self) -> bool {
152148
false
@@ -165,13 +161,6 @@ impl StdoutStream for Logger {
165161
}
166162

167163
impl Logger {
168-
pub fn new() -> Self {
169-
Self {
170-
properties: Default::default(),
171-
appenders: vec![],
172-
}
173-
}
174-
175164
/// Builder-style method to attach an additional appender.
176165
pub fn with_appender<S: AppenderBuilder + Sync + Send + 'static>(mut self, sink: S) -> Self {
177166
self.appenders.push(Arc::new(sink));
@@ -186,12 +175,13 @@ impl Logger {
186175
pub async fn write_msg(&self, msg: String) {
187176
let bytes = msg.as_bytes();
188177
for appender in &self.appenders {
189-
if let Err(error) = Box::into_pin(appender.build(self.properties.clone()))
190-
.write_all(bytes)
191-
.await
192-
{
178+
let mut pin = Box::into_pin(appender.build(self.properties.clone()));
179+
if let Err(error) = pin.write_all(bytes).await {
193180
tracing::warn!(cause=?error, "write_msg");
194181
}
182+
if let Err(error) = pin.flush().await {
183+
tracing::warn!(cause=?error, "write_msg flush");
184+
}
195185
}
196186
}
197187
}

src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl PreCompiledLoader<u64> for Context {
5151
impl ContextT for Context {
5252
type BackendConnector = HttpsConnector<HttpConnector>;
5353
fn make_logger(&self, _app_name: SmolStr, _wrk: &App) -> Logger {
54-
let logger = Logger::new();
54+
let logger = Logger::default();
5555
logger.with_appender(Console::default())
5656
}
5757

0 commit comments

Comments
 (0)