9
9
using System . Collections . Concurrent ;
10
10
using System . Runtime . ExceptionServices ;
11
11
using System . Threading . Tasks ;
12
+ using Akka . Util . Extensions ;
12
13
13
14
namespace Akka . Util . Internal
14
15
{
@@ -73,149 +74,78 @@ await Task
73
74
/// <summary>
74
75
/// Shared implementation of call across all states. Thrown exception or execution of the call beyond the allowed
75
76
/// call timeout is counted as a failed call, otherwise a successful call
76
- ///
77
- /// NOTE: In .Net there is no way to cancel an uncancellable task. We are merely cancelling the wait and marking this
78
- /// as a failure.
79
- ///
80
- /// see http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx
81
77
/// </summary>
82
- /// <typeparam name="T">TBD</typeparam>
83
- /// <param name="task">Implementation of the call</param>
84
- /// <returns>result of the call</returns>
78
+ /// <param name="task"><see cref="Task"/> Implementation of the call</param>
79
+ /// <returns><see cref="Task"/> containing the result of the call</returns>
85
80
public async Task < T > CallThrough < T > ( Func < Task < T > > task )
86
81
{
87
- var deadline = DateTime . UtcNow . Add ( _callTimeout ) ;
88
- ExceptionDispatchInfo capturedException = null ;
89
- T result = default ( T ) ;
82
+ var result = default ( T ) ;
90
83
try
91
84
{
92
- result = await task ( ) . ConfigureAwait ( false ) ;
85
+ result = await task ( ) . WaitAsync ( _callTimeout ) . ConfigureAwait ( false ) ;
86
+ CallSucceeds ( ) ;
93
87
}
94
88
catch ( Exception ex )
95
89
{
96
- capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
97
- }
98
-
99
- // Need to make sure that timeouts are reported as timeouts
100
- if ( capturedException != null )
101
- {
90
+ var capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
102
91
CallFails ( capturedException . SourceException ) ;
103
92
capturedException . Throw ( ) ;
104
93
}
105
- else if ( DateTime . UtcNow . CompareTo ( deadline ) >= 0 )
106
- {
107
- CallFails ( new TimeoutException (
108
- $ "Execution did not complete within the time allotted { _callTimeout . TotalMilliseconds } ms") ) ;
109
- }
110
- else
111
- {
112
- CallSucceeds ( ) ;
113
- }
94
+
114
95
return result ;
115
96
}
116
-
117
- public async Task < T > CallThrough < T , TState > ( TState state , Func < TState , Task < T > > task )
97
+
98
+ public async Task < T > CallThrough < T , TState > ( TState state , Func < TState , Task < T > > task )
118
99
{
119
- var deadline = DateTime . UtcNow . Add ( _callTimeout ) ;
120
- ExceptionDispatchInfo capturedException = null ;
121
- T result = default ( T ) ;
100
+ var result = default ( T ) ;
122
101
try
123
102
{
124
- result = await task ( state ) . ConfigureAwait ( false ) ;
103
+ result = await task ( state ) . WaitAsync ( _callTimeout ) . ConfigureAwait ( false ) ;
104
+ CallSucceeds ( ) ;
125
105
}
126
106
catch ( Exception ex )
127
107
{
128
- capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
129
- }
130
-
131
- // Need to make sure that timeouts are reported as timeouts
132
- if ( capturedException != null )
133
- {
108
+ var capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
134
109
CallFails ( capturedException . SourceException ) ;
135
110
capturedException . Throw ( ) ;
136
111
}
137
- else if ( DateTime . UtcNow . CompareTo ( deadline ) >= 0 )
138
- {
139
- CallFails ( new TimeoutException (
140
- $ "Execution did not complete within the time allotted { _callTimeout . TotalMilliseconds } ms") ) ;
141
- }
142
- else
143
- {
144
- CallSucceeds ( ) ;
145
- }
112
+
146
113
return result ;
147
114
}
148
115
149
116
/// <summary>
150
117
/// Shared implementation of call across all states. Thrown exception or execution of the call beyond the allowed
151
118
/// call timeout is counted as a failed call, otherwise a successful call
152
- ///
153
- /// NOTE: In .Net there is no way to cancel an uncancellable task. We are merely cancelling the wait and marking this
154
- /// as a failure.
155
- ///
156
- /// see http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx
157
119
/// </summary>
158
120
/// <param name="task"><see cref="Task"/> Implementation of the call</param>
159
- /// <returns><see cref="Task"/></returns>
121
+ /// <returns><see cref="Task"/> containing the result of the call </returns>
160
122
public async Task CallThrough ( Func < Task > task )
161
123
{
162
- var deadline = DateTime . UtcNow . Add ( _callTimeout ) ;
163
- ExceptionDispatchInfo capturedException = null ;
164
-
165
124
try
166
125
{
167
- await task ( ) . ConfigureAwait ( false ) ;
126
+ await task ( ) . WaitAsync ( _callTimeout ) . ConfigureAwait ( false ) ;
127
+ CallSucceeds ( ) ;
168
128
}
169
129
catch ( Exception ex )
170
130
{
171
- capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
172
- }
173
-
174
- // Need to make sure that timeouts are reported as timeouts
175
- if ( capturedException != null )
176
- {
177
- CallFails ( capturedException ? . SourceException ) ;
131
+ var capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
132
+ CallFails ( capturedException . SourceException ) ;
178
133
capturedException . Throw ( ) ;
179
- }
180
- else if ( DateTime . UtcNow . CompareTo ( deadline ) >= 0 )
181
- {
182
- CallFails ( new TimeoutException (
183
- $ "Execution did not complete within the time allotted { _callTimeout . TotalMilliseconds } ms") ) ;
184
- }
185
- else
186
- {
187
- CallSucceeds ( ) ;
188
134
}
189
135
}
190
-
136
+
191
137
public async Task CallThrough < TState > ( TState state , Func < TState , Task > task )
192
138
{
193
- var deadline = DateTime . UtcNow . Add ( _callTimeout ) ;
194
- ExceptionDispatchInfo capturedException = null ;
195
-
196
139
try
197
140
{
198
- await task ( state ) . ConfigureAwait ( false ) ;
141
+ await task ( state ) . WaitAsync ( _callTimeout ) . ConfigureAwait ( false ) ;
142
+ CallSucceeds ( ) ;
199
143
}
200
144
catch ( Exception ex )
201
145
{
202
- capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
203
- }
204
-
205
- // Need to make sure that timeouts are reported as timeouts
206
- if ( capturedException != null )
207
- {
208
- CallFails ( capturedException ? . SourceException ) ;
146
+ var capturedException = ExceptionDispatchInfo . Capture ( ex ) ;
147
+ CallFails ( capturedException . SourceException ) ;
209
148
capturedException . Throw ( ) ;
210
- }
211
- else if ( DateTime . UtcNow . CompareTo ( deadline ) >= 0 )
212
- {
213
- CallFails ( new TimeoutException (
214
- $ "Execution did not complete within the time allotted { _callTimeout . TotalMilliseconds } ms") ) ;
215
- }
216
- else
217
- {
218
- CallSucceeds ( ) ;
219
149
}
220
150
}
221
151
@@ -237,9 +167,7 @@ public abstract Task<T> InvokeState<T, TState>(TState state,
237
167
/// <returns><see cref="Task"/> containing result of protected call</returns>
238
168
public abstract Task Invoke ( Func < Task > body ) ;
239
169
240
- public abstract Task InvokeState < TState > ( TState state ,
241
- Func < TState , Task > body ) ;
242
-
170
+ public abstract Task InvokeState < TState > ( TState state , Func < TState , Task > body ) ;
243
171
244
172
/// <summary>
245
173
/// Invoked when call fails
@@ -265,7 +193,6 @@ public void Enter()
265
193
EnterInternal ( ) ;
266
194
NotifyTransitionListeners ( ) ;
267
195
}
268
-
269
196
}
270
197
271
198
/// <summary>
0 commit comments