@@ -159,26 +159,33 @@ The crate [`async-listen`] have a helper to achieve this task:
159
159
#
160
160
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
161
161
#
162
- use async_listen::ListenExt;
162
+ use async_listen::{ ListenExt, error_hint} ;
163
163
164
164
async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
165
165
166
166
let listener = TcpListener::bind(addr).await?;
167
167
let mut incoming = listener
168
168
.incoming()
169
- .log_warnings(|e| eprintln!("Error: {}. Pausing for 500ms.", e) ) // 1
169
+ .log_warnings(log_accept_error ) // 1
170
170
.handle_errors(Duration::from_millis(500));
171
171
while let Some(socket) = incoming.next().await { // 2
172
172
// body
173
173
}
174
174
Ok(())
175
175
}
176
+
177
+ fn log_accept_error(e: &io::Error) {
178
+ eprintln!("Error: {}. Listener paused for 0.5s. {}", e, error_hint(e)) // 3
179
+ }
176
180
```
177
181
178
182
1 . Logs resource shortages (` async-listen ` calls them warnings). If you use
179
183
` log ` crate or any other in your app this should go to the log.
180
184
2 . Stream yields sockets without ` Result ` wrapper after ` handle_errors ` because
181
185
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.
182
189
183
190
[ `async-listen` ] : https://crates.io/crates/async-listen/
184
191
@@ -221,14 +228,14 @@ looks like the following:
221
228
#
222
229
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
223
230
#
224
- use async_listen::{ListenExt, Token};
231
+ use async_listen::{ListenExt, Token, error_hint };
225
232
226
233
async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
227
234
228
235
let listener = TcpListener::bind(addr).await?;
229
236
let mut incoming = listener
230
237
.incoming()
231
- .log_warnings(|e| eprintln!("Error: {}. Pausing for 500ms.", e) )
238
+ .log_warnings(log_accept_error )
232
239
.handle_errors(Duration::from_millis(500)) // 1
233
240
.backpressure(100);
234
241
while let Some((token, socket)) = incoming.next().await { // 2
@@ -241,6 +248,9 @@ async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> {
241
248
async fn connection_loop(_token: &Token, stream: TcpStream) { // 4
242
249
// ...
243
250
}
251
+ # fn log_accept_error(e: &io::Error) {
252
+ # eprintln!("Error: {}. Listener paused for 0.5s. {}", e, error_hint(e));
253
+ # }
244
254
```
245
255
246
256
1 . We need to handle errors first, because [ ` backpressure ` ] helper expects
0 commit comments