Skip to content

Commit 317cfbd

Browse files
committed
feat: added keepExecutions state to improve resource allocation
1 parent 0ba056e commit 317cfbd

File tree

10 files changed

+93
-39
lines changed

10 files changed

+93
-39
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ switcher.isItOn();
189189
Create chained calls to validate the switcher with a more readable and maintainable code.
190190

191191
```java
192-
import static **.MyAppFeatures.*;
192+
import static org.example.MyAppFeatures.*;
193193

194194
getSwitcher(FEATURE01)
195195
.checkValue("My value")
@@ -198,10 +198,22 @@ getSwitcher(FEATURE01)
198198
```
199199

200200
4. **Accessing the last SwitcherResult**
201-
Switchers stores the last execution result, which can be retrieved using the following operation.
201+
Switchers stores the last execution result, which can be retrieved using the following operation. Requires enabling executions history for each Switcher using:
202202

203203
```java
204-
switcher.getLastExecutionResult();
204+
getSwitcher(FEATURE01)
205+
.keepExecutions();
206+
.checkValue("My value")
207+
.checkNetwork("10.0.0.1")
208+
209+
switcher.isItOn();
210+
switcher.getLastExecutionResult(); // returns the last SwitcherResult
211+
```
212+
213+
Executions history can also be cleared using:
214+
215+
```java
216+
switcher.flushExecutions();
205217
```
206218

207219
5. **Throttling**

