Skip to content

Commit 4427baf

Browse files
authored
feat: improved async performance with thread-safe type (#381) (#382)
1 parent efc6bec commit 4427baf

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public AsyncSwitcher(final Switcher switcher, long delay) {
4646
* Validate if next run is ready to be performed, otherwise it will skip and delegate the
4747
* Switcher result for the Switcher history execution.
4848
*/
49-
public synchronized void execute() {
49+
public void execute() {
5050
SwitcherUtils.debug(logger, "nextRun: {} - currentTimeMillis: {}", nextRun, System.currentTimeMillis());
5151

5252
if (nextRun < System.currentTimeMillis()) {

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
public abstract class SwitcherBuilder implements Switcher {
1616

1717
protected final SwitcherProperties properties;
18-
19-
protected final Map<List<Entry>, SwitcherResult> historyExecution;
2018

2119
protected long delay;
2220

@@ -32,7 +30,6 @@ public abstract class SwitcherBuilder implements Switcher {
3230

3331
protected SwitcherBuilder(final SwitcherProperties properties) {
3432
this.properties = properties;
35-
this.historyExecution = new HashMap<>();
3633
this.entry = new ArrayList<>();
3734
this.delay = 0;
3835
}
@@ -42,11 +39,7 @@ protected SwitcherBuilder(final SwitcherProperties properties) {
4239
*
4340
* @return {@link SwitcherBuilder}
4441
*/
45-
public SwitcherBuilder flush() {
46-
this.historyExecution.clear();
47-
this.entry.clear();
48-
return this;
49-
}
42+
public abstract SwitcherBuilder flush();
5043

5144
/**
5245
* Skip API calls given a delay time

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import com.switcherapi.client.exception.SwitcherException;
77
import com.switcherapi.client.test.SwitcherBypass;
88

9-
import java.util.*;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Objects;
12+
import java.util.Optional;
13+
import java.util.concurrent.ConcurrentHashMap;
1014

1115
/**
1216
* SwitcherRequest is the entry point to evaluate criteria and return the result.
@@ -20,6 +24,8 @@
2024
*/
2125
public final class SwitcherRequest extends SwitcherBuilder {
2226

27+
private final ConcurrentHashMap<List<Entry>, SwitcherResult> historyExecution;
28+
2329
private final SwitcherExecutor switcherExecutor;
2430

2531
private final String switcherKey;
@@ -39,6 +45,7 @@ public SwitcherRequest(final String switcherKey,
3945
super(switcherProperties);
4046
this.switcherExecutor = switcherExecutor;
4147
this.switcherKey = switcherKey;
48+
this.historyExecution = new ConcurrentHashMap<>();
4249
}
4350

4451
@Override
@@ -61,7 +68,14 @@ public SwitcherRequest prepareEntry(final Entry entry, final boolean add) {
6168
public SwitcherRequest prepareEntry(final Entry entry) {
6269
return this.prepareEntry(entry, false);
6370
}
64-
71+
72+
@Override
73+
public SwitcherBuilder flush() {
74+
this.entry.clear();
75+
this.historyExecution.clear();
76+
return this;
77+
}
78+
6579
@Override
6680
public boolean isItOn() throws SwitcherException {
6781
final SwitcherResult response = submit();
@@ -80,9 +94,9 @@ public SwitcherResult submit() throws SwitcherException {
8094
}
8195

8296
asyncSwitcher.execute();
83-
final Optional<SwitcherResult> response = getFromHistory();
84-
if (response.isPresent()) {
85-
return response.get();
97+
final SwitcherResult response = getFromHistory();
98+
if (Objects.nonNull(response)) {
99+
return response;
86100
}
87101
}
88102

@@ -117,7 +131,7 @@ public List<Entry> getEntry() {
117131

118132
@Override
119133
public SwitcherResult getLastExecutionResult() {
120-
return getFromHistory().orElse(null);
134+
return getFromHistory();
121135
}
122136

123137
public boolean isBypassMetrics() {
@@ -128,8 +142,8 @@ private boolean canUseAsync() {
128142
return super.delay > 0 && !this.historyExecution.isEmpty();
129143
}
130144

131-
private Optional<SwitcherResult> getFromHistory() {
132-
return Optional.ofNullable(historyExecution.get(entry));
145+
private SwitcherResult getFromHistory() {
146+
return historyExecution.get(entry);
133147
}
134148

135149
@Override

src/main/java/com/switcherapi/client/service/local/ClientLocalService.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.switcherapi.client.exception.SwitcherKeyNotFoundException;
55
import com.switcherapi.client.model.Entry;
66
import com.switcherapi.client.model.SwitcherRequest;
7+
import com.switcherapi.client.model.SwitcherResult;
78
import com.switcherapi.client.model.criteria.Config;
89
import com.switcherapi.client.model.criteria.Domain;
910
import com.switcherapi.client.model.criteria.Group;
1011
import com.switcherapi.client.model.criteria.StrategyConfig;
11-
import com.switcherapi.client.model.SwitcherResult;
1212
import com.switcherapi.client.service.SwitcherFactory;
1313
import com.switcherapi.client.service.SwitcherValidator;
1414
import com.switcherapi.client.utils.SwitcherUtils;
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.Objects;
2223
import java.util.Set;
2324

2425
/**
@@ -72,7 +73,7 @@ public SwitcherResult executeCriteria(final SwitcherRequest switcher, final Doma
7273
for (final Group group : domain.getGroup()) {
7374
config = findConfigInGroup(group, switcher.getSwitcherKey());
7475

75-
if (config != null) {
76+
if (Objects.nonNull(config)) {
7677
return getSwitcherResult(switcher, group, config);
7778
}
7879
}
@@ -101,10 +102,13 @@ private SwitcherResult getSwitcherResult(SwitcherRequest switcher, Group group,
101102
}
102103

103104
private Config findConfigInGroup(final Group group, final String switcherKey) {
104-
return Arrays.stream(group.getConfig())
105-
.filter(c -> c.getKey().equals(switcherKey))
106-
.findFirst()
107-
.orElse(null);
105+
for (Config config : group.getConfig()) {
106+
if (config.getKey().equals(switcherKey)) {
107+
return config;
108+
}
109+
}
110+
111+
return null;
108112
}
109113

110114
/**
@@ -126,7 +130,7 @@ private SwitcherResult processOperation(final StrategyConfig[] configStrategies,
126130

127131
final Entry switcherInput = tryGetSwitcherInput(input, strategyConfig);
128132

129-
if (switcherInput == null) {
133+
if (Objects.isNull(switcherInput)) {
130134
return strategyFailed(switcher, strategyConfig, STRATEGY_FAIL_NO_INPUT_PATTERN);
131135
}
132136

@@ -143,7 +147,7 @@ private SwitcherResult strategyFailed(SwitcherRequest switcher, StrategyConfig s
143147
}
144148

145149
private Entry tryGetSwitcherInput(final List<Entry> input, StrategyConfig strategyConfig) {
146-
if (input == null) {
150+
if (Objects.isNull(input)) {
147151
return null;
148152
}
149153

0 commit comments

Comments
 (0)