@@ -35,6 +35,8 @@ use std::{
35
35
36
36
/// Read timeout for inbound messages.
37
37
const READ_TIMEOUT : Duration = Duration :: from_secs ( 15 ) ;
38
+ /// Write timeout for outbound messages.
39
+ const WRITE_TIMEOUT : Duration = Duration :: from_secs ( 15 ) ;
38
40
39
41
/// Query result.
40
42
#[ derive( Debug ) ]
@@ -91,16 +93,24 @@ impl QueryExecutor {
91
93
/// Send message to remote peer.
92
94
pub fn send_message ( & mut self , peer : PeerId , message : Bytes , mut substream : Substream ) {
93
95
self . futures . push ( Box :: pin ( async move {
94
- match substream. send_framed ( message) . await {
95
- Ok ( _) => QueryContext {
96
+ match tokio:: time:: timeout ( WRITE_TIMEOUT , substream. send_framed ( message) ) . await {
97
+ // Timeout error.
98
+ Err ( _) =>
99
+ return QueryContext {
100
+ peer,
101
+ query_id : None ,
102
+ result : QueryResult :: Timeout ,
103
+ } ,
104
+ // Writing message to substream failed.
105
+ Ok ( Err ( _) ) => QueryContext {
96
106
peer,
97
107
query_id : None ,
98
- result : QueryResult :: SendSuccess { substream } ,
108
+ result : QueryResult :: SubstreamClosed ,
99
109
} ,
100
- Err ( _ ) => QueryContext {
110
+ Ok ( Ok ( ( ) ) ) => QueryContext {
101
111
peer,
102
112
query_id : None ,
103
- result : QueryResult :: SubstreamClosed ,
113
+ result : QueryResult :: SendSuccess { substream } ,
104
114
} ,
105
115
}
106
116
} ) ) ;
@@ -143,14 +153,25 @@ impl QueryExecutor {
143
153
mut substream : Substream ,
144
154
) {
145
155
self . futures . push ( Box :: pin ( async move {
146
- if let Err ( _) = substream. send_framed ( message) . await {
147
- let _ = substream. close ( ) . await ;
148
- return QueryContext {
149
- peer,
150
- query_id,
151
- result : QueryResult :: SubstreamClosed ,
152
- } ;
153
- }
156
+ match tokio:: time:: timeout ( WRITE_TIMEOUT , substream. send_framed ( message) ) . await {
157
+ // Timeout error.
158
+ Err ( _) =>
159
+ return QueryContext {
160
+ peer,
161
+ query_id,
162
+ result : QueryResult :: Timeout ,
163
+ } ,
164
+ // Writing message to substream failed.
165
+ Ok ( Err ( _) ) => {
166
+ let _ = substream. close ( ) . await ;
167
+ return QueryContext {
168
+ peer,
169
+ query_id,
170
+ result : QueryResult :: SubstreamClosed ,
171
+ } ;
172
+ }
173
+ Ok ( Ok ( ( ) ) ) => ( ) ,
174
+ } ;
154
175
155
176
match tokio:: time:: timeout ( READ_TIMEOUT , substream. next ( ) ) . await {
156
177
Err ( _) => QueryContext {
0 commit comments