@@ -30,36 +30,32 @@ where
30
30
F : Fn ( Request ) -> Fut ,
31
31
Fut : Future < Output = http_types:: Result < Response > > ,
32
32
{
33
- // TODO: make configurable
33
+ // TODO: make these values configurable
34
34
let timeout_duration = Duration :: from_secs ( 10 ) ;
35
35
const MAX_REQUESTS : usize = 200 ;
36
36
let mut num_requests = 0 ;
37
37
38
- // Decode a request. This may be the first of many since the connection is Keep-Alive by default.
39
- let r = io. clone ( ) ;
40
- let req = decode ( addr, r) . await ?;
41
-
42
- if let Some ( mut req) = req {
43
- loop {
44
- match num_requests {
45
- MAX_REQUESTS => return Ok ( ( ) ) ,
46
- _ => num_requests += 1 ,
47
- } ;
48
-
49
- // TODO: what to do when the endpoint returns Err
50
- let res = endpoint ( req) . await ?;
51
- let mut encoder = Encoder :: encode ( res) ;
52
- io:: copy ( & mut encoder, & mut io) . await ?;
53
-
54
- // Decode a new request, timing out if this takes longer than the
55
- // timeout duration.
56
- req = match timeout ( timeout_duration, decode ( addr, io. clone ( ) ) ) . await {
57
- Ok ( Ok ( Some ( r) ) ) => r,
58
- Ok ( Ok ( None ) ) | Err ( TimeoutError { .. } ) => break , /* EOF or timeout */
59
- Ok ( Err ( e) ) => return Err ( e) . into ( ) ,
60
- } ;
61
- // Loop back with the new request and stream and start again
62
- }
38
+ loop {
39
+ // Stop parsing requests if we exceed the threshold.
40
+ match num_requests {
41
+ MAX_REQUESTS => return Ok ( ( ) ) ,
42
+ _ => num_requests += 1 ,
43
+ } ;
44
+
45
+ // Decode a new request, timing out if this takes longer than the
46
+ // timeout duration.
47
+ let req = match timeout ( timeout_duration, decode ( addr, io. clone ( ) ) ) . await {
48
+ Ok ( Ok ( Some ( r) ) ) => r,
49
+ Ok ( Ok ( None ) ) | Err ( TimeoutError { .. } ) => break , /* EOF or timeout */
50
+ Ok ( Err ( e) ) => return Err ( e) . into ( ) ,
51
+ } ;
52
+
53
+ // Pass the request to the endpoint and encode the response.
54
+ let res = endpoint ( req) . await ?;
55
+ let mut encoder = Encoder :: encode ( res) ;
56
+
57
+ // Stream the response to the writer.
58
+ io:: copy ( & mut encoder, & mut io) . await ?;
63
59
}
64
60
65
61
Ok ( ( ) )
0 commit comments