-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tomasz Maruszak <[email protected]>
- Loading branch information
Showing
37 changed files
with
442 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 3 additions & 10 deletions
13
src/Samples/Sample.CircuitBreaker.HealthCheck/Consumers/AddConsumer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Consumers; | ||
|
||
public class AddConsumer : IConsumer<Add> | ||
{ | ||
private readonly ILogger<AddConsumer> _logger; | ||
|
||
public AddConsumer(ILogger<AddConsumer> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public class AddConsumer(ILogger<AddConsumer> logger) : IConsumer<Add> | ||
{ | ||
public Task OnHandle(Add message, CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("{A} + {B} = {C}", message.a, message.b, message.a + message.b); | ||
logger.LogInformation("{A} + {B} = {C}", message.A, message.B, message.A + message.B); | ||
return Task.CompletedTask; | ||
} | ||
} |
13 changes: 3 additions & 10 deletions
13
src/Samples/Sample.CircuitBreaker.HealthCheck/Consumers/SubtractConsumer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Consumers; | ||
|
||
public class SubtractConsumer : IConsumer<Subtract> | ||
{ | ||
private readonly ILogger<SubtractConsumer> _logger; | ||
|
||
public SubtractConsumer(ILogger<SubtractConsumer> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public class SubtractConsumer(ILogger<SubtractConsumer> logger) : IConsumer<Subtract> | ||
{ | ||
public Task OnHandle(Subtract message, CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("{A} - {B} = {C}", message.a, message.b, message.a - message.b); | ||
logger.LogInformation("{A} - {B} = {C}", message.A, message.B, message.A - message.B); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/AddRandomHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
public class AddRandomHealthCheck : RandomHealthCheck | ||
public class AddRandomHealthCheck(ILogger<AddRandomHealthCheck> logger) : RandomHealthCheck(logger) | ||
{ | ||
public AddRandomHealthCheck(ILogger<AddRandomHealthCheck> logger) | ||
: base(logger) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 1 addition & 7 deletions
8
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/SubtractRandomHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
public class SubtractRandomHealthCheck : RandomHealthCheck | ||
public class SubtractRandomHealthCheck(ILogger<SubtractRandomHealthCheck> logger) : RandomHealthCheck(logger) | ||
{ | ||
public SubtractRandomHealthCheck(ILogger<SubtractRandomHealthCheck> logger) | ||
: base(logger) | ||
{ | ||
} | ||
} |
19 changes: 6 additions & 13 deletions
19
src/Samples/Sample.CircuitBreaker.HealthCheck/IntermittentMessagePublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Models; | ||
|
||
public record Add(int a, int b); | ||
public record Add(int A, int B); |
2 changes: 1 addition & 1 deletion
2
src/Samples/Sample.CircuitBreaker.HealthCheck/Models/Subtract.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Models; | ||
|
||
public record Subtract(int a, int b); | ||
public record Subtract(int A, int B); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/SlimMessageBus.Host.CircuitBreaker.HealthCheck/Config/AbstractConsumerProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SlimMessageBus.Host.CircuitBreaker.HealthCheck; | ||
|
||
static internal class AbstractConsumerProperties | ||
{ | ||
static readonly internal ProviderExtensionProperty<bool> IsPaused = new("CircuitBreaker_IsPaused"); | ||
static readonly internal ProviderExtensionProperty<List<IConsumerCircuitBreaker>?> Breakers = new("CircuitBreaker_Breakers"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/SlimMessageBus.Host.CircuitBreaker.HealthCheck/Config/ConsumerSettingsProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace SlimMessageBus.Host.CircuitBreaker.HealthCheck; | ||
|
||
static internal class ConsumerSettingsProperties | ||
{ | ||
/// <summary> | ||
/// <see cref="IConsumerCircuitBreaker"/> to be used with the consumer. | ||
/// </summary> | ||
static readonly internal ProviderExtensionProperty<TypeCollection<IConsumerCircuitBreaker>> CircuitBreakerTypes = new("CircuitBreakerTypes"); | ||
|
||
static readonly internal ProviderExtensionProperty<Dictionary<string, HealthStatus>> HealthStatusTags = new("CircuitBreaker_HealthStatusTags"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/SlimMessageBus.Host.CircuitBreaker.HealthCheck/HealthCheckCircuitBreaker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...s.Host.CircuitBreaker.HealthCheck/HealthCheckCircuitBreakerAbstractConsumerInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
namespace SlimMessageBus.Host.CircuitBreaker.HealthCheck; | ||
|
||
internal sealed class HealthCheckCircuitBreakerAbstractConsumerInterceptor : IAbstractConsumerInterceptor | ||
{ | ||
public int Order => 100; | ||
|
||
public async Task<bool> CanStart(AbstractConsumer consumer) | ||
{ | ||
var breakerTypes = consumer.Settings.SelectMany(x => x.GetOrDefault(ConsumerSettingsProperties.CircuitBreakerTypes, [])).ToHashSet(); | ||
if (breakerTypes.Count == 0) | ||
{ | ||
// no breakers, allow to pass | ||
return true; | ||
} | ||
|
||
var breakers = consumer.GetOrCreate(AbstractConsumerProperties.Breakers, () => [])!; | ||
|
||
async Task BreakerChanged(Circuit state) | ||
{ | ||
if (!consumer.IsStarted) | ||
{ | ||
return; | ||
} | ||
|
||
var isPaused = consumer.IsPaused(); | ||
var shouldPause = state == Circuit.Closed || breakers.Exists(x => x.State == Circuit.Closed); | ||
if (shouldPause != isPaused) | ||
{ | ||
var settings = consumer.Settings.Count > 0 ? consumer.Settings[0] : null; | ||
var path = settings?.Path ?? "[unknown path]"; | ||
var bus = settings?.MessageBusSettings?.Name ?? "default"; | ||
if (shouldPause) | ||
{ | ||
consumer.Logger.LogWarning("Circuit breaker tripped for '{Path}' on '{Bus}' bus. Consumer paused.", path, bus); | ||
await consumer.DoStop().ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
consumer.Logger.LogInformation("Circuit breaker restored for '{Path}' on '{Bus}' bus. Consumer resumed.", path, bus); | ||
await consumer.DoStart().ConfigureAwait(false); | ||
} | ||
consumer.SetIsPaused(shouldPause); | ||
} | ||
} | ||
|
||
var sp = consumer.Settings.Select(x => x.MessageBusSettings.ServiceProvider).First(x => x != null); | ||
foreach (var breakerType in breakerTypes) | ||
{ | ||
var breaker = (IConsumerCircuitBreaker)ActivatorUtilities.CreateInstance(sp, breakerType, consumer.Settings); | ||
breakers.Add(breaker); | ||
|
||
await breaker.Subscribe(BreakerChanged); | ||
} | ||
|
||
var isPaused = breakers.Exists(x => x.State == Circuit.Closed); | ||
consumer.SetIsPaused(isPaused); | ||
return !isPaused; | ||
} | ||
|
||
public async Task<bool> CanStop(AbstractConsumer consumer) | ||
{ | ||
var breakers = consumer.GetOrDefault(AbstractConsumerProperties.Breakers, null); | ||
if (breakers == null || breakers.Count == 0) | ||
{ | ||
// no breakers, allow to pass | ||
return true; | ||
} | ||
|
||
foreach (var breaker in breakers) | ||
{ | ||
breaker.Unsubscribe(); | ||
|
||
if (breaker is IAsyncDisposable asyncDisposable) | ||
{ | ||
await asyncDisposable.DisposeAsync(); | ||
} | ||
else if (breaker is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
breakers.Clear(); | ||
|
||
return !consumer.IsPaused(); | ||
} | ||
} | ||
|
||
internal static class AbstractConsumerExtensions | ||
{ | ||
public static bool IsPaused(this AbstractConsumer consumer) => consumer.GetOrDefault(AbstractConsumerProperties.IsPaused, false); | ||
public static void SetIsPaused(this AbstractConsumer consumer, bool isPaused) => AbstractConsumerProperties.IsPaused.Set(consumer, isPaused); | ||
} |
Oops, something went wrong.