Compare commits
2 commits
1b3147d493
...
05b5efc8bf
Author | SHA1 | Date | |
---|---|---|---|
05b5efc8bf | |||
54aa382f8b |
4 changed files with 36 additions and 11 deletions
|
@ -23,3 +23,4 @@ tower = { version = "0.4", features = ["full"] }
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
eyre = "0.6"
|
eyre = "0.6"
|
||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
|
console-subscriber = "0.1"
|
||||||
|
|
|
@ -14,15 +14,18 @@ async fn handle_req(req: Request) -> eyre::Result<Response> {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> eyre::Result<()> {
|
async fn main() -> eyre::Result<()> {
|
||||||
tracing_subscriber::fmt()
|
setup_logging();
|
||||||
.with_max_level(tracing::Level::TRACE)
|
|
||||||
.init();
|
|
||||||
|
|
||||||
let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
|
let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
|
||||||
|
|
||||||
let make_service = tower::service_fn(|addr: &AddrStream| {
|
let make_service = tower::service_fn(|addr: &AddrStream| {
|
||||||
tracing::debug!("Accept: {} -> {}", addr.remote_addr, addr.local_addr);
|
tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
|
||||||
futures::future::ok::<_, std::convert::Infallible>(tower::service_fn(handle_req))
|
|
||||||
|
let service = tower::ServiceBuilder::new()
|
||||||
|
.buffer(16)
|
||||||
|
.service_fn(handle_req);
|
||||||
|
|
||||||
|
futures::future::ok::<_, std::convert::Infallible>(service)
|
||||||
});
|
});
|
||||||
|
|
||||||
let server = Server::new(incoming).serve(make_service);
|
let server = Server::new(incoming).serve(make_service);
|
||||||
|
@ -30,3 +33,23 @@ async fn main() -> eyre::Result<()> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't mind this, this is just for debugging.
|
||||||
|
fn setup_logging() {
|
||||||
|
use tracing_subscriber::prelude::*;
|
||||||
|
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(console_subscriber::spawn())
|
||||||
|
.with(
|
||||||
|
tracing_subscriber::fmt::layer().with_filter(
|
||||||
|
tracing_subscriber::filter::Targets::new()
|
||||||
|
.with_default(tracing::Level::DEBUG)
|
||||||
|
.with_target("boitalettres", tracing::Level::TRACE)
|
||||||
|
.with_target("simple", tracing::Level::TRACE)
|
||||||
|
.with_target("tower", tracing::Level::TRACE)
|
||||||
|
.with_target("tokio_tower", tracing::Level::TRACE)
|
||||||
|
.with_target("mio", tracing::Level::TRACE),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.init();
|
||||||
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub enum Error<A> {
|
||||||
#[error("Error occured when accepting new connections")]
|
#[error("Error occured when accepting new connections")]
|
||||||
Accept(#[source] A),
|
Accept(#[source] A),
|
||||||
#[error("Error occured on service creation")]
|
#[error("Error occured on service creation")]
|
||||||
MakeService(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
|
MakeService(#[source] tower::BoxError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
|
@ -51,7 +51,7 @@ where
|
||||||
I: Accept,
|
I: Accept,
|
||||||
I::Conn: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
I::Conn: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||||
S: MakeServiceRef<I::Conn, Request, Response = Response>,
|
S: MakeServiceRef<I::Conn, Request, Response = Response>,
|
||||||
S::MakeError: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + std::fmt::Display,
|
S::MakeError: Into<tower::BoxError> + std::fmt::Display,
|
||||||
S::Error: std::fmt::Display,
|
S::Error: std::fmt::Display,
|
||||||
S::Future: Send + 'static,
|
S::Future: Send + 'static,
|
||||||
S::Service: Send + 'static,
|
S::Service: Send + 'static,
|
||||||
|
|
|
@ -8,7 +8,8 @@ use futures::stream::Stream;
|
||||||
|
|
||||||
use crate::proto::{Request, Response};
|
use crate::proto::{Request, Response};
|
||||||
|
|
||||||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
|
type Error = tower::BoxError;
|
||||||
|
type Result<T, E = Error> = std::result::Result<T, E>;
|
||||||
|
|
||||||
#[pin_project::pin_project]
|
#[pin_project::pin_project]
|
||||||
pub struct Connection<C> {
|
pub struct Connection<C> {
|
||||||
|
@ -32,7 +33,7 @@ impl<C> Stream for Connection<C>
|
||||||
where
|
where
|
||||||
C: AsyncRead + Unpin,
|
C: AsyncRead + Unpin,
|
||||||
{
|
{
|
||||||
type Item = Result<Request, Error>;
|
type Item = Result<Request>;
|
||||||
|
|
||||||
fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
|
fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
use imap_codec::parse::command::command as parse_command;
|
use imap_codec::parse::command::command as parse_command;
|
||||||
|
@ -75,7 +76,7 @@ impl<C> Connection<C>
|
||||||
where
|
where
|
||||||
C: AsyncWrite,
|
C: AsyncWrite,
|
||||||
{
|
{
|
||||||
fn poll_flush_buffer(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Result<(), Error>> {
|
fn poll_flush_buffer(self: Pin<&mut Self>, cx: &mut task::Context) -> Poll<Result<()>> {
|
||||||
use bytes::Buf;
|
use bytes::Buf;
|
||||||
|
|
||||||
let mut this = self.project();
|
let mut this = self.project();
|
||||||
|
@ -89,7 +90,7 @@ where
|
||||||
Poll::Ready(Ok(()))
|
Poll::Ready(Ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn send(&mut self, item: Response) -> Result<(), Error> {
|
pub(crate) fn send(&mut self, item: Response) -> Result<()> {
|
||||||
use bytes::BufMut;
|
use bytes::BufMut;
|
||||||
use imap_codec::codec::Encode;
|
use imap_codec::codec::Encode;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue