Skip to content

Commit 0029037

Browse files
committed
async-listen crate: Add error_hint() invocation
1 parent c8c0756 commit 0029037

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

docs/src/patterns/accept-loop.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,33 @@ The crate [`async-listen`] have a helper to achieve this task:
159159
#
160160
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
161161
#
162-
use async_listen::ListenExt;
162+
use async_listen::{ListenExt, error_hint};
163163
164164
async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
165165
166166
let listener = TcpListener::bind(addr).await?;
167167
let mut incoming = listener
168168
.incoming()
169-
.log_warnings(|e| eprintln!("Error: {}. Pausing for 500ms.", e)) // 1
169+
.log_warnings(log_accept_error) // 1
170170
.handle_errors(Duration::from_millis(500));
171171
while let Some(socket) = incoming.next().await { // 2
172172
// body
173173
}
174174
Ok(())
175175
}
176+
177+
fn log_accept_error(e: &io::Error) {
178+
eprintln!("Error: {}. Listener paused for 0.5s. {}", e, error_hint(e)) // 3
179+
}
176180
```
177181

178182
1. Logs resource shortages (`async-listen` calls them warnings). If you use
179183
`log` crate or any other in your app this should go to the log.
180184
2. Stream yields sockets without `Result` wrapper after `handle_errors` because
181185
all errors are already handled.
186+
3. Together with the error we print a hint, which explains some errors for end
187+
users. For example, it recommends increasing open file limit and gives
188+
a link.
182189

183190
[`async-listen`]: https://crates.io/crates/async-listen/
184191

@@ -221,14 +228,14 @@ looks like the following:
221228
#
222229
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
223230
#
224-
use async_listen::{ListenExt, Token};
231+
use async_listen::{ListenExt, Token, error_hint};
225232
226233
async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
227234
228235
let listener = TcpListener::bind(addr).await?;
229236
let mut incoming = listener
230237
.incoming()
231-
.log_warnings(|e| eprintln!("Error: {}. Pausing for 500ms.", e))
238+
.log_warnings(log_accept_error)
232239
.handle_errors(Duration::from_millis(500)) // 1
233240
.backpressure(100);
234241
while let Some((token, socket)) = incoming.next().await { // 2
@@ -241,6 +248,9 @@ async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
241248
async fn connection_loop(_token: &Token, stream: TcpStream) { // 4
242249
// ...
243250
}
251+
# fn log_accept_error(e: &io::Error) {
252+
# eprintln!("Error: {}. Listener paused for 0.5s. {}", e, error_hint(e));
253+
# }
244254
```
245255

246256
1. We need to handle errors first, because [`backpressure`] helper expects

0 commit comments

Comments
 (0)