Skip to content

Commit

Permalink
Have true RequestTimeouts and directly encode default lock time.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnml1135 committed Sep 4, 2024
1 parent 92fbec0 commit 2f8f44b
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ public class ServiceOptions
public const string Key = "Service";

public string ServiceId { get; set; } = "machine_api";
public TimeSpan ReadWriteLockTimeout { get; set; } = TimeSpan.FromSeconds(55);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
namespace Serval.Machine.Shared.Services;

public class DistributedReaderWriterLock(
string hostId,
IRepository<RWLock> locks,
IIdGenerator idGenerator,
string id,
TimeSpan defaultLifetime
) : IDistributedReaderWriterLock
public class DistributedReaderWriterLock(string hostId, IRepository<RWLock> locks, IIdGenerator idGenerator, string id)
: IDistributedReaderWriterLock
{
private readonly string _hostId = hostId;
private readonly IRepository<RWLock> _locks = locks;
private readonly IIdGenerator _idGenerator = idGenerator;
private readonly string _id = id;
private readonly TimeSpan _defaultLifetime = defaultLifetime;
private readonly TimeSpan _defaultLifetime = TimeSpan.FromSeconds(10);

public async Task<IAsyncDisposable> ReaderLockAsync(
TimeSpan? lifetime = default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ await _locks.InsertAsync(
// the lock is already made - no new one needs to be made
// This is done instead of checking if it exists first to prevent race conditions.
}
return new DistributedReaderWriterLock(
_serviceOptions.ServiceId,
_locks,
_idGenerator,
id,
_serviceOptions.ReadWriteLockTimeout
);
return new DistributedReaderWriterLock(_serviceOptions.ServiceId, _locks, _idGenerator, id);
}

public async Task<bool> DeleteAsync(string id, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ CancellationToken cancellationToken
if (engine is null)
throw new OperationCanceledException();

await using (await @lock.WriterLockAsync(cancellationToken: cancellationToken))
await using (
await @lock.WriterLockAsync(lifetime: TimeSpan.FromMinutes(5), cancellationToken: cancellationToken)
)
{
cancellationToken.ThrowIfCancellationRequested();
await smtModelTrainer.SaveAsync(CancellationToken.None);
Expand Down
6 changes: 6 additions & 0 deletions src/Serval/src/Serval.ApiServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddFeatureManagement();
services.AddRouting(o => o.LowercaseUrls = true);
services.AddRequestTimeouts(o =>
{
o.DefaultPolicy = new RequestTimeoutPolicy { Timeout = TimeSpan.FromSeconds(10) };
o.AddPolicy("LongRequest", TimeSpan.FromSeconds(55));
});
services.AddOutputCache(options =>
{
options.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(10);
Expand Down Expand Up @@ -215,6 +220,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthentication();

app.UseRouting();
app.UseRequestTimeouts();
app.UseOutputCache();
app.UseAuthorization();
app.UseEndpoints(x =>
Expand Down
1 change: 1 addition & 0 deletions src/Serval/src/Serval.ApiServer/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
global using MassTransit.Mediator;
global using Microsoft.AspNetCore.Authentication.JwtBearer;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Http.Timeouts;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.OutputCaching;
global using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ IUrlService urlService
/// <response code="503">A necessary service is currently unavailable. Check `/health` for more details.</response>
[Authorize(Scopes.ReadAssessmentEngines)]
[HttpGet]
[RequestTimeout("LongRequest")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
Expand Down
1 change: 1 addition & 0 deletions src/Serval/src/Serval.Assessment/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
global using MassTransit;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Http.Timeouts;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Routing;
global using Microsoft.Extensions.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ IUrlService urlService
/// <response code="503">A necessary service is currently unavailable. Check `/health` for more details. </response>
[Authorize(Scopes.ReadFiles)]
[HttpGet]
[RequestTimeout("LongRequest")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
Expand Down
1 change: 1 addition & 0 deletions src/Serval/src/Serval.DataFiles/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
global using MassTransit.Mediator;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Http.Timeouts;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Mvc.ModelBinding;
global using Microsoft.AspNetCore.Routing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ IUrlService urlService
/// <response code="503">A necessary service is currently unavailable. Check `/health` for more details.</response>
[Authorize(Scopes.ReadTranslationEngines)]
[HttpGet]
[RequestTimeout("LongRequest")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(void), StatusCodes.Status403Forbidden)]
Expand Down
1 change: 1 addition & 0 deletions src/Serval/src/Serval.Translation/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
global using MassTransit;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Http.Timeouts;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Routing;
global using Microsoft.Extensions.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class WebhooksController(IAuthorizationService authService, IWebhookServi
/// <response code="503">A necessary service is currently unavailable. Check `/health` for more details. </response>
[Authorize(Scopes.ReadHooks)]
[HttpGet]
[RequestTimeout("LongRequest")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status503ServiceUnavailable)]
public async Task<IEnumerable<WebhookDto>> GetAllAsync(CancellationToken cancellationToken)
Expand Down
1 change: 1 addition & 0 deletions src/Serval/src/Serval.Webhooks/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
global using MassTransit;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Http.Timeouts;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Routing;
global using Microsoft.Extensions.Options;
Expand Down

0 comments on commit 2f8f44b

Please sign in to comment.