@@ -127,12 +127,48 @@ enum SelectOutput<A> {
127127 Done ,
128128}
129129
130- pub ( crate ) struct TcpIncoming {
130+ /// Binds a socket address for a [Router](super::Router)
131+ ///
132+ /// An incoming stream, usable with [Router::serve_with_incoming](super::Router::serve_with_incoming),
133+ /// of `AsyncRead + AsyncWrite` that communicate with clients that connect to a socket address.
134+ #[ derive( Debug ) ]
135+ pub struct TcpIncoming {
131136 inner : AddrIncoming ,
132137}
133138
134139impl TcpIncoming {
135- pub ( crate ) fn new (
140+ /// Creates an instance by binding (opening) the specified socket address
141+ /// to which the specified TCP 'nodelay' and 'keepalive' parameters are applied.
142+ /// Returns a TcpIncoming if the socket address was successfully bound.
143+ ///
144+ /// # Examples
145+ /// ```no_run
146+ /// # use tower_service::Service;
147+ /// # use http::{request::Request, response::Response};
148+ /// # use tonic::{body::BoxBody, transport::{Body, NamedService, Server, server::TcpIncoming}};
149+ /// # use core::convert::Infallible;
150+ /// # use std::error::Error;
151+ /// # fn main() { } // Cannot have type parameters, hence instead define:
152+ /// # fn run<S>(some_service: S) -> Result<(), Box<dyn Error + Send + Sync>>
153+ /// # where
154+ /// # S: Service<Request<Body>, Response = Response<BoxBody>, Error = Infallible> + NamedService + Clone + Send + 'static,
155+ /// # S::Future: Send + 'static,
156+ /// # {
157+ /// // Find a free port
158+ /// let mut port = 1322;
159+ /// let tinc = loop {
160+ /// let addr = format!("127.0.0.1:{}", port).parse().unwrap();
161+ /// match TcpIncoming::new(addr, true, None) {
162+ /// Ok(t) => break t,
163+ /// Err(_) => port += 1
164+ /// }
165+ /// };
166+ /// Server::builder()
167+ /// .add_service(some_service)
168+ /// .serve_with_incoming(tinc);
169+ /// # Ok(())
170+ /// # }
171+ pub fn new (
136172 addr : SocketAddr ,
137173 nodelay : bool ,
138174 keepalive : Option < Duration > ,
@@ -151,3 +187,17 @@ impl Stream for TcpIncoming {
151187 Pin :: new ( & mut self . inner ) . poll_accept ( cx)
152188 }
153189}
190+
191+ #[ cfg( test) ]
192+ mod tests {
193+ use crate :: transport:: server:: TcpIncoming ;
194+ #[ tokio:: test]
195+ async fn one_tcpincoming_at_a_time ( ) {
196+ let addr = "127.0.0.1:1322" . parse ( ) . unwrap ( ) ;
197+ {
198+ let _t1 = TcpIncoming :: new ( addr, true , None ) . unwrap ( ) ;
199+ let _t2 = TcpIncoming :: new ( addr, true , None ) . unwrap_err ( ) ;
200+ }
201+ let _t3 = TcpIncoming :: new ( addr, true , None ) . unwrap ( ) ;
202+ }
203+ }
0 commit comments