Skip to content

Commit 67fb0ec

Browse files
author
rhamlett_microsoft
committed
Added Azure detection for better cloud compatibility.
1 parent 663695f commit 67fb0ec

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/PerfProblemSimulator/Services/LatencyProbeService.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,19 @@ private void ProbeLoop(object? state)
131131

132132
// Get the base URL (may need to refresh after server starts)
133133
var baseUrl = _baseUrl ?? GetProbeBaseUrl();
134-
_logger.LogInformation("Latency probe targeting: {BaseUrl}/api/health/probe", baseUrl);
134+
var isAzure = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME"));
135+
_logger.LogInformation("Latency probe targeting: {BaseUrl}/api/health/probe (Azure: {IsAzure})", baseUrl, isAzure);
135136

136-
// Create a handler that doesn't pool connections to avoid socket reuse issues
137+
// Create a handler configured for the environment
137138
var handler = new SocketsHttpHandler
138139
{
139-
PooledConnectionLifetime = TimeSpan.Zero,
140-
PooledConnectionIdleTimeout = TimeSpan.Zero,
141-
ConnectTimeout = TimeSpan.FromSeconds(5)
140+
// For Azure, allow connection pooling for better performance
141+
// For local, disable pooling to avoid socket reuse issues
142+
PooledConnectionLifetime = isAzure ? TimeSpan.FromMinutes(2) : TimeSpan.Zero,
143+
PooledConnectionIdleTimeout = isAzure ? TimeSpan.FromMinutes(1) : TimeSpan.Zero,
144+
ConnectTimeout = TimeSpan.FromSeconds(10),
145+
// Enable automatic decompression
146+
AutomaticDecompression = System.Net.DecompressionMethods.All
142147
};
143148

144149
using var httpClient = new HttpClient(handler)
@@ -147,6 +152,9 @@ private void ProbeLoop(object? state)
147152
Timeout = TimeSpan.FromMilliseconds(RequestTimeoutMs)
148153
};
149154

155+
// Add a user agent for Azure (some proxies require it)
156+
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("LatencyProbe/1.0");
157+
150158
while (!cancellationToken.IsCancellationRequested)
151159
{
152160
try
@@ -172,7 +180,6 @@ private void ProbeLoop(object? state)
172180
}
173181
}
174182
}
175-
176183
/// <summary>
177184
/// Measures latency to the probe endpoint.
178185
/// </summary>
@@ -253,7 +260,25 @@ private void BroadcastLatency(LatencyMeasurement measurement)
253260
/// </summary>
254261
private string GetProbeBaseUrl()
255262
{
256-
// Try to get the actual server addresses from IServer
263+
// Check if running in Azure App Service
264+
var websiteHostname = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
265+
if (!string.IsNullOrEmpty(websiteHostname))
266+
{
267+
// Running in Azure App Service - use the public hostname
268+
// Azure provides HTTPS by default
269+
_logger.LogInformation("Detected Azure App Service environment: {Hostname}", websiteHostname);
270+
return $"https://{websiteHostname}";
271+
}
272+
273+
// Check if running in a container with a custom hostname
274+
var containerHostname = Environment.GetEnvironmentVariable("CONTAINER_APP_HOSTNAME");
275+
if (!string.IsNullOrEmpty(containerHostname))
276+
{
277+
_logger.LogInformation("Detected Container Apps environment: {Hostname}", containerHostname);
278+
return $"https://{containerHostname}";
279+
}
280+
281+
// Try to get the actual server addresses from IServer (works for local development)
257282
try
258283
{
259284
var addressFeature = _server.Features.Get<IServerAddressesFeature>();

0 commit comments

Comments
 (0)