Skip to content

Commit 35d500f

Browse files
Update MemoryInformationRetriever to use the DocumentStore already configured HTTP Client (#5275)
1 parent 7e323d8 commit 35d500f

2 files changed

Lines changed: 10 additions & 18 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ namespace ServiceControl.Audit.Persistence.RavenDB;
66
using System.Threading;
77
using System.Threading.Tasks;
88

9-
class MemoryInformationRetriever(DatabaseConfiguration databaseConfiguration)
9+
class MemoryInformationRetriever(IRavenDocumentStoreProvider documentStoreProvider)
1010
{
11-
// Connection string is composed of the server URL. The ?? operator is needed because ServerUrl
12-
// is populated when running embedded and connection string when running in external mode.
13-
// However, the tricky part is that when tests are run they behave like if it was external mode.
14-
// Only one of ConnectionString and ServerUrl will be non-null, so we'll check ConnectionString first
15-
// to be consistent with the error instance implementation, where ServerUrl always has a value.
16-
readonly HttpClient client = new() { BaseAddress = new Uri(databaseConfiguration.ServerConfiguration.ConnectionString ?? databaseConfiguration.ServerConfiguration.ServerUrl) };
17-
1811
record ResponseDto
1912
{
2013
public MemoryInformation MemoryInformation { get; set; }
@@ -28,7 +21,10 @@ record MemoryInformation
2821

2922
public async Task<(bool IsHighDirty, string DirtyMemory)> GetMemoryInformation(CancellationToken cancellationToken = default)
3023
{
31-
var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken);
24+
var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken);
25+
var client = documentStore.GetRequestExecutor().HttpClient;
26+
var requestUrl = documentStore.Urls[0].TrimEnd('/') + "/admin/debug/memory/stats?includeThreads=false&includeMappings=false";
27+
var httpResponse = await client.GetAsync(requestUrl, cancellationToken);
3228
var responseDto = JsonSerializer.Deserialize<ResponseDto>(await httpResponse.Content.ReadAsStringAsync(cancellationToken));
3329

3430
return responseDto.MemoryInformation is null

src/ServiceControl.Persistence.RavenDB/MemoryInformationRetriever.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ namespace ServiceControl.Persistence.RavenDB;
66
using System.Threading;
77
using System.Threading.Tasks;
88

9-
class MemoryInformationRetriever(RavenPersisterSettings persisterSettings)
9+
class MemoryInformationRetriever(IRavenDocumentStoreProvider documentStoreProvider)
1010
{
11-
// Connection string is composed of the server URL. The ?? operator is needed because ServerUrl
12-
// is populated when running embedded and connection string when running in external mode.
13-
// However, the tricky part is that when tests are run they behave like if it was external mode.
14-
// ServerUrl is always populated by the persister settings, hence the code first checks for the
15-
// presence of a connection string, and if null, falls back to ServerUrl
16-
readonly HttpClient client = new() { BaseAddress = new Uri(persisterSettings.ConnectionString ?? persisterSettings.ServerUrl) };
17-
1811
record ResponseDto
1912
{
2013
public MemoryInformation MemoryInformation { get; set; }
@@ -28,7 +21,10 @@ record MemoryInformation
2821

2922
public async Task<(bool IsHighDirty, string DirtyMemory)> GetMemoryInformation(CancellationToken cancellationToken = default)
3023
{
31-
var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken);
24+
var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken);
25+
var client = documentStore.GetRequestExecutor().HttpClient;
26+
var requestUrl = documentStore.Urls[0].TrimEnd('/') + "/admin/debug/memory/stats?includeThreads=false&includeMappings=false";
27+
var httpResponse = await client.GetAsync(requestUrl, cancellationToken);
3228
var responseDto = JsonSerializer.Deserialize<ResponseDto>(await httpResponse.Content.ReadAsStringAsync(cancellationToken));
3329

3430
return responseDto.MemoryInformation is null

0 commit comments

Comments
 (0)