@@ -50,6 +50,7 @@ import (
50
50
"github.com/uber/cadence/common/messaging"
51
51
"github.com/uber/cadence/common/metrics"
52
52
"github.com/uber/cadence/common/persistence"
53
+ "github.com/uber/cadence/common/quotas"
53
54
"github.com/uber/cadence/common/service"
54
55
"github.com/uber/cadence/common/tokenbucket"
55
56
"github.com/uber/cadence/service/worker/archiver"
72
73
tokenSerializer common.TaskTokenSerializer
73
74
metricsClient metrics.Client
74
75
startWG sync.WaitGroup
75
- rateLimiter tokenbucket. TokenBucket
76
+ rateLimiter quotas. Policy
76
77
config * Config
77
78
blobstoreClient blobstore.Client
78
79
versionChecker * versionChecker
@@ -165,7 +166,7 @@ func NewWorkflowHandler(sVice service.Service, config *Config, metadataMgr persi
165
166
tokenSerializer : common .NewJSONTaskTokenSerializer (),
166
167
metricsClient : sVice .GetMetricsClient (),
167
168
domainCache : cache .NewDomainCache (metadataMgr , sVice .GetClusterMetadata (), sVice .GetMetricsClient (), sVice .GetLogger ()),
168
- rateLimiter : tokenbucket .NewDynamicTokenBucket (config .RPS , clock .NewRealTimeSource ()),
169
+ rateLimiter : quotas . NewSimpleRateLimiter ( tokenbucket .NewDynamicTokenBucket (config .RPS , clock .NewRealTimeSource () )),
169
170
blobstoreClient : blobstoreClient ,
170
171
versionChecker : & versionChecker {checkVersion : config .EnableClientVersionCheck ()},
171
172
domainHandler : newDomainHandler (
@@ -341,7 +342,7 @@ func (wh *WorkflowHandler) PollForActivityTask(
341
342
return nil , wh .error (errRequestNotSet , scope )
342
343
}
343
344
344
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
345
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
345
346
return nil , wh .error (createServiceBusyError (), scope )
346
347
}
347
348
@@ -420,7 +421,7 @@ func (wh *WorkflowHandler) PollForDecisionTask(
420
421
return nil , wh .error (errRequestNotSet , scope )
421
422
}
422
423
423
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
424
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
424
425
return nil , wh .error (createServiceBusyError (), scope )
425
426
}
426
427
@@ -553,7 +554,7 @@ func (wh *WorkflowHandler) RecordActivityTaskHeartbeat(
553
554
}
554
555
555
556
// Count the request in the RPS, but we still accept it even if RPS is exceeded
556
- wh .rateLimiter .TryConsume ( 1 )
557
+ wh .rateLimiter .Allow ( )
557
558
558
559
wh .Service .GetLogger ().Debug ("Received RecordActivityTaskHeartbeat" )
559
560
if heartbeatRequest .TaskToken == nil {
@@ -634,7 +635,7 @@ func (wh *WorkflowHandler) RecordActivityTaskHeartbeatByID(
634
635
}
635
636
636
637
// Count the request in the RPS, but we still accept it even if RPS is exceeded
637
- wh .rateLimiter .TryConsume ( 1 )
638
+ wh .rateLimiter .Allow ( )
638
639
639
640
wh .Service .GetLogger ().Debug ("Received RecordActivityTaskHeartbeatByID" )
640
641
domainID , err := wh .domainCache .GetDomainID (heartbeatRequest .GetDomain ())
@@ -740,7 +741,7 @@ func (wh *WorkflowHandler) RespondActivityTaskCompleted(
740
741
}
741
742
742
743
// Count the request in the RPS, but we still accept it even if RPS is exceeded
743
- wh .rateLimiter .TryConsume ( 1 )
744
+ wh .rateLimiter .Allow ( )
744
745
745
746
if completeRequest .TaskToken == nil {
746
747
return wh .error (errTaskTokenNotSet , scope )
@@ -822,7 +823,7 @@ func (wh *WorkflowHandler) RespondActivityTaskCompletedByID(
822
823
}
823
824
824
825
// Count the request in the RPS, but we still accept it even if RPS is exceeded
825
- wh .rateLimiter .TryConsume ( 1 )
826
+ wh .rateLimiter .Allow ( )
826
827
827
828
domainID , err := wh .domainCache .GetDomainID (completeRequest .GetDomain ())
828
829
if err != nil {
@@ -930,7 +931,7 @@ func (wh *WorkflowHandler) RespondActivityTaskFailed(
930
931
}
931
932
932
933
// Count the request in the RPS, but we still accept it even if RPS is exceeded
933
- wh .rateLimiter .TryConsume ( 1 )
934
+ wh .rateLimiter .Allow ( )
934
935
935
936
if failedRequest .TaskToken == nil {
936
937
return wh .error (errTaskTokenNotSet , scope )
@@ -1001,7 +1002,7 @@ func (wh *WorkflowHandler) RespondActivityTaskFailedByID(
1001
1002
}
1002
1003
1003
1004
// Count the request in the RPS, but we still accept it even if RPS is exceeded
1004
- wh .rateLimiter .TryConsume ( 1 )
1005
+ wh .rateLimiter .Allow ( )
1005
1006
1006
1007
domainID , err := wh .domainCache .GetDomainID (failedRequest .GetDomain ())
1007
1008
if err != nil {
@@ -1097,7 +1098,7 @@ func (wh *WorkflowHandler) RespondActivityTaskCanceled(
1097
1098
}
1098
1099
1099
1100
// Count the request in the RPS, but we still accept it even if RPS is exceeded
1100
- wh .rateLimiter .TryConsume ( 1 )
1101
+ wh .rateLimiter .Allow ( )
1101
1102
1102
1103
if cancelRequest .TaskToken == nil {
1103
1104
return wh .error (errTaskTokenNotSet , scope )
@@ -1180,7 +1181,7 @@ func (wh *WorkflowHandler) RespondActivityTaskCanceledByID(
1180
1181
}
1181
1182
1182
1183
// Count the request in the RPS, but we still accept it even if RPS is exceeded
1183
- wh .rateLimiter .TryConsume ( 1 )
1184
+ wh .rateLimiter .Allow ( )
1184
1185
1185
1186
domainID , err := wh .domainCache .GetDomainID (cancelRequest .GetDomain ())
1186
1187
if err != nil {
@@ -1287,7 +1288,7 @@ func (wh *WorkflowHandler) RespondDecisionTaskCompleted(
1287
1288
}
1288
1289
1289
1290
// Count the request in the RPS, but we still accept it even if RPS is exceeded
1290
- wh .rateLimiter .TryConsume ( 1 )
1291
+ wh .rateLimiter .Allow ( )
1291
1292
1292
1293
if completeRequest .TaskToken == nil {
1293
1294
return nil , wh .error (errTaskTokenNotSet , scope )
@@ -1365,7 +1366,7 @@ func (wh *WorkflowHandler) RespondDecisionTaskFailed(
1365
1366
}
1366
1367
1367
1368
// Count the request in the RPS, but we still accept it even if RPS is exceeded
1368
- wh .rateLimiter .TryConsume ( 1 )
1369
+ wh .rateLimiter .Allow ( )
1369
1370
1370
1371
if failedRequest .TaskToken == nil {
1371
1372
return wh .error (errTaskTokenNotSet , scope )
@@ -1435,7 +1436,7 @@ func (wh *WorkflowHandler) RespondQueryTaskCompleted(
1435
1436
}
1436
1437
1437
1438
// Count the request in the RPS, but we still accept it even if RPS is exceeded
1438
- wh .rateLimiter .TryConsume ( 1 )
1439
+ wh .rateLimiter .Allow ( )
1439
1440
1440
1441
if completeRequest .TaskToken == nil {
1441
1442
return wh .error (errTaskTokenNotSet , scope )
@@ -1487,7 +1488,7 @@ func (wh *WorkflowHandler) StartWorkflowExecution(
1487
1488
return nil , wh .error (errRequestNotSet , scope )
1488
1489
}
1489
1490
1490
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
1491
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
1491
1492
return nil , wh .error (createServiceBusyError (), scope )
1492
1493
}
1493
1494
@@ -1630,7 +1631,7 @@ func (wh *WorkflowHandler) GetWorkflowExecutionHistory(
1630
1631
return nil , wh .error (errRequestNotSet , scope )
1631
1632
}
1632
1633
1633
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
1634
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
1634
1635
return nil , wh .error (createServiceBusyError (), scope )
1635
1636
}
1636
1637
@@ -1832,7 +1833,7 @@ func (wh *WorkflowHandler) SignalWorkflowExecution(ctx context.Context,
1832
1833
return wh .error (errRequestNotSet , scope )
1833
1834
}
1834
1835
1835
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
1836
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
1836
1837
return wh .error (createServiceBusyError (), scope )
1837
1838
}
1838
1839
@@ -1911,7 +1912,7 @@ func (wh *WorkflowHandler) SignalWithStartWorkflowExecution(ctx context.Context,
1911
1912
return nil , wh .error (errRequestNotSet , scope )
1912
1913
}
1913
1914
1914
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
1915
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
1915
1916
return nil , wh .error (createServiceBusyError (), scope )
1916
1917
}
1917
1918
@@ -2069,7 +2070,7 @@ func (wh *WorkflowHandler) TerminateWorkflowExecution(ctx context.Context,
2069
2070
return wh .error (errRequestNotSet , scope )
2070
2071
}
2071
2072
2072
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2073
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2073
2074
return wh .error (createServiceBusyError (), scope )
2074
2075
}
2075
2076
@@ -2114,7 +2115,7 @@ func (wh *WorkflowHandler) ResetWorkflowExecution(ctx context.Context,
2114
2115
return nil , wh .error (errRequestNotSet , scope )
2115
2116
}
2116
2117
2117
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2118
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2118
2119
return nil , wh .error (createServiceBusyError (), scope )
2119
2120
}
2120
2121
@@ -2159,7 +2160,7 @@ func (wh *WorkflowHandler) RequestCancelWorkflowExecution(
2159
2160
return wh .error (errRequestNotSet , scope )
2160
2161
}
2161
2162
2162
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2163
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2163
2164
return wh .error (createServiceBusyError (), scope )
2164
2165
}
2165
2166
@@ -2203,7 +2204,7 @@ func (wh *WorkflowHandler) ListOpenWorkflowExecutions(ctx context.Context,
2203
2204
return nil , wh .error (errRequestNotSet , scope )
2204
2205
}
2205
2206
2206
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2207
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2207
2208
return nil , wh .error (createServiceBusyError (), scope )
2208
2209
}
2209
2210
@@ -2310,7 +2311,7 @@ func (wh *WorkflowHandler) ListClosedWorkflowExecutions(ctx context.Context,
2310
2311
return nil , wh .error (errRequestNotSet , scope )
2311
2312
}
2312
2313
2313
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2314
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2314
2315
return nil , wh .error (createServiceBusyError (), scope )
2315
2316
}
2316
2317
@@ -2435,7 +2436,7 @@ func (wh *WorkflowHandler) ListWorkflowExecutions(ctx context.Context, listReque
2435
2436
return nil , wh .error (errRequestNotSet , scope )
2436
2437
}
2437
2438
2438
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2439
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2439
2440
return nil , wh .error (createServiceBusyError (), scope )
2440
2441
}
2441
2442
@@ -2495,7 +2496,7 @@ func (wh *WorkflowHandler) ScanWorkflowExecutions(ctx context.Context, listReque
2495
2496
return nil , wh .error (errRequestNotSet , scope )
2496
2497
}
2497
2498
2498
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2499
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2499
2500
return nil , wh .error (createServiceBusyError (), scope )
2500
2501
}
2501
2502
@@ -2555,7 +2556,7 @@ func (wh *WorkflowHandler) CountWorkflowExecutions(ctx context.Context, countReq
2555
2556
return nil , wh .error (errRequestNotSet , scope )
2556
2557
}
2557
2558
2558
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2559
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2559
2560
return nil , wh .error (createServiceBusyError (), scope )
2560
2561
}
2561
2562
@@ -2766,7 +2767,7 @@ func (wh *WorkflowHandler) DescribeWorkflowExecution(ctx context.Context, reques
2766
2767
return nil , wh .error (errRequestNotSet , scope )
2767
2768
}
2768
2769
2769
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2770
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2770
2771
return nil , wh .error (createServiceBusyError (), scope )
2771
2772
}
2772
2773
@@ -2811,7 +2812,7 @@ func (wh *WorkflowHandler) DescribeTaskList(ctx context.Context, request *gen.De
2811
2812
return nil , wh .error (errRequestNotSet , scope )
2812
2813
}
2813
2814
2814
- if ok , _ := wh .rateLimiter .TryConsume ( 1 ); ! ok {
2815
+ if ok := wh .rateLimiter .Allow ( ); ! ok {
2815
2816
return nil , wh .error (createServiceBusyError (), scope )
2816
2817
}
2817
2818
0 commit comments