@@ -9,6 +9,7 @@ use std::collections::HashMap;
9
9
use std:: os:: unix:: io:: RawFd ;
10
10
use std:: result:: Result as StdResult ;
11
11
use std:: sync:: Arc ;
12
+ use std:: time:: Duration ;
12
13
13
14
use crate :: asynchronous:: stream:: { receive, respond, respond_with_status} ;
14
15
use crate :: asynchronous:: unix_incoming:: UnixIncoming ;
@@ -30,6 +31,7 @@ use tokio::{
30
31
select, spawn,
31
32
sync:: mpsc:: { channel, Receiver , Sender } ,
32
33
sync:: watch,
34
+ time:: timeout,
33
35
} ;
34
36
use tokio_vsock:: VsockListener ;
35
37
@@ -322,10 +324,32 @@ async fn do_handle_request(
322
324
metadata : context:: from_pb ( & req. metadata ) ,
323
325
} ;
324
326
325
- method . handler ( ctx , req ) . await . map_err ( |e| {
327
+ let get_unknown_status_and_log_err = |e| {
326
328
error ! ( "method handle {} got error {:?}" , path, & e) ;
327
329
get_status ( Code :: UNKNOWN , e)
328
- } )
330
+ } ;
331
+
332
+ if req. timeout_nano == 0 {
333
+ method
334
+ . handler ( ctx, req)
335
+ . await
336
+ . map_err ( get_unknown_status_and_log_err)
337
+ } else {
338
+ timeout (
339
+ Duration :: from_nanos ( req. timeout_nano as u64 ) ,
340
+ method. handler ( ctx, req) ,
341
+ )
342
+ . await
343
+ . map_err ( |_| {
344
+ // Timed out
345
+ error ! ( "method handle {} got error timed out" , path) ;
346
+ get_status ( Code :: DEADLINE_EXCEEDED , "timeout" )
347
+ } )
348
+ . and_then ( |r| {
349
+ // Handler finished
350
+ r. map_err ( get_unknown_status_and_log_err)
351
+ } )
352
+ }
329
353
}
330
354
331
355
async fn handle_request (
0 commit comments