|
1 | 1 | // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -use std::collections::HashMap; |
| 4 | +use logger::error; |
5 | 5 | use std::io::{Read, Write};
|
6 | 6 | use std::os::unix::io::AsRawFd;
|
7 | 7 | use std::os::unix::io::RawFd;
|
@@ -49,9 +49,9 @@ impl ServerRequest {
|
49 | 49 | /// The response is then wrapped in a `ServerResponse`.
|
50 | 50 | ///
|
51 | 51 | /// Returns a `ServerResponse` ready for yielding to the server
|
52 |
| - pub fn process<F>(&self, callable: F) -> ServerResponse |
| 52 | + pub fn process<F>(&self, mut callable: F) -> ServerResponse |
53 | 53 | where
|
54 |
| - F: Fn(&Request) -> Response, |
| 54 | + F: FnMut(&Request) -> Response, |
55 | 55 | {
|
56 | 56 | let http_response = callable(self.inner());
|
57 | 57 | ServerResponse::new(http_response, self.id)
|
@@ -388,6 +388,27 @@ impl HttpServer {
|
388 | 388 | Ok(parsed_requests)
|
389 | 389 | }
|
390 | 390 |
|
| 391 | + /// This function is responsible with flushing any remaining outgoing |
| 392 | + /// requests on the server. |
| 393 | + /// |
| 394 | + /// Note that this function can block the thread on write, since the |
| 395 | + /// operation is blocking. |
| 396 | + pub fn flush_outgoing_writes(&mut self) { |
| 397 | + for (_, connection) in self.connections.iter_mut() { |
| 398 | + while connection.state == ClientConnectionState::AwaitingOutgoing { |
| 399 | + if let Err(e) = connection.write() { |
| 400 | + if let ServerError::ConnectionError(ConnectionError::InvalidWrite) = e { |
| 401 | + // Nothing is logged since an InvalidWrite means we have successfully |
| 402 | + // flushed the connection |
| 403 | + } else { |
| 404 | + error!("Connection write error: {}", e); |
| 405 | + } |
| 406 | + break; |
| 407 | + } |
| 408 | + } |
| 409 | + } |
| 410 | + } |
| 411 | + |
391 | 412 | /// The file descriptor of the `epoll` structure can enable the server to become
|
392 | 413 | /// a non-blocking structure in an application.
|
393 | 414 | ///
|
|
0 commit comments