Skip to content

Commit

Permalink
Merge pull request #1706 from bcgov/redis-lock
Browse files Browse the repository at this point in the history
Add initial redis lock implementation
  • Loading branch information
pbolduc authored Mar 27, 2024
2 parents 32d690c + 63c7568 commit c49ad66
Show file tree
Hide file tree
Showing 13 changed files with 548 additions and 51 deletions.
3 changes: 1 addition & 2 deletions src/backend/TrafficCourts/Citizen.Service/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public static void ConfigureApplication(this WebApplicationBuilder builder, Seri
builder.AddSerilog();
builder.AddOpenTelemetry(Diagnostics.Source, logger, options =>
{
options.AddSource(MassTransit.Logging.DiagnosticHeaders.DefaultListenerName)
.AddRedisInstrumentation();
options.AddSource(MassTransit.Logging.DiagnosticHeaders.DefaultListenerName);
}, meters: new string[] { "MassTransit", "ComsClient", "CitizenService" });

builder.Services.AddHttpContextAccessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.7" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.13" />
<PackageReference Include="Refit.HttpClientFactory" Version="7.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.7.17" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private static void AddTracing(WebApplicationBuilder builder, ActivitySource act
{
builder
.SetResourceBuilder(resourceBuilder)
.AddRedisInstrumentation()
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation(options => options.Filter = AspNetCoreRequestFilter)
.AddSource(activitySource.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.7.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.13" />
<PackageReference Include="Scrutor" Version="4.2.2" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
Expand Down
91 changes: 47 additions & 44 deletions src/backend/TrafficCourts/Staff.Service/Models/Lock.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
namespace TrafficCourts.Staff.Service.Models;

public class Lock
{
/// <summary>
/// The function that returns the current UTC date and time. Overriden for tests.
/// </summary>
private readonly Func<DateTimeOffset> _utcNow;

public Lock() : this(() => DateTimeOffset.UtcNow)
{
}

internal Lock(Func<DateTimeOffset> utcNow)
{
_utcNow = utcNow ?? throw new ArgumentNullException(nameof(utcNow));
CreatedAtUtc = _utcNow();
}

/// <summary>
/// Lock ID
/// </summary>
public string? LockId { get; set; }

/// <summary>
/// The ticket number associated with the dispute.
/// </summary>
public string TicketNumber { get; set; } = string.Empty;

/// <summary>
/// Username of the person who acquired the lock.
/// </summary>
public string Username { get; set; } = string.Empty;

/// <summary>
/// The time in UTC when the acquired lock expires.
/// </summary>
public DateTimeOffset ExpiryTimeUtc { get; set; }

/// <summary>
/// The time in UTC when the lock was created.
/// </summary>
public DateTimeOffset CreatedAtUtc { get; }
}
using System;

namespace TrafficCourts.Staff.Service.Models;

public class Lock
{
private readonly TimeProvider _timeProvider;

public Lock() : this(TimeProvider.System)
{
}

internal Lock(TimeProvider timeProvider)
{
ArgumentNullException.ThrowIfNull(timeProvider);
CreatedAtUtc = timeProvider.GetUtcNow();
_timeProvider = timeProvider;
}

/// <summary>
/// Lock ID
/// </summary>
public string LockId { get; set; } = string.Empty;

/// <summary>
/// The ticket number associated with the dispute.
/// </summary>
public string TicketNumber { get; set; } = string.Empty;

/// <summary>
/// Username of the person who acquired the lock.
/// </summary>
public string Username { get; set; } = string.Empty;

/// <summary>
/// The time in UTC when the acquired lock expires.
/// </summary>
public DateTimeOffset ExpiryTimeUtc { get; set; }

/// <summary>
/// The time in UTC when the lock was created.
/// </summary>
public DateTimeOffset CreatedAtUtc { get; set; }

[System.Text.Json.Serialization.JsonIgnore]
public bool IsExpired => ExpiryTimeUtc < _timeProvider.GetUtcNow();
}
Loading

0 comments on commit c49ad66

Please sign in to comment.