@@ -52,7 +52,7 @@ public Task<Message> SendFor (Message message, Func<Message, Task<bool>> sender,
52
52
throw new ArgumentNullException ( "message" , "Message.Header is null" ) ;
53
53
54
54
if ( timeout > 0 ) {
55
- if ( ! this . timeouts . TryAdd ( message . Header . MessageId , timeout ) )
55
+ if ( ! this . timeouts . TryAdd ( message . Header . MessageId , DateTime . Now + TimeSpan . FromMilliseconds ( timeout ) ) )
56
56
throw new InvalidOperationException ( "Message already waiting response" ) ;
57
57
}
58
58
@@ -96,7 +96,7 @@ public Task<Message> SendFor (Message message, Task<bool> sendTask, int timeout)
96
96
throw new ArgumentNullException ( "message" , "Message.Header is null" ) ;
97
97
98
98
if ( timeout > 0 ) {
99
- if ( ! this . timeouts . TryAdd ( message . Header . MessageId , timeout ) )
99
+ if ( ! this . timeouts . TryAdd ( message . Header . MessageId , DateTime . Now + TimeSpan . FromMilliseconds ( timeout ) ) )
100
100
throw new InvalidOperationException ( "Message already waiting response" ) ;
101
101
}
102
102
@@ -137,7 +137,7 @@ public void Receive (Message message)
137
137
138
138
tcs . TrySetResult ( message ) ;
139
139
140
- int timeout ;
140
+ DateTime timeout ;
141
141
this . timeouts . TryRemove ( message . Header . MessageId , out timeout ) ;
142
142
}
143
143
@@ -156,13 +156,11 @@ public void Clear()
156
156
/// </summary>
157
157
public void CheckTimeouts ( )
158
158
{
159
- var timespan = DateTime . Now - this . lastTimeoutCheck ;
160
- this . lastTimeoutCheck = DateTime . Now ;
161
-
159
+ var now = DateTime . Now ;
162
160
List < int > remove = new List < int > ( ) ;
163
161
164
162
foreach ( var kvp in this . timeouts ) {
165
- if ( timespan . TotalMilliseconds < kvp . Value )
163
+ if ( now < kvp . Value )
166
164
continue ;
167
165
168
166
TaskCompletionSource < Message > tcs ;
@@ -174,16 +172,15 @@ public void CheckTimeouts()
174
172
175
173
foreach ( int id in remove ) {
176
174
TaskCompletionSource < Message > tcs ;
177
- int timeout ;
175
+ DateTime timeout ;
178
176
179
177
this . timeouts . TryRemove ( id , out timeout ) ;
180
178
this . messageResponses . TryRemove ( id , out tcs ) ;
181
179
}
182
180
}
183
181
184
182
private readonly ConcurrentDictionary < int , TaskCompletionSource < Message > > messageResponses = new ConcurrentDictionary < int , TaskCompletionSource < Message > > ( ) ;
185
- private readonly ConcurrentDictionary < int , int > timeouts = new ConcurrentDictionary < int , int > ( ) ;
186
- private DateTime lastTimeoutCheck = DateTime . Now ;
183
+ private readonly ConcurrentDictionary < int , DateTime > timeouts = new ConcurrentDictionary < int , DateTime > ( ) ;
187
184
188
185
private Task < Message > SendForCore ( Message message , Func < Message , Task < bool > > sender = null , Task < bool > sendTask = null )
189
186
{
0 commit comments