33
44namespace 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