-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added EnableSubscriptionAutoHeal - see #27
- Loading branch information
Showing
7 changed files
with
79 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,67 @@ | ||
using Blumchen.Subscriber; | ||
using Blumchen.Subscriptions.Management; | ||
using Npgsql; | ||
using Npgsql.Replication; | ||
using Polly; | ||
|
||
namespace Blumchen.DependencyInjection; | ||
|
||
public record WorkerOptions(ResiliencePipeline ResiliencePipeline, ISubscriberOptions SubscriberOptions); | ||
public record WorkerOptions( | ||
ISubscriberOptions SubscriberOptions, | ||
ResiliencePipeline OuterPipeline, | ||
ResiliencePipeline InnerPipeline); | ||
|
||
public interface IWorkerOptionsBuilder | ||
{ | ||
IWorkerOptionsBuilder ResiliencyPipeline(ResiliencePipeline resiliencePipeline); | ||
IWorkerOptionsBuilder Subscription(Func<OptionsBuilder, OptionsBuilder>? builder); | ||
WorkerOptions Build(); | ||
IWorkerOptionsBuilder EnableSubscriptionAutoHeal(); | ||
} | ||
|
||
internal sealed class WorkerOptionsBuilder: IWorkerOptionsBuilder | ||
{ | ||
private ResiliencePipeline? _resiliencePipeline = default; | ||
private ResiliencePipeline? _outerPipeline = default; | ||
private Func<string, string, ResiliencePipeline>? _innerPipelineFn = default; | ||
private Func<OptionsBuilder, OptionsBuilder>? _builder; | ||
|
||
public IWorkerOptionsBuilder ResiliencyPipeline(ResiliencePipeline resiliencePipeline) | ||
{ | ||
_resiliencePipeline = resiliencePipeline; | ||
_outerPipeline = resiliencePipeline; | ||
return this; | ||
}public IWorkerOptionsBuilder Subscription(Func<OptionsBuilder, OptionsBuilder>? builder) | ||
{ | ||
_builder = builder; | ||
return this; | ||
return this; | ||
} | ||
|
||
public WorkerOptions Build() | ||
{ | ||
ArgumentNullException.ThrowIfNull(_resiliencePipeline); | ||
ArgumentNullException.ThrowIfNull(_outerPipeline); | ||
ArgumentNullException.ThrowIfNull(_builder); | ||
return new(_resiliencePipeline, _builder(new OptionsBuilder()).Build()); | ||
var subscriberOptions = _builder(new OptionsBuilder()).Build(); | ||
return new(subscriberOptions, _outerPipeline, | ||
_innerPipelineFn?.Invoke(subscriberOptions.ConnectionStringBuilder.ConnectionString,subscriberOptions.ReplicationOptions.SlotName) ?? | ||
ResiliencePipeline.Empty | ||
); | ||
} | ||
|
||
public IWorkerOptionsBuilder EnableSubscriptionAutoHeal() | ||
{ | ||
_innerPipelineFn = (replicationSlotName, connectionString) => new ResiliencePipelineBuilder().AddRetry(new() | ||
{ | ||
ShouldHandle = | ||
new PredicateBuilder().Handle<PostgresException>(exception => | ||
exception.SqlState.Equals("55000", StringComparison.OrdinalIgnoreCase)), | ||
MaxRetryAttempts = 1, | ||
OnRetry = async args => | ||
{ | ||
await using var conn = new LogicalReplicationConnection(connectionString); | ||
await conn.Open(args.Context.CancellationToken); | ||
await conn.ReCreate(replicationSlotName, args.Context.CancellationToken).ConfigureAwait(false); | ||
}, | ||
}).Build(); | ||
return this; | ||
} | ||
} | ||
|
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
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