Skip to content

Commit d2ec96f

Browse files
committed
Fixes WithSyncCircuitBreaker
1 parent 692ff7f commit d2ec96f

File tree

2 files changed

+10
-32
lines changed

2 files changed

+10
-32
lines changed

src/core/Akka.TestKit/TestKitBase_Within.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void Within(
4949
cancellationToken: cancellationToken)
5050
.ConfigureAwait(false).GetAwaiter().GetResult();
5151
}
52-
52+
5353
/// <summary>
5454
/// Async version of <see cref="Within(TimeSpan, Action, TimeSpan?, CancellationToken)"/>
5555
/// that takes a <see cref="Func{Task}"/> instead of an <see cref="Action"/>

src/core/Akka/Pattern/CircuitBreaker.cs

+9-31
Original file line numberDiff line numberDiff line change
@@ -240,41 +240,19 @@ public Task WithCircuitBreaker<TState>(TState state, Func<TState, Task> body) =>
240240
CurrentState.InvokeState(state, body);
241241

242242
/// <summary>
243-
/// The failure will be recorded farther down.
243+
/// Wraps invocations of asynchronous calls that need to be protected.
244244
/// </summary>
245-
/// <param name="body">TBD</param>
246-
public void WithSyncCircuitBreaker(Action body)
247-
{
248-
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
249-
if (!cbTask.Wait(CallTimeout))
250-
{
251-
//throw new TimeoutException( string.Format( "Execution did not complete within the time allotted {0} ms", CallTimeout.TotalMilliseconds ) );
252-
}
253-
if (cbTask.Exception != null)
254-
{
255-
ExceptionDispatchInfo.Capture(cbTask.Exception).Throw();
256-
}
257-
}
245+
/// <param name="body">Call needing protected</param>
246+
public void WithSyncCircuitBreaker(Action body) =>
247+
WithCircuitBreaker(body, b => Task.Run(b)).GetAwaiter().GetResult();
258248

259249
/// <summary>
260-
/// Wraps invocations of asynchronous calls that need to be protected
261-
/// If this does not complete within the time allotted, it should return default(<typeparamref name="T"/>)
262-
///
263-
/// <code>
264-
/// Await.result(
265-
/// withCircuitBreaker(try Future.successful(body) catch { case NonFatal(t) ⇒ Future.failed(t) }),
266-
/// callTimeout)
267-
/// </code>
268-
///
250+
/// Wraps invocations of asynchronous calls that need to be protected.
269251
/// </summary>
270-
/// <typeparam name="T">TBD</typeparam>
271-
/// <param name="body">TBD</param>
272-
/// <returns><typeparamref name="T"/> or default(<typeparamref name="T"/>)</returns>
273-
public T WithSyncCircuitBreaker<T>(Func<T> body)
274-
{
275-
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
276-
return cbTask.Wait(CallTimeout) ? cbTask.Result : default(T);
277-
}
252+
/// <param name="body">Call needing protected</param>
253+
/// <returns>The result of the call</returns>
254+
public T WithSyncCircuitBreaker<T>(Func<T> body) =>
255+
WithCircuitBreaker(body, b => Task.Run(b)).Result;
278256

279257
/// <summary>
280258
/// Mark a successful call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the

0 commit comments

Comments
 (0)