refactor: convert actix web middleware to async await (#1338)

Refactored Box::pin to replace and_then() calls to use async move 

Closes 545
This commit is contained in:
Taddes 2022-06-15 13:48:32 -04:00 committed by GitHub
parent 75e5f273ab
commit f76b5fc675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 13 deletions

View File

@ -7,7 +7,7 @@ use actix_web::{
web::Data,
Error, HttpMessage,
};
use futures::future::{self, LocalBoxFuture, TryFutureExt};
use futures::future::{self, LocalBoxFuture};
use sentry::protocol::Event;
use std::task::Poll;
@ -85,7 +85,10 @@ where
.app_data::<Data<ServerState>>()
.map(|state| Metrics::from(state.get_ref()));
Box::pin(self.service.call(sreq).and_then(move |mut sresp| {
let fut = self.service.call(sreq);
Box::pin(async move {
let mut sresp = fut.await?;
// handed an actix_error::error::Error;
// Fetch out the tags (in case any have been added.) NOTE: request extensions
// are NOT automatically passed to responses. You need to check both.
@ -142,14 +145,14 @@ where
}
if !apie.is_reportable() {
trace!("Sentry: Not reporting error: {:?}", apie);
return future::ok(sresp);
return Ok(sresp);
}
report(&tags, event_from_error(apie));
}
}
}
future::ok(sresp)
}))
Ok(sresp)
})
}
}

View File

@ -1,4 +1,5 @@
use std::fmt::Display;
use std::task::{Context, Poll};
use actix_web::{
@ -7,7 +8,7 @@ use actix_web::{
Error,
};
use futures::future::{self, LocalBoxFuture, TryFutureExt};
use futures::future::{self, LocalBoxFuture};
use syncstorage_db_common::util::SyncTimestamp;
use crate::error::{ApiError, ApiErrorKind};
@ -38,13 +39,12 @@ where
}
let ts = SyncTimestamp::default().as_seconds();
Box::pin(self.service.call(sreq).and_then(move |mut resp| {
future::ready(
set_weave_timestamp(resp.headers_mut(), ts)
.map_err(Into::into)
.map(|_| resp),
)
}))
let fut = self.service.call(sreq);
Box::pin(async move {
let mut resp = fut.await?;
set_weave_timestamp(resp.headers_mut(), ts)?;
Ok(resp)
})
}
}