src/main/java/com/switcherapi/client/SwitcherContextBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public static SwitcherRequest getSwitcher(String key, boolean keepEntries) {
398398

399399
final SwitcherRequest switcher = switchers.get(key);
400400
if (!keepEntries) {
401-
switcher.flush();
401+
switcher.resetInputs();
402402
}
403403

404404
return switcher;

src/main/java/com/switcherapi/client/model/AsyncSwitcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public AsyncSwitcher(final Switcher switcher, long delay) {
4444

4545
/**
4646
* Validate if next run is ready to be performed, otherwise it will skip and delegate the
47-
* Switcher result for the Switcher history execution.
47+
* Switcher result for the Switcher executions map.
4848
*/
4949
public void execute() {
5050
SwitcherUtils.debug(logger, "nextRun: {} - currentTimeMillis: {}", nextRun, System.currentTimeMillis());
@@ -60,7 +60,7 @@ public void execute() {
6060
public void run() {
6161
try {
6262
final SwitcherResult response = switcher.executeCriteria();
63-
switcher.updateHistoryExecution(response);
63+
switcher.updateExecutions(response);
6464
} catch (SwitcherException e) {
6565
logger.error(e.getMessage(), e);
6666
}

src/main/java/com/switcherapi/client/model/Switcher.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
*/
2626
public interface Switcher {
2727

28+
/**
29+
* Clear executions from SwitcherRequest
30+
*
31+
* @return instance of SwitcherInterface
32+
*/
33+
Switcher flushExecutions();
34+
2835
/**
2936
* Prepare the Switcher including a list of inputs necessary to run the criteria afterward.
3037
*
@@ -77,11 +84,11 @@ public interface Switcher {
7784
SwitcherResult executeCriteria();
7885

7986
/**
80-
* Update the history of executions.
87+
* Update Switcher executions.
8188
*
8289
* @param response the response to be updated
8390
*/
84-
void updateHistoryExecution(SwitcherResult response);
91+
void updateExecutions(SwitcherResult response);
8592

8693
/**
8794
* Get the key of the switcher.

src/main/java/com/switcherapi/client/model/SwitcherBuilder.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public abstract class SwitcherBuilder implements Switcher {
2222

2323
protected boolean bypassMetrics;
2424

25+
protected boolean keepExecutions;
26+
2527
protected Boolean restrictRelay;
2628

2729
protected String defaultResult;
@@ -35,11 +37,14 @@ protected SwitcherBuilder(final SwitcherProperties properties) {
3537
}
3638

3739
/**
38-
* Clear all entries previously added and history of executions.
40+
* Clear all entries previously added
3941
*
4042
* @return {@link SwitcherBuilder}
4143
*/
42-
public abstract SwitcherBuilder flush();
44+
public SwitcherBuilder resetInputs() {
45+
this.entry.clear();
46+
return this;
47+
}
4348

4449
/**
4550
* Skip API calls given a delay time
@@ -48,6 +53,7 @@ protected SwitcherBuilder(final SwitcherProperties properties) {
4853
* @return {@link SwitcherBuilder}
4954
*/
5055
public SwitcherBuilder throttle(long delay) {
56+
this.keepExecutions();
5157
this.delay = delay;
5258
return this;
5359
}
@@ -187,6 +193,16 @@ public SwitcherBuilder bypassMetrics() {
187193
return this;
188194
}
189195

196+
/**
197+
* Keep executions in memory
198+
*
199+
* @return {@link SwitcherBuilder}
200+
*/
201+
public SwitcherBuilder keepExecutions() {
202+
this.keepExecutions = true;
203+
return this;
204+
}
205+
190206
public boolean isRemote() {
191207
return remote;
192208
}

src/main/java/com/switcherapi/client/model/SwitcherRequest.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public final class SwitcherRequest extends SwitcherBuilder {
2626

27-
private final ConcurrentHashMap<List<Entry>, SwitcherResult> historyExecution;
27+
private final ConcurrentHashMap<List<Entry>, SwitcherResult> executionsMap;
2828

2929
private final SwitcherExecutor switcherExecutor;
3030

@@ -45,7 +45,13 @@ public SwitcherRequest(final String switcherKey,
4545
super(switcherProperties);
4646
this.switcherExecutor = switcherExecutor;
4747
this.switcherKey = switcherKey;
48-
this.historyExecution = new ConcurrentHashMap<>();
48+
this.executionsMap = new ConcurrentHashMap<>();
49+
}
50+
51+
@Override
52+
public SwitcherRequest flushExecutions() {
53+
this.executionsMap.clear();
54+
return this;
4955
}
5056

5157
@Override
@@ -57,7 +63,7 @@ public SwitcherRequest prepareEntry(final List<Entry> entry) {
5763
@Override
5864
public SwitcherRequest prepareEntry(final Entry entry, final boolean add) {
5965
if (!add) {
60-
this.flush();
66+
this.resetInputs();
6167
}
6268

6369
this.entry.add(entry);
@@ -69,13 +75,6 @@ public SwitcherRequest prepareEntry(final Entry entry) {
6975
return this.prepareEntry(entry, false);
7076
}
7177

72-
@Override
73-
public SwitcherBuilder flush() {
74-
this.entry.clear();
75-
this.historyExecution.clear();
76-
return this;
77-
}
78-
7978
@Override
8079
public boolean isItOn() throws SwitcherException {
8180
final SwitcherResult response = submit();
@@ -94,14 +93,14 @@ public SwitcherResult submit() throws SwitcherException {
9493
}
9594

9695
asyncSwitcher.execute();
97-
final SwitcherResult response = getFromHistory();
96+
final SwitcherResult response = executionsMap.get(entry);
9897
if (Objects.nonNull(response)) {
9998
return response;
10099
}
101100
}
102101

103102
final SwitcherResult response = this.executeCriteria();
104-
this.updateHistoryExecution(response);
103+
this.updateExecutions(response);
105104
return response;
106105
}
107106

@@ -115,8 +114,10 @@ public SwitcherResult executeCriteria() {
115114
}
116115

117116
@Override
118-
public void updateHistoryExecution(final SwitcherResult response) {
119-
historyExecution.put(entry, response);
117+
public void updateExecutions(final SwitcherResult response) {
118+
if (super.keepExecutions) {
119+
executionsMap.put(entry, response);
120+
}
120121
}
121122

122123
@Override
@@ -131,19 +132,15 @@ public List<Entry> getEntry() {
131132

132133
@Override
133134
public SwitcherResult getLastExecutionResult() {
134-
return getFromHistory();
135+
return executionsMap.get(entry);
135136
}
136137

137138
public boolean isBypassMetrics() {
138139
return bypassMetrics;
139140
}
140141

141142
private boolean canUseAsync() {
142-
return super.delay > 0 && !this.historyExecution.isEmpty();
143-
}
144-
145-
private SwitcherResult getFromHistory() {
146-
return historyExecution.get(entry);
143+
return super.delay > 0 && !this.executionsMap.isEmpty();
147144
}
148145

149146
@Override

src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void shouldFlushStrategyInputs() {
9696

9797
//test
9898
switcherBuilder
99-
.flush()
99+
.resetInputs()
100100
.checkValue("anotherValue");
101101

102102
assertEquals(1, switcherBuilder.getEntry().size());

src/test/java/com/switcherapi/client/SwitcherLocal1Test.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.switcherapi.client.model.ContextKey;
88
import com.switcherapi.client.model.Entry;
99
import com.switcherapi.client.model.StrategyValidator;
10+
import com.switcherapi.client.model.Switcher;
11+
import com.switcherapi.client.model.SwitcherBuilder;
1012
import com.switcherapi.client.model.SwitcherRequest;
1113
import com.switcherapi.fixture.Product;
1214
import com.google.gson.Gson;
@@ -49,36 +51,54 @@ void localShouldValidateContext() {
4951

5052
@Test
5153
void localShouldReturnTrue() {
52-
SwitcherRequest switcher = Switchers.getSwitcher(Switchers.USECASE11, true);
54+
Switcher switcher = Switchers.getSwitcher(Switchers.USECASE11, true)
55+
.keepExecutions();
5356

5457
assertNull(switcher.getLastExecutionResult());
5558
assertTrue(switcher.isItOn());
5659

57-
// check result history
60+
// check result from executions
5861
assertTrue(switcher.getLastExecutionResult().isItOn());
5962
}
6063

6164
@Test
6265
void localShouldReturnTrueUsingFriendlyConstantName() {
63-
SwitcherRequest switcher = Switchers.getSwitcher(Switchers.friendlyFeatureName, true);
66+
Switcher switcher = Switchers.getSwitcher(Switchers.friendlyFeatureName, true)
67+
.keepExecutions();
6468

6569
assertNull(switcher.getLastExecutionResult());
6670
assertTrue(switcher.isItOn());
6771

68-
// check result history
72+
// check result from executions
6973
assertTrue(switcher.getLastExecutionResult().isItOn());
7074
}
7175

7276
@Test
7377
void localShouldReturnFalse() {
74-
SwitcherRequest switcher = Switchers.getSwitcher(Switchers.USECASE12);
78+
Switcher switcher = Switchers.getSwitcher(Switchers.USECASE12)
79+
.keepExecutions();
7580

7681
assertNull(switcher.getLastExecutionResult());
7782
assertFalse(switcher.isItOn());
7883

79-
// check result history
84+
// check result from executions
8085
assertFalse(switcher.getLastExecutionResult().isItOn());
8186
}
87+
88+
@Test
89+
void localShouldCleanExecutions() {
90+
SwitcherBuilder switcher = Switchers.getSwitcher(Switchers.USECASE11, true)
91+
.keepExecutions();
92+
93+
assertNull(switcher.getLastExecutionResult());
94+
assertTrue(switcher.isItOn());
95+
96+
// check result from executions
97+
assertTrue(switcher.getLastExecutionResult().isItOn());
98+
switcher.flushExecutions();
99+
100+
assertNull(switcher.getLastExecutionResult());
101+
}
82102

83103
@Test
84104
void localShouldReturnFalse_groupDisabled() {

src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ void shouldReturnTrue_withThrottle() {
4949
//test
5050
Switcher switcher = SwitchersBase
5151
.getSwitcher(SwitchersBase.USECASE11)
52-
.flush()
52+
.flushExecutions()
53+
.resetInputs()
5354
.checkValue("value")
5455
.throttle(1000);
5556

src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void shouldRetrieveNewResponse_whenStrategyInputChanged() {
4848
//test
4949
SwitcherBuilder switcher = SwitchersBase
5050
.getSwitcher(SwitchersBase.USECASE11)
51-
.flush()
51+
.flushExecutions()
52+
.resetInputs()
5253
.throttle(1000);
5354

5455
for (int i = 0; i < 100; i++) {

0 commit comments

Comments
 (0)