Skip to content

Commit

Permalink
Startup changes for more production scenarios (#422)
Browse files Browse the repository at this point in the history
This PR fixes the startup of the EventBus to enable more production usage scenarios:
- Readiness check in the EventBus should only be done if enabled.
- Ensure `StartupDelay` is positive before using it in the EventBus.
- Remove limits on `StartupDelay` and it should be `null` by default to allow for really quick starts.
  • Loading branch information
mburumaxwell authored Jun 16, 2022
1 parent 42bf74d commit e3030ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/Tingle.EventBus/DependencyInjection/EventBusOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public class EventBusOptions
{
/// <summary>
/// The duration of time to delay the starting of the bus.
/// The default value is 5 seconds. Max value is 10 minutes and minimum is 5 seconds.
/// When <see langword="null"/>, the bus is started immediately.
/// </summary>
public TimeSpan? StartupDelay { get; set; } = TimeSpan.FromSeconds(5);
public TimeSpan? StartupDelay { get; set; }

/// <summary>
/// Gets the <see cref="EventBusReadinessOptions"/> for the Event Bus.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ public void PostConfigure(string name, EventBusOptions options)
options.Readiness.Timeout = TimeSpan.FromTicks(ticks);
}

// Check bounds for startup delay, if provided
if (options.StartupDelay != null)
{
ticks = options.StartupDelay.Value.Ticks;
ticks = Math.Max(ticks, TimeSpan.FromSeconds(5).Ticks); // must be more than 5 seconds
ticks = Math.Min(ticks, TimeSpan.FromMinutes(10).Ticks); // must be less than 10 minutes
options.StartupDelay = TimeSpan.FromTicks(ticks);
}

// Check bounds for duplicate detection duration, if duplicate detection is enabled
if (options.EnableDeduplication)
{
Expand Down
26 changes: 15 additions & 11 deletions src/Tingle.EventBus/EventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ public async Task CancelAsync<TEvent>(IList<string> ids, CancellationToken cance
public async Task StartAsync(CancellationToken cancellationToken)
{
// If a startup delay has been specified, apply it
if (options.StartupDelay != null)
var delay = options.StartupDelay;
if (delay != null && delay > TimeSpan.Zero)
{
// We cannot await the call because it will cause other components not to start.
// Instead, create a cancellation token linked to the one provided so that we can
// stop startup if told to do so.
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_ = DelayThenStartTransportsAsync(options.StartupDelay.Value, cts.Token);
_ = DelayThenStartTransportsAsync(delay.Value, cts.Token);
}
else
{
Expand Down Expand Up @@ -239,16 +240,19 @@ private async Task DelayThenStartTransportsAsync(TimeSpan delay, CancellationTok

private async Task StartTransportsAsync(CancellationToken cancellationToken)
{
try
{
// Perform readiness check before starting bus.
logger.StartupReadinessCheck();
await readinessProvider.WaitReadyAsync(cancellationToken: cancellationToken);
}
catch (Exception ex)
if (options.Readiness.Enabled)
{
logger.StartupReadinessCheckFailed(ex);
throw; // re-throw to prevent from getting healthy
try
{
// Perform readiness check before starting bus.
logger.StartupReadinessCheck();
await readinessProvider.WaitReadyAsync(cancellationToken: cancellationToken);
}
catch (Exception ex)
{
logger.StartupReadinessCheckFailed(ex);
throw; // re-throw to prevent from getting healthy
}
}

// Start the bus and its transports
Expand Down

0 comments on commit e3030ee

Please sign in to comment.