-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(actix): Migrate server actor to the "service" arch (#1723)
This change introduces changes to migrate `Server` actor to the new `Service` architecture: * remove `actix` dependencies from the `Server` actor * implement `Service` for the `Server` actor * introduce the `HttpServer` helper struct, which takes care of starting the shutting down the `actix_web` http service, and removes exposed `Recipient` from the `Server` actor - now everything is hidden and in the future can be removed from the one place. The former `Server` actor and current `ServerService` subscribes only to `Shutdown` watch channel and makes sure to trigger the shutdown of the http server with all its workers.
- Loading branch information
Showing
5 changed files
with
114 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,37 @@ | ||
use ::actix::prelude::*; | ||
use actix_web::server::StopServer; | ||
use futures01::prelude::*; | ||
|
||
use relay_config::Config; | ||
use relay_statsd::metric; | ||
use relay_system::{Controller, Shutdown}; | ||
use relay_system::{Controller, Service, Shutdown}; | ||
|
||
use crate::service; | ||
use crate::service::HttpServer; | ||
use crate::statsd::RelayCounters; | ||
|
||
pub struct Server { | ||
http_server: Recipient<StopServer>, | ||
pub struct ServerService { | ||
http_server: HttpServer, | ||
} | ||
|
||
impl Server { | ||
pub fn start(config: Config) -> anyhow::Result<Addr<Self>> { | ||
impl ServerService { | ||
pub fn start(config: Config) -> anyhow::Result<()> { | ||
metric!(counter(RelayCounters::ServerStarting) += 1); | ||
let http_server = service::start(config)?; | ||
Ok(Server { http_server }.start()) | ||
} | ||
} | ||
|
||
impl Actor for Server { | ||
type Context = Context<Self>; | ||
|
||
fn started(&mut self, context: &mut Self::Context) { | ||
Controller::subscribe(context.address()); | ||
let http_server = HttpServer::start(config)?; | ||
Self { http_server }.start(); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Handler<Shutdown> for Server { | ||
type Result = ResponseFuture<(), ()>; | ||
|
||
fn handle(&mut self, message: Shutdown, _context: &mut Self::Context) -> Self::Result { | ||
let graceful = message.timeout.is_some(); | ||
|
||
// We assume graceful shutdown if we're given a timeout. The actix-web http server is | ||
// configured with the same timeout, so it will match. Unfortunately, we have to drop any | ||
// errors and replace them with the generic `TimeoutError`. | ||
let future = self | ||
.http_server | ||
.send(StopServer { graceful }) | ||
.map_err(|_| ()) | ||
.and_then(|result| result.map_err(|_| ())); | ||
|
||
Box::new(future) | ||
impl Service for ServerService { | ||
type Interface = (); | ||
|
||
fn spawn_handler(self, _rx: relay_system::Receiver<Self::Interface>) { | ||
tokio::spawn(async move { | ||
let mut shutdown = Controller::shutdown_handle(); | ||
loop { | ||
tokio::select! { | ||
Shutdown { timeout } = shutdown.notified() => { | ||
self.http_server.shutdown(timeout.is_some()); | ||
}, | ||
else => break, | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters