Skip to content

Commit e04a0f9

Browse files
committed
Make ThrottleDataHolder a singleton object
1 parent 0f156d4 commit e04a0f9

File tree

9 files changed

+44
-39
lines changed

9 files changed

+44
-39
lines changed

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/internal/ServiceReferenceHolder.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class ServiceReferenceHolder {
7171

7272
private ConfigurationContextService cfgCtxService;
7373
private APIManagerConfigurationService amConfigService;
74-
public ThrottleDataHolder throttleDataHolder;
74+
private final ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
7575
private ThrottleProperties throttleProperties;
7676
private ConfigurationContext axis2ConfigurationContext;
7777
private TracingService tracingService;
@@ -100,9 +100,7 @@ public class ServiceReferenceHolder {
100100

101101
private Set<String> activeTenants = new ConcurrentSkipListSet<>();
102102
private JedisPool redisPool;
103-
public void setThrottleDataHolder(ThrottleDataHolder throttleDataHolder) {
104-
this.throttleDataHolder = throttleDataHolder;
105-
}
103+
106104
public ThrottleDataHolder getThrottleDataHolder() {
107105
return throttleDataHolder;
108106
}

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/listeners/ServerStartupListener.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ public void completedServerStartup() {
4444
// This prevents errors in an All in one setup caused by the ThrottleDataPublisher trying to connect to the
4545
// event receiver, before the event receiver has been started on completion of server startup.
4646
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
47-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
48-
APIThrottleDataServiceImpl throttleDataServiceImpl =
49-
new APIThrottleDataServiceImpl(throttleDataHolder);
47+
APIThrottleDataServiceImpl throttleDataServiceImpl = new APIThrottleDataServiceImpl();
5048
CacheInvalidationService cacheInvalidationService = new CacheInvalidationServiceImpl();
5149
// Register APIThrottleDataService so that ThrottleData maps are available to other components.
5250
ServiceReferenceHolder.getInstance().setCacheInvalidationService(cacheInvalidationService);
5351
ServiceReferenceHolder.getInstance().setAPIThrottleDataService(throttleDataServiceImpl);
54-
ServiceReferenceHolder.getInstance().setThrottleDataHolder(throttleDataHolder);
5552
ServiceReferenceHolder.getInstance().setRevokedTokenService(new RevokedTokenDataImpl());
5653
SubscriptionsDataService subscriptionsDataService = new SubscriptionsDataServiceImpl();
5754
ServiceReferenceHolder.getInstance().setSubscriptionsDataService(subscriptionsDataService);

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/service/APIThrottleDataServiceImpl.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@
3232
*/
3333
public class APIThrottleDataServiceImpl implements APIThrottleDataService {
3434

35-
private ThrottleDataHolder throttleDataHolder;
36-
37-
public APIThrottleDataServiceImpl(ThrottleDataHolder throttleDataHolder) {
38-
39-
this.throttleDataHolder = throttleDataHolder;
40-
}
35+
private ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
4136

4237
@Override
4338
public void addThrottledApiConditions(String resourceKey, String name,

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/throttling/ThrottleDataHolder.java

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public void addThrottledAPIKey(String key, Long value){
7070
throttledAPIKeysMap.put(key,value);
7171
}
7272

73+
private static final ThrottleDataHolder instance = new ThrottleDataHolder();
74+
75+
private ThrottleDataHolder() {
76+
77+
}
78+
79+
public static ThrottleDataHolder getInstance() {
80+
return instance;
81+
}
82+
7383
public void addThrottledApiConditions(String key, String conditionKey, List<ConditionDto> conditionValue) {
7484

7585
Map<String, List<ConditionDto>> conditionMap;

components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandlerTest.java

+25-19
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void init() {
122122

123123
@Test
124124
public void testDoNotThrottleWhenMsgIsAResponseAndAuthCtxNotAvailable() {
125-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
125+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
126126

127127
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
128128

@@ -133,7 +133,7 @@ public void testDoNotThrottleWhenMsgIsAResponseAndAuthCtxNotAvailable() {
133133

134134
@Test
135135
public void testSubscriptionLevelThrottlingInitWhenThrottleCtxIsNull() {
136-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
136+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
137137

138138
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
139139

@@ -145,7 +145,7 @@ public void testSubscriptionLevelThrottlingInitWhenThrottleCtxIsNull() {
145145

146146
@Test
147147
public void testSubscriptionLevelThrottlingInitialization() {
148-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
148+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
149149

150150
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
151151

@@ -160,7 +160,7 @@ public void testSubscriptionLevelThrottlingInitialization() {
160160

161161
@Test
162162
public void testMsgThrottleOutWhenBlockingConditionsAreSatisfied() {
163-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
163+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
164164

165165
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
166166

@@ -181,7 +181,7 @@ public void testMsgThrottleOutWhenBlockingConditionsAreSatisfied() {
181181

182182
@Test
183183
public void testMsgThrottleContinueWhenAPITierIsNotAvailable() {
184-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
184+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
185185

186186
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
187187

@@ -196,7 +196,7 @@ public void testMsgThrottleContinueWhenAPITierIsNotAvailable() {
196196

197197
@Test
198198
public void testMsgDoContinueWhenAllThrottlingLevelsAreNotThrolled() {
199-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
199+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
200200
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
201201
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
202202
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -214,7 +214,7 @@ public void testMsgDoContinueWhenAllThrottlingLevelsAreNotThrolled() {
214214

215215
@Test
216216
public void testMsgDoThrottleWhenUserLevelThrottlingIsTriggerred() {
217-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
217+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
218218

219219
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
220220
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -233,7 +233,7 @@ public void testMsgDoThrottleWhenUserLevelThrottlingIsTriggerred() {
233233

234234
@Test
235235
public void testMsgThrottleOutWhenAPILevelIsThrottled() {
236-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
236+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
237237

238238
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
239239
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -261,7 +261,7 @@ public void testMsgThrottleOutWhenAPILevelIsThrottled() {
261261

262262
@Test
263263
public void testMsgThrottleOutWhenResourceLevelIsThrottled() {
264-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
264+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
265265

266266
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
267267
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -289,7 +289,7 @@ public void testMsgThrottleOutWhenResourceLevelIsThrottled() {
289289

290290
@Test
291291
public void testMsgThrottleOutWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsEnabled() {
292-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
292+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
293293

294294
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
295295
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -311,12 +311,13 @@ public void testMsgThrottleOutWhenSubscriptionLevelIsThrottledAndStopOnQuotaReac
311311
//Should throttle out and discontinue message flow, when subscription level is throttled out
312312
//and stop on quota reach is enabled
313313
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
314+
throttleDataHolder.removeThrottleData(subscriptionLevelThrottleKey);
314315

315316
}
316317

317318
@Test
318319
public void testMsgContinueWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsDisabled() {
319-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
320+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
320321

321322
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
322323
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -340,11 +341,12 @@ public void testMsgContinueWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIs
340341
//Though subscription level is throttled out, should continue the message flow, if stop on quota reach is
341342
//disabled
342343
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
344+
throttleDataHolder.removeThrottleData(subscriptionLevelThrottleKey);
343345
}
344346

345347
@Test
346348
public void testMsgThrottleOutWhenApplicationLevelIsThrottled() {
347-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
349+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
348350

349351
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
350352
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -365,11 +367,12 @@ public void testMsgThrottleOutWhenApplicationLevelIsThrottled() {
365367

366368
//Should discontinue message flow, when application level is throttled
367369
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
370+
throttleDataHolder.removeThrottleData(applicationLevelThrottleKey);
368371
}
369372

370373
@Test
371374
public void testMsgThrottleOutWhenProductionHardThrottlingLimitsThrottled() {
372-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
375+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
373376

374377
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
375378
accessInformation);
@@ -399,7 +402,7 @@ public void testMsgThrottleOutWhenProductionHardThrottlingLimitsThrottled() {
399402

400403
@Test
401404
public void testMsgThrottleOutWhenSandBoxHardThrottlingLimitsThrottled() {
402-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
405+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
403406

404407
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
405408
accessInformation);
@@ -438,7 +441,7 @@ public void testMsgThrottleOutWhenSandBoxHardThrottlingLimitsThrottled() {
438441

439442
@Test
440443
public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
441-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
444+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
442445

443446
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
444447
accessInformation);
@@ -465,11 +468,13 @@ public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
465468
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
466469
throttleDataHolder.removeKeyTemplate("testKeyTemplate");
467470
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
471+
throttleDataHolder.removeThrottleData("testKeyTemplate");
472+
throttleDataHolder.removeKeyTemplate("$user");
468473
}
469474

470475
@Test
471476
public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {
472-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
477+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
473478

474479
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
475480
accessInformation);
@@ -500,7 +505,7 @@ public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {
500505

501506
@Test
502507
public void testHandleResponse() {
503-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
508+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
504509

505510
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
506511
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
@@ -509,7 +514,7 @@ public void testHandleResponse() {
509514

510515
@Test
511516
public void testCheckForStaledThrottleData() {
512-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
517+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
513518
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
514519
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
515520
accessInformation);
@@ -533,11 +538,12 @@ public void testCheckForStaledThrottleData() {
533538
throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
534539
throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() - 10000);
535540
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
541+
throttleDataHolder.removeThrottleData("testKeyTemplate");
536542
}
537543

538544
@Test
539545
public void testMsgThrottleOutWithUserBlockingConditions() {
540-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
546+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
541547

542548
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
543549
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);

components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/internal/ServiceReferenceHolderTestCase.java

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public void testServiceReferenceHolder() {
4343

4444
serviceReferenceHolder.getThrottleDataHolder();
4545
ThrottleDataHolder throttleDataHolder = Mockito.mock(ThrottleDataHolder.class);
46-
serviceReferenceHolder.setThrottleDataHolder(throttleDataHolder);
4746

4847
ConfigurationContextService cfgCtxService = Mockito.mock(ConfigurationContextService.class);
4948
serviceReferenceHolder.setConfigurationContextService(cfgCtxService);

components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/throttling/ThrottleDataHolderTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ public class ThrottleDataHolderTest {
3030
public void addThrottleDataFromMap() throws Exception {
3131
Map<String,Long> map = new HashMap<>();
3232
map.put("/api/1.0.0",System.currentTimeMillis());
33-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
33+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
3434
throttleDataHolder.addThrottleDataFromMap(map);
3535
throttleDataHolder.removeThrottleData("/api/1.0.0");
3636
}
3737

3838

3939
@Test
4040
public void removeThrottledAPIKey() throws Exception {
41-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
41+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
4242
throttleDataHolder.addThrottledAPIKey("/api/1.0.0",System.currentTimeMillis());
4343
throttleDataHolder.removeThrottledAPIKey("/api/1.0.0");
4444
}
4545

4646

4747
@Test
4848
public void addBlockingCondition() throws Exception {
49-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
49+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
5050
throttleDataHolder.addAPIBlockingCondition("/api1/1.0.0","enabled");
5151
throttleDataHolder.removeAPIBlockingCondition("/api1/1.0.0");
5252
throttleDataHolder.addApplicationBlockingCondition("admin:DefaultApplication","enabled");

components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/throttling/util/BlockingConditionRetrieverTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void run() throws Exception {
6363
eventHubConfigurationDto.setPassword("admin".toCharArray());
6464
eventHubConfigurationDto.setEnabled(true);
6565
eventHubConfigurationDto.setServiceUrl("http://localhost:18083/internal/data/v1");
66-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
66+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
6767
BlockingConditionRetriever blockingConditionRetriever =
6868
new BlockingConditionRetrieverWrapper(eventHubConfigurationDto, throttleDataHolder);
6969
blockingConditionRetriever.run();

components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/throttling/util/KeyTemplateRetrieverTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void run() throws Exception {
6868
eventHubConfigurationDto.setPassword("admin".toCharArray());
6969
eventHubConfigurationDto.setEnabled(true);
7070
eventHubConfigurationDto.setServiceUrl("http://localhost:18084/internal/data/v1");
71-
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
71+
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
7272
KeyTemplateRetriever keyTemplateRetriever = new KeyTemplateRetrieverWrapper(eventHubConfigurationDto,
7373
throttleDataHolder);
7474
keyTemplateRetriever.run();

0 commit comments

Comments
 (0)