Skip to content

Commit 5c161b8

Browse files
committed
add query metrics throttle
1 parent 89de7d9 commit 5c161b8

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/DurableTask.SqlServer.AzureFunctions/SqlMetricsProvider.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
namespace DurableTask.SqlServer.AzureFunctions
55
{
6+
using System;
67
using System.Threading;
78
using System.Threading.Tasks;
89

910
public class SqlMetricsProvider
1011
{
1112
readonly SqlOrchestrationService service;
13+
DateTime metricsTimeStamp = DateTime.MinValue;
14+
SqlScaleMetric? metrics;
1215

1316
public SqlMetricsProvider(SqlOrchestrationService service)
1417
{
@@ -17,13 +20,22 @@ public SqlMetricsProvider(SqlOrchestrationService service)
1720

1821
public virtual async Task<SqlScaleMetric> GetMetricsAsync(int? previousWorkerCount = null)
1922
{
20-
// GetRecommendedReplicaCountAsync will write a trace if the recommendation results
21-
// in a worker count that is different from the worker count we pass in as an argument.
22-
int recommendedReplicaCount = await this.service.GetRecommendedReplicaCountAsync(
23-
previousWorkerCount,
24-
CancellationToken.None);
23+
int recommendedReplicaCount;
2524

26-
return new SqlScaleMetric { RecommendedReplicaCount = recommendedReplicaCount };
25+
// We only want to query the metrics every 5 seconds.
26+
if (this.metrics == null || DateTime.UtcNow >= this.metricsTimeStamp.AddSeconds(5))
27+
{
28+
// GetRecommendedReplicaCountAsync will write a trace if the recommendation results
29+
// in a worker count that is different from the worker count we pass in as an argument.
30+
recommendedReplicaCount = await this.service.GetRecommendedReplicaCountAsync(
31+
previousWorkerCount,
32+
CancellationToken.None);
33+
34+
this.metricsTimeStamp = DateTime.UtcNow;
35+
this.metrics = new SqlScaleMetric { RecommendedReplicaCount = recommendedReplicaCount };
36+
}
37+
38+
return this.metrics;
2739
}
2840
}
2941
}

0 commit comments

Comments
 (0)