1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-28 06:20:36 +02:00

remove fn state service

This commit is contained in:
Nikolay Kim
2018-09-19 08:04:31 -07:00
parent 601c8a4ee6
commit 4c422983ba
5 changed files with 18 additions and 177 deletions

View File

@ -32,22 +32,6 @@ fn logger<T: AsyncRead + AsyncWrite + fmt::Debug>(
future::ok(stream)
}
/// Stateful service, counts number of connections, `ServiceState` is a state
/// for the service
#[derive(Debug)]
struct ServiceState {
num: Arc<AtomicUsize>,
}
/// Service function for our stateful service
fn service<T: AsyncRead + AsyncWrite>(
st: &mut ServiceState, _stream: T,
) -> impl Future<Item = (), Error = ()> {
let num = st.num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
}
fn main() {
env::set_var("RUST_LOG", "actix_net=trace");
env_logger::init();
@ -89,11 +73,12 @@ fn main() {
// `Response` and then uses that response as an input for next
// service. in this case, on success we use `logger` service
.and_then(logger)
// next service uses two components, service state and service function
// actix-net generates `NewService` impl that creates `ServiceState` instance
// for each new service and use `service` function as
// `Service::call`
.and_then((service, move || Ok(ServiceState { num: num.clone() })))
// Next service counts number of connections
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
})
},
).unwrap()
.start();

View File

@ -55,7 +55,11 @@ fn main() {
openssl
.clone()
.map_err(|e| println!("Openssl error: {}", e))
.and_then((service, move || Ok(ServiceState { num: num.clone() })))
.and_then(move |_| {
let num = num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
})
}).unwrap()
.start();