Skip to content

Commit dbd04c6

Browse files
committed
Improve license validation logic and exception handling
- Refactored license check to use `CancellationTokenSource` for a more robust timeout mechanism. - Simplified loop logic by removing redundant variables. - Replaced `NotSupportedException` with `InvalidOperationException` for better error clarity.
1 parent 1fa9f5d commit dbd04c6

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/LicenseStatusCheck.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace ServiceControl.Audit.Persistence.RavenDB;
33
using System;
44
using System.Net.Http;
55
using System.Net.Http.Json;
6-
using System.Text.Json;
76
using System.Threading;
87
using System.Threading.Tasks;
98

@@ -13,29 +12,29 @@ record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool E
1312

1413
public static async Task WaitForLicenseOrThrow(DatabaseConfiguration configuration, CancellationToken cancellationToken)
1514
{
16-
using var client = new HttpClient() { BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl) };
17-
var licenseCorrectlySetup = false;
18-
var attempts = 0;
19-
while (!licenseCorrectlySetup)
15+
using var client = new HttpClient
16+
{
17+
BaseAddress = new Uri(configuration.ServerConfiguration.ConnectionString ?? configuration.ServerConfiguration.ServerUrl)
18+
};
19+
using var cts = new CancellationTokenSource(30_000);
20+
21+
while (!cts.IsCancellationRequested)
2022
{
2123
var httpResponse = await client.GetAsync("license/status", cancellationToken);
2224
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
2325
if (licenseStatus.Expired)
2426
{
25-
throw new NotSupportedException("The current RavenDB license is expired. Please, contact support");
27+
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
2628
}
2729

2830
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
2931
{
30-
licenseCorrectlySetup = true;
31-
}
32-
33-
if (++attempts > 10)
34-
{
35-
throw new NotSupportedException("Cannot validate the current RavenDB license. Please, contact support");
32+
return;
3633
}
3734

38-
await Task.Delay(500, cancellationToken);
35+
await Task.Delay(200, cancellationToken);
3936
}
37+
38+
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
4039
}
4140
}

src/ServiceControl.Persistence.RavenDB/LicenseStatusCheck.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace ServiceControl.Persistence.RavenDB;
33
using System;
44
using System.Net.Http;
55
using System.Net.Http.Json;
6-
using System.Text.Json;
76
using System.Threading;
87
using System.Threading.Tasks;
98

@@ -13,29 +12,29 @@ record LicenseStatusFragment(string Id, string LicensedTo, string Status, bool E
1312

1413
public static async Task WaitForLicenseOrThrow(RavenPersisterSettings configuration, CancellationToken cancellationToken)
1514
{
16-
using var client = new HttpClient() { BaseAddress = new Uri(configuration.ConnectionString ?? configuration.ServerUrl) };
17-
var licenseCorrectlySetup = false;
18-
var attempts = 0;
19-
while (!licenseCorrectlySetup)
15+
using var client = new HttpClient
16+
{
17+
BaseAddress = new Uri(configuration.ConnectionString ?? configuration.ServerUrl)
18+
};
19+
using var cts = new CancellationTokenSource(30_000);
20+
21+
while (!cts.IsCancellationRequested)
2022
{
2123
var httpResponse = await client.GetAsync("license/status", cancellationToken);
2224
var licenseStatus = await httpResponse.Content.ReadFromJsonAsync<LicenseStatusFragment>(cancellationToken);
2325
if (licenseStatus.Expired)
2426
{
25-
throw new NotSupportedException("The current RavenDB license is expired. Please, contact support");
27+
throw new InvalidOperationException("The current RavenDB license is expired. Please, contact support");
2628
}
2729

2830
if (licenseStatus.LicensedTo != null && licenseStatus.Id != null)
2931
{
30-
licenseCorrectlySetup = true;
31-
}
32-
33-
if (++attempts > 10)
34-
{
35-
throw new NotSupportedException("Cannot validate the current RavenDB license. Please, contact support");
32+
return;
3633
}
3734

38-
await Task.Delay(500, cancellationToken);
35+
await Task.Delay(200, cancellationToken);
3936
}
37+
38+
throw new InvalidOperationException("Cannot validate the current RavenDB license. Please, contact support");
4039
}
4140
}

0 commit comments

Comments
 (0)