Skip to content

Commit

Permalink
feat: remove box self from services, remove syncwrapper from axum ser…
Browse files Browse the repository at this point in the history
…vice (#677)

* feat: remove box self from all services

* feat: remove syncwrapper from axum integration
  • Loading branch information
oddgrd authored Mar 3, 2023
1 parent 91a9fdd commit 69b82e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ salvo = { version = "0.37.5", optional = true }
serde_json = { workspace = true }
serenity = { version = "0.11.5", default-features = false, features = ["client", "gateway", "rustls_backend", "model"], optional = true }
poise = { version = "0.5.2", optional = true }
sync_wrapper = { version = "0.1.1", optional = true }
thiserror = { workspace = true }
thruster = { version = "1.3.0", optional = true }
tide = { version = "0.16.0", optional = true }
Expand Down Expand Up @@ -70,7 +69,7 @@ codegen = ["shuttle-codegen/frameworks"]
loader = ["cargo", "libloading"]

web-actix-web = ["actix-web", "num_cpus"]
web-axum = ["axum", "sync_wrapper"]
web-axum = ["axum"]
web-rocket = ["rocket"]
web-thruster = ["thruster"]
web-tide = ["tide", "async-std"]
Expand Down
32 changes: 15 additions & 17 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ impl<T> Service for T
where
T: poem::Endpoint + Sync + Send + 'static,
{
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
poem::Server::new(poem::listener::TcpListener::bind(addr))
.run(self)
.await
Expand All @@ -529,7 +529,7 @@ where
T: Send + Sync + Clone + 'static + warp::Filter,
T::Extract: warp::reply::Reply,
{
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
warp::serve(*self).run(addr).await;
Ok(())
}
Expand All @@ -540,26 +540,27 @@ pub type ShuttleWarp<T> = Result<warp::filters::BoxedFilter<T>, Error>;

#[cfg(feature = "web-axum")]
#[async_trait]
impl Service for sync_wrapper::SyncWrapper<axum::Router> {
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
let router = self.into_inner();

impl Service for axum::Router {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
axum::Server::bind(&addr)
.serve(router.into_make_service())
.serve(self.into_make_service())
.await
.map_err(error::CustomError::new)?;

Ok(())
}
}

#[cfg(feature = "web-axum")]
pub type ShuttleAxum = Result<axum::Router, Error>;

#[cfg(feature = "web-actix-web")]
#[async_trait]
impl<F> Service for F
where
F: FnOnce(&mut actix_web::web::ServiceConfig) + Sync + Send + Clone + 'static,
{
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), Error> {
// Start a worker for each cpu, but no more than 4.
let worker_count = num_cpus::get().max(4);

Expand All @@ -575,13 +576,10 @@ where
#[cfg(feature = "web-actix-web")]
pub type ShuttleActixWeb<F> = Result<F, Error>;

#[cfg(feature = "web-axum")]
pub type ShuttleAxum = Result<sync_wrapper::SyncWrapper<axum::Router>, Error>;

#[cfg(feature = "web-salvo")]
#[async_trait]
impl Service for salvo::Router {
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
salvo::Server::new(salvo::listener::TcpListener::bind(addr))
.serve(self)
.await;
Expand All @@ -599,7 +597,7 @@ impl<T> Service for T
where
T: thruster::ThrusterServer + Sync + Send + 'static,
{
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
self.build(&addr.ip().to_string(), addr.port()).await;

Ok(())
Expand All @@ -615,7 +613,7 @@ impl<T> Service for tide::Server<T>
where
T: Clone + Send + Sync + 'static,
{
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
self.listen(addr).await.map_err(error::CustomError::new)?;

Ok(())
Expand All @@ -637,7 +635,7 @@ where
T::Error: std::error::Error + Send + Sync,
T::Future: std::future::Future + Send + Sync,
{
async fn bind(mut self: Box<Self>, addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, addr: SocketAddr) -> Result<(), error::Error> {
let shared = tower::make::Shared::new(self);
hyper::Server::bind(&addr)
.serve(shared)
Expand All @@ -651,7 +649,7 @@ where
#[cfg(feature = "bot-serenity")]
#[async_trait]
impl Service for serenity::Client {
async fn bind(mut self: Box<Self>, _addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, _addr: SocketAddr) -> Result<(), error::Error> {
self.start().await.map_err(error::CustomError::new)?;

Ok(())
Expand All @@ -668,7 +666,7 @@ where
T: std::marker::Send + std::marker::Sync + 'static,
E: std::marker::Send + std::marker::Sync + 'static,
{
async fn bind(mut self: Box<Self>, _addr: SocketAddr) -> Result<(), error::Error> {
async fn bind(mut self, _addr: SocketAddr) -> Result<(), error::Error> {
self.start().await.map_err(error::CustomError::new)?;

Ok(())
Expand Down

0 comments on commit 69b82e6

Please sign in to comment.