Skip to content

Commit

Permalink
Fixes WithSyncCircuitBreaker
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelhamed committed Sep 28, 2022
1 parent febec6b commit bda4de1
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions src/core/Akka/Pattern/CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,41 +240,19 @@ public Task WithCircuitBreaker<TState>(TState state, Func<TState, Task> body) =>
CurrentState.InvokeState(state, body);

/// <summary>
/// The failure will be recorded farther down.
/// Wraps invocations of asynchronous calls that need to be protected.
/// </summary>
/// <param name="body">TBD</param>
public void WithSyncCircuitBreaker(Action body)
{
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
if (!cbTask.Wait(CallTimeout))
{
//throw new TimeoutException( string.Format( "Execution did not complete within the time allotted {0} ms", CallTimeout.TotalMilliseconds ) );
}
if (cbTask.Exception != null)
{
ExceptionDispatchInfo.Capture(cbTask.Exception).Throw();
}
}
/// <param name="body">Call needing protected</param>
public void WithSyncCircuitBreaker(Action body) =>
WithCircuitBreaker(body, b => Task.Run(b)).GetAwaiter().GetResult();

/// <summary>
/// Wraps invocations of asynchronous calls that need to be protected
/// If this does not complete within the time allotted, it should return default(<typeparamref name="T"/>)
///
/// <code>
/// Await.result(
/// withCircuitBreaker(try Future.successful(body) catch { case NonFatal(t) ⇒ Future.failed(t) }),
/// callTimeout)
/// </code>
///
/// Wraps invocations of asynchronous calls that need to be protected.
/// </summary>
/// <typeparam name="T">TBD</typeparam>
/// <param name="body">TBD</param>
/// <returns><typeparamref name="T"/> or default(<typeparamref name="T"/>)</returns>
public T WithSyncCircuitBreaker<T>(Func<T> body)
{
var cbTask = WithCircuitBreaker(body,(b) => Task.Factory.StartNew(b));
return cbTask.Wait(CallTimeout) ? cbTask.Result : default(T);
}
/// <param name="body">Call needing protected</param>
/// <returns>The result of the call</returns>
public T WithSyncCircuitBreaker<T>(Func<T> body) =>
WithCircuitBreaker(body, b => Task.Run(b)).Result;

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

0 comments on commit bda4de1

Please sign in to comment.