@@ -12,7 +12,7 @@ use futures::future::{Either, join_all};
1212use futures:: sync:: mpsc;
1313use parking_lot:: RwLock ;
1414use tokio:: timer:: Interval ;
15- use tokio:: timer :: timeout :: Error as TimeoutError ;
15+ use tokio:: util :: FutureExt ;
1616
1717use std:: collections:: HashMap ;
1818use std:: net:: SocketAddr ;
@@ -70,8 +70,6 @@ pub const FAKE_FRIENDS_NUMBER: usize = 2;
7070/// Maximum number of entry in Lru cache for precomputed keys.
7171pub const PRECOMPUTED_LRU_CACHE_SIZE : usize = KBUCKET_DEFAULT_SIZE as usize * KBUCKET_MAX_ENTRIES as usize + // For KTree.
7272 KBUCKET_DEFAULT_SIZE as usize * ( 2 + 10 ) ; // For friend's close_nodes of 2 fake friends + 10 friends reserved
73- /// Timeout in seconds for packet sending
74- pub const DHT_SEND_TIMEOUT : u64 = 1 ;
7573/// How often DHT main loop should be called.
7674const MAIN_LOOP_INTERVAL : u64 = 1 ;
7775
@@ -382,15 +380,22 @@ impl Server {
382380 . map_err ( |e| e. context ( RunErrorKind :: Wakeup ) . into ( ) )
383381 . for_each ( move |_instant| {
384382 trace ! ( "Bootstrap wake up" ) ;
385- self . send_bootstrap_requests ( )
386- . map_err ( |e| e. context ( RunErrorKind :: SendTo ) . into ( ) )
383+ self . send_bootstrap_requests ( ) . timeout ( interval) . then ( |r| {
384+ if let Err ( e) = r {
385+ warn ! ( "Failed to send initial bootstrap packets: {}" , e) ;
386+ if let Some ( e) = e. into_inner ( ) {
387+ return future:: err ( e. context ( RunErrorKind :: SendTo ) . into ( ) )
388+ }
389+ }
390+ future:: ok ( ( ) )
391+ } )
387392 } )
388393 }
389394
390395 /// Check if all nodes in Ktree are discarded (including the case when
391396 /// it's empty) and if so then send `NodesRequest` packet to nodes from
392397 /// initial bootstrap list and from Ktree.
393- fn send_bootstrap_requests ( & self ) -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
398+ fn send_bootstrap_requests ( & self ) -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
394399 let mut request_queue = self . request_queue . write ( ) ;
395400 let close_nodes = self . close_nodes . read ( ) ;
396401
@@ -417,9 +422,12 @@ impl Server {
417422 . map_err ( |e| e. context ( RunErrorKind :: Wakeup ) . into ( ) )
418423 . for_each ( move |_instant| {
419424 trace ! ( "DHT server wake up" ) ;
420- self . dht_main_loop ( ) . then ( |res| {
425+ self . dht_main_loop ( ) . timeout ( interval ) . then ( |res| {
421426 if let Err ( e) = res {
422427 warn ! ( "Failed to send DHT periodical packets: {}" , e) ;
428+ if let Some ( e) = e. into_inner ( ) {
429+ return future:: err ( e. context ( RunErrorKind :: SendTo ) . into ( ) )
430+ }
423431 }
424432 future:: ok ( ( ) )
425433 } )
@@ -455,7 +463,7 @@ impl Server {
455463 }
456464
457465 /// Send `PingRequest` packets to nodes from `nodes_to_ping` list.
458- fn send_pings ( & self ) -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
466+ fn send_pings ( & self ) -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
459467 let nodes_to_ping = mem:: replace (
460468 & mut * self . nodes_to_ping . write ( ) ,
461469 Kbucket :: < PackedNode > :: new ( MAX_TO_PING )
@@ -478,7 +486,7 @@ impl Server {
478486 /// a friend and we don't know it's address then this method will send
479487 /// `PingRequest` immediately instead of adding to a `nodes_to_ping`
480488 /// list.
481- fn ping_add ( & self , node : & PackedNode ) -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
489+ fn ping_add ( & self , node : & PackedNode ) -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
482490 let close_nodes = self . close_nodes . read ( ) ;
483491
484492 if !close_nodes. can_add ( & node) {
@@ -502,7 +510,7 @@ impl Server {
502510 /// necessary to check whether node is alive before adding it to close
503511 /// nodes lists.
504512 fn ping_nodes_to_bootstrap ( & self , request_queue : & mut RequestQueue < PublicKey > , nodes_to_bootstrap : & mut Kbucket < PackedNode > , pk : PublicKey )
505- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
513+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
506514 let capacity = nodes_to_bootstrap. capacity ( ) as u8 ;
507515 let nodes_to_bootstrap = mem:: replace ( nodes_to_bootstrap, Kbucket :: new ( capacity) ) ;
508516
@@ -516,7 +524,7 @@ impl Server {
516524 /// Iterate over nodes from close nodes list and send `NodesRequest` packets
517525 /// to them if necessary.
518526 fn ping_close_nodes < ' a , T > ( & self , request_queue : & mut RequestQueue < PublicKey > , nodes : T , pk : PublicKey )
519- -> Box < dyn Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send >
527+ -> Box < dyn Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send >
520528 where T : Iterator < Item = & ' a mut DhtNode > // if change to impl Future the result will be dependent on nodes lifetime
521529 {
522530 let futures = nodes
@@ -539,7 +547,7 @@ impl Server {
539547 /// it was sent less than `NODES_REQ_INTERVAL`. This function should be
540548 /// called every second.
541549 fn send_nodes_req_random < ' a , T > ( & self , request_queue : & mut RequestQueue < PublicKey > , nodes : T , pk : PublicKey )
542- -> Box < dyn Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send >
550+ -> Box < dyn Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send >
543551 where T : Iterator < Item = & ' a DhtNode > // if change to impl Future the result will be dependent on nodes lifetime
544552 {
545553 let good_nodes = nodes
@@ -572,7 +580,7 @@ impl Server {
572580
573581 /// Send `PingRequest` packet to the node.
574582 fn send_ping_req ( & self , node : & PackedNode , request_queue : & mut RequestQueue < PublicKey > )
575- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
583+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
576584 let payload = PingRequestPayload {
577585 id : request_queue. new_ping_id ( node. pk ) ,
578586 } ;
@@ -586,7 +594,7 @@ impl Server {
586594
587595 /// Send `NodesRequest` packet to the node.
588596 fn send_nodes_req ( & self , node : & PackedNode , request_queue : & mut RequestQueue < PublicKey > , search_pk : PublicKey )
589- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
597+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
590598 // Check if packet is going to be sent to ourselves.
591599 if self . pk == node. pk {
592600 trace ! ( "Attempt to send NodesRequest to ourselves." ) ;
@@ -607,7 +615,7 @@ impl Server {
607615
608616 /// Send `NatPingRequest` packet to all friends and try to punch holes.
609617 fn send_nat_ping_req ( & self , request_queue : & mut RequestQueue < PublicKey > , friends : & mut HashMap < PublicKey , DhtFriend > )
610- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
618+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
611619 let futures = friends. values_mut ( )
612620 . filter ( |friend| !friend. is_addr_known ( ) )
613621 . map ( |friend| {
@@ -645,7 +653,7 @@ impl Server {
645653
646654 /// Try to punch holes to specified friend.
647655 fn punch_holes ( & self , request_queue : & mut RequestQueue < PublicKey > , friend : & mut DhtFriend , returned_addrs : & [ SocketAddr ] )
648- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
656+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
649657 let punch_addrs = friend. hole_punch . next_punch_addrs ( returned_addrs) ;
650658
651659 let packets = punch_addrs. into_iter ( ) . map ( |addr| {
@@ -661,13 +669,13 @@ impl Server {
661669 ( packet, addr)
662670 } ) . collect :: < Vec < _ > > ( ) ;
663671
664- send_all_to_bounded ( & self . tx , stream:: iter_ok ( packets) , Duration :: from_secs ( DHT_SEND_TIMEOUT ) )
672+ send_all_to ( & self . tx , stream:: iter_ok ( packets) )
665673 }
666674
667675 /// Send `NatPingRequest` packet to all close nodes of friend in the hope
668676 /// that they will redirect it to this friend.
669677 fn send_nat_ping_req_inner ( & self , friend : & DhtFriend , nat_ping_req_packet : DhtRequest )
670- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
678+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
671679 let packet = Packet :: DhtRequest ( nat_ping_req_packet) ;
672680 let futures = friend. close_nodes . nodes
673681 . iter ( )
@@ -713,8 +721,8 @@ impl Server {
713721
714722 /// Send UDP packet to specified address.
715723 fn send_to ( & self , addr : SocketAddr , packet : Packet )
716- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
717- send_to_bounded ( & self . tx , ( packet, addr) , Duration :: from_secs ( DHT_SEND_TIMEOUT ) )
724+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
725+ send_to ( & self . tx , ( packet, addr) )
718726 }
719727
720728 /// Handle received `PingRequest` packet and response with `PingResponse`
@@ -1335,7 +1343,7 @@ impl Server {
13351343 /// Handle `OnionRequest` from TCP relay and send `OnionRequest1` packet
13361344 /// to the next node in the onion path.
13371345 pub fn handle_tcp_onion_request ( & self , packet : OnionRequest , addr : SocketAddr )
1338- -> impl Future < Item = ( ) , Error = TimeoutError < mpsc:: SendError < ( Packet , SocketAddr ) > > > + Send {
1346+ -> impl Future < Item = ( ) , Error = mpsc:: SendError < ( Packet , SocketAddr ) > > + Send {
13391347 let onion_symmetric_key = self . onion_symmetric_key . read ( ) ;
13401348
13411349 let onion_return = OnionReturn :: new (
0 commit comments