Repository navigation
#251 Health check circuit breaker #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zarusz
merged 1 commit into
zarusz:feature/v3_circuit_breaker
from
EtherZa:feature/251-health-check-circuit-breaker
Dec 25, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
src/Samples/Sample.CircuitBreaker.HealthCheck/Consumers/AddConsumer.cs
This file contains hidden or 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,17 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck.Consumers; | ||
|
|
||
| public class AddConsumer : IConsumer<Add> | ||
| { | ||
| private readonly ILogger<AddConsumer> _logger; | ||
|
|
||
| public AddConsumer(ILogger<AddConsumer> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public Task OnHandle(Add message, CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogInformation("{A} + {B} = {C}", message.a, message.b, message.a + message.b); | ||
| return Task.CompletedTask; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/Samples/Sample.CircuitBreaker.HealthCheck/Consumers/SubtractConsumer.cs
This file contains hidden or 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,17 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck.Consumers; | ||
|
|
||
| public class SubtractConsumer : IConsumer<Subtract> | ||
| { | ||
| private readonly ILogger<SubtractConsumer> _logger; | ||
|
|
||
| public SubtractConsumer(ILogger<SubtractConsumer> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public Task OnHandle(Subtract message, CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogInformation("{A} - {B} = {C}", message.a, message.b, message.a - message.b); | ||
| return Task.CompletedTask; | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/Samples/Sample.CircuitBreaker.HealthCheck/GlobalUsings.cs
This file contains hidden or 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,17 @@ | ||
| global using System.Net.Mime; | ||
| global using System.Reflection; | ||
|
|
||
| global using Microsoft.Extensions.Configuration; | ||
| global using Microsoft.Extensions.DependencyInjection; | ||
| global using Microsoft.Extensions.Hosting; | ||
| global using Microsoft.Extensions.Logging; | ||
|
|
||
| global using Sample.CircuitBreaker.HealthCheck.Consumers; | ||
| global using Sample.CircuitBreaker.HealthCheck.Models; | ||
|
|
||
| global using SecretStore; | ||
|
|
||
| global using SlimMessageBus; | ||
| global using SlimMessageBus.Host; | ||
| global using SlimMessageBus.Host.RabbitMQ; | ||
| global using SlimMessageBus.Host.Serialization.SystemTextJson; |
11 changes: 11 additions & 0 deletions
11
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/AddRandomHealthCheck.cs
This file contains hidden or 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 Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| public class AddRandomHealthCheck : RandomHealthCheck | ||
| { | ||
| public AddRandomHealthCheck(ILogger<AddRandomHealthCheck> logger) | ||
| : base(logger) | ||
| { | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/RandomHealthCheck.cs
This file contains hidden or 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,20 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
|
||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| public abstract class RandomHealthCheck : IHealthCheck | ||
| { | ||
| private readonly ILogger _logger; | ||
|
|
||
| protected RandomHealthCheck(ILogger logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
| { | ||
| var value = (HealthStatus)Random.Shared.Next(3); | ||
| _logger.LogInformation("{HealthCheck} evaluated as {HealthStatus}", this.GetType(), value); | ||
| return Task.FromResult(new HealthCheckResult(value, value.ToString())); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/SubtractRandomHealthCheck.cs
This file contains hidden or 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 Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| public class SubtractRandomHealthCheck : RandomHealthCheck | ||
| { | ||
| public SubtractRandomHealthCheck(ILogger<SubtractRandomHealthCheck> logger) | ||
| : base(logger) | ||
| { | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Samples/Sample.CircuitBreaker.HealthCheck/IntermittentMessagePublisher.cs
This file contains hidden or 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,28 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck; | ||
| public class IntermittentMessagePublisher : BackgroundService | ||
| { | ||
| private readonly ILogger _logger; | ||
| private readonly IMessageBus _messageBus; | ||
|
|
||
| public IntermittentMessagePublisher(ILogger<IntermittentMessagePublisher> logger, IMessageBus messageBus) | ||
| { | ||
| _logger = logger; | ||
| _messageBus = messageBus; | ||
| } | ||
|
|
||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| while (!stoppingToken.IsCancellationRequested) | ||
| { | ||
| var a = Random.Shared.Next(10); | ||
| var b = Random.Shared.Next(10); | ||
|
|
||
| //_logger.LogInformation("Emitting {A} +- {B} = ?", a, b); | ||
|
|
||
| await Task.WhenAll( | ||
| _messageBus.Publish(new Add(a, b)), | ||
| _messageBus.Publish(new Subtract(a, b)), | ||
| Task.Delay(1000, stoppingToken)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,3 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck.Models; | ||
|
|
||
| public record Add(int a, int b); |
3 changes: 3 additions & 0 deletions
3
src/Samples/Sample.CircuitBreaker.HealthCheck/Models/Subtract.cs
This file contains hidden or 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,3 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck.Models; | ||
|
|
||
| public record Subtract(int a, int b); |
This file contains hidden or 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,91 @@ | ||
| namespace Sample.CircuitBreaker.HealthCheck; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
|
||
| using Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
|
||
| using SlimMessageBus.Host.CircuitBreaker.HealthCheck.Config; | ||
|
|
||
| public static class Program | ||
| { | ||
| private static async Task Main(string[] args) | ||
| { | ||
| // Local file with secrets | ||
| Secrets.Load(@"..\..\..\..\..\secrets.txt"); | ||
|
|
||
| await Host.CreateDefaultBuilder(args) | ||
| .ConfigureServices((builder, services) => | ||
| { | ||
| const string AddTag = "add"; | ||
| const string SubtractTag = "subtract"; | ||
|
|
||
| services.AddSlimMessageBus(mbb => | ||
| { | ||
| var ticks = DateTimeOffset.UtcNow.Ticks; | ||
| var addTopic = $"Sample-CircuitBreaker-HealthCheck-add-{ticks}"; | ||
| var subtractTopic = $"Sample-CircuitBreaker-HealthCheck-subtract-{ticks}"; | ||
|
|
||
| mbb | ||
| .WithProviderRabbitMQ( | ||
| cfg => | ||
| { | ||
| cfg.ConnectionString = Secrets.Service.PopulateSecrets(builder.Configuration.GetValue<string>("RabbitMQ:ConnectionString")); | ||
| cfg.ConnectionFactory.ClientProvidedName = $"Sample_CircuitBreaker_HealthCheck_{Environment.MachineName}"; | ||
|
|
||
| cfg.UseMessagePropertiesModifier((m, p) => p.ContentType = MediaTypeNames.Application.Json); | ||
| cfg.UseExchangeDefaults(durable: false); | ||
| cfg.UseQueueDefaults(durable: false); | ||
| }); | ||
| mbb | ||
| .Produce<Add>(x => x | ||
| .Exchange(addTopic, exchangeType: ExchangeType.Fanout, autoDelete: false) | ||
| .RoutingKeyProvider((m, p) => Guid.NewGuid().ToString())) | ||
| .Consume<Add>( | ||
| cfg => | ||
| { | ||
| cfg | ||
| .Queue(nameof(Add), autoDelete: false) | ||
| .Path(nameof(Add)) | ||
| .ExchangeBinding(addTopic) | ||
| .WithConsumer<AddConsumer>() | ||
| .PauseOnDegradedHealthCheck(AddTag); | ||
| }); | ||
|
|
||
| mbb | ||
| .Produce<Subtract>(x => x | ||
| .Exchange(subtractTopic, exchangeType: ExchangeType.Fanout, autoDelete: false) | ||
| .RoutingKeyProvider((m, p) => Guid.NewGuid().ToString())) | ||
| .Consume<Subtract>( | ||
| cfg => | ||
| { | ||
| cfg | ||
| .Queue(nameof(Subtract), autoDelete: false) | ||
| .Path(nameof(Subtract)) | ||
| .ExchangeBinding(subtractTopic) | ||
| .WithConsumer<SubtractConsumer>() | ||
| .PauseOnUnhealthyCheck(SubtractTag); | ||
| }); | ||
|
|
||
| mbb.AddServicesFromAssembly(Assembly.GetExecutingAssembly()); | ||
| mbb.AddJsonSerializer(); | ||
| }); | ||
|
|
||
| services.AddHostedService<IntermittentMessagePublisher>(); | ||
| services.AddSingleton<AddRandomHealthCheck>(); | ||
| services.AddSingleton<SubtractRandomHealthCheck>(); | ||
|
|
||
| services.Configure<HealthCheckPublisherOptions>(cfg => | ||
| { | ||
| // aggressive to toggle health status often (sample only) | ||
| cfg.Delay = TimeSpan.FromSeconds(3); | ||
| cfg.Period = TimeSpan.FromSeconds(5); | ||
| }); | ||
|
|
||
| services | ||
| .AddHealthChecks() | ||
| .AddCheck<AddRandomHealthCheck>("Add", tags: [AddTag]) | ||
| .AddCheck<SubtractRandomHealthCheck>("Subtract", tags: [SubtractTag]); | ||
| }) | ||
| .Build() | ||
| .RunAsync(); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/Samples/Sample.CircuitBreaker.HealthCheck/Sample.CircuitBreaker.HealthCheck.csproj
This file contains hidden or 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,33 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.7" /> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\SlimMessageBus.Host.CircuitBreaker.HealthCheck\SlimMessageBus.Host.CircuitBreaker.HealthCheck.csproj" /> | ||
| <ProjectReference Include="..\..\SlimMessageBus.Host.RabbitMQ\SlimMessageBus.Host.RabbitMQ.csproj" /> | ||
| <ProjectReference Include="..\..\SlimMessageBus.Host\SlimMessageBus.Host.csproj" /> | ||
| <ProjectReference Include="..\..\SlimMessageBus.Host.Serialization.SystemTextJson\SlimMessageBus.Host.Serialization.SystemTextJson.csproj" /> | ||
| <ProjectReference Include="..\..\SlimMessageBus\SlimMessageBus.csproj" /> | ||
| <ProjectReference Include="..\..\Tools\SecretStore\SecretStore.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="appsettings.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| <ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
| <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
19 changes: 19 additions & 0 deletions
19
src/Samples/Sample.CircuitBreaker.HealthCheck/appsettings.json
This file contains hidden or 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,19 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft": "Warning", | ||
| "Microsoft.Hosting.Lifetime": "Information" | ||
| }, | ||
| "Console": { | ||
| "FormatterName": "simple", | ||
| "FormatterOptions": { | ||
| "SingleLine": true, | ||
| "TimestampFormat": "HH:mm:ss.fff " | ||
| } | ||
| } | ||
| }, | ||
| "RabbitMQ": { | ||
| "ConnectionString": "{{rabbitmq_connectionstring}}" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.