Skip to content

Commit 03b8a2a

Browse files
committed
Cleanup state after future has completed
- Clean up request and promise maps whenever future completes - Enables timeout for Client/Server.send() (cherry picked from commit c801fbb221b4090405e58ed37d414fab06b16ce3)
1 parent 6c636a2 commit 03b8a2a

File tree

6 files changed

+48
-14
lines changed

6 files changed

+48
-14
lines changed

ocpp-common/src/main/java/eu/chargetime/ocpp/Client.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public void handleConfirmation(String uniqueId, Confirmation confirmation) {
7878
promiseRepository.getPromise(uniqueId);
7979
if (promiseOptional.isPresent()) {
8080
promiseOptional.get().complete(confirmation);
81-
promiseRepository.removePromise(uniqueId);
8281
} else {
8382
logger.debug("Promise not found for confirmation {}", confirmation);
8483
}
@@ -105,11 +104,9 @@ public void handleError(
105104
Optional<CompletableFuture<Confirmation>> promiseOptional =
106105
promiseRepository.getPromise(uniqueId);
107106
if (promiseOptional.isPresent()) {
108-
promiseOptional
109-
.get()
107+
promiseOptional.get()
110108
.completeExceptionally(
111109
new CallErrorException(errorCode, errorDescription, payload));
112-
promiseRepository.removePromise(uniqueId);
113110
} else {
114111
logger.debug("Promise not found for error {}", errorDescription);
115112
}
@@ -158,10 +155,16 @@ public CompletableFuture<Confirmation> send(Request request)
158155
throw new OccurenceConstraintException();
159156
}
160157

161-
String id = session.storeRequest(request);
162-
CompletableFuture<Confirmation> promise = promiseRepository.createPromise(id);
158+
String requestUuid = session.storeRequest(request);
159+
CompletableFuture<Confirmation> promise = promiseRepository.createPromise(requestUuid);
163160

164-
session.sendRequest(featureOptional.get().getAction(), request, id);
161+
// Clean up after the promise has completed, no matter if it was successful or had an error or a timeout.
162+
promise.whenComplete((confirmation, throwable) -> {
163+
session.removeRequest(requestUuid);
164+
promiseRepository.removePromise(requestUuid);
165+
});
166+
167+
session.sendRequest(featureOptional.get().getAction(), request, requestUuid);
165168
return promise;
166169
}
167170

ocpp-common/src/main/java/eu/chargetime/ocpp/ISession.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public interface ISession {
4040

4141
String storeRequest(Request payload);
4242

43+
void removeRequest(String ticket);
44+
4345
void sendRequest(String action, Request payload, String uuid);
4446

4547
boolean completePendingPromise(String id, Confirmation confirmation) throws UnsupportedFeatureException, OccurenceConstraintException;

ocpp-common/src/main/java/eu/chargetime/ocpp/Queue.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public Optional<Request> restoreRequest(String ticket) {
8989
return Optional.empty();
9090
}
9191

92+
/**
93+
* Remove a stored {@link Request} using a unique identifier.
94+
* If no request is found for the identifier this method has no effect.
95+
*
96+
* @param ticket unique identifier returned when {@link Request} was initially stored.
97+
*/
98+
public void removeRequest(String ticket) {
99+
requestQueue.remove(ticket);
100+
}
101+
92102
@Override
93103
public String toString() {
94104
return MoreObjects.toStringHelper(this).add("requestQueue", requestQueue).toString();

ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public void handleConfirmation(String uniqueId, Confirmation confirmation) {
9797
promiseRepository.getPromise(uniqueId);
9898
if (promiseOptional.isPresent()) {
9999
promiseOptional.get().complete(confirmation);
100-
promiseRepository.removePromise(uniqueId);
101100
} else {
102101
logger.debug("Promise not found for confirmation {}", confirmation);
103102
}
@@ -135,11 +134,9 @@ public void handleError(
135134
Optional<CompletableFuture<Confirmation>> promiseOptional =
136135
promiseRepository.getPromise(uniqueId);
137136
if (promiseOptional.isPresent()) {
138-
promiseOptional
139-
.get()
137+
promiseOptional.get()
140138
.completeExceptionally(
141139
new CallErrorException(errorCode, errorDescription, payload));
142-
promiseRepository.removePromise(uniqueId);
143140
} else {
144141
logger.debug("Promise not found for error {}", errorDescription);
145142
}
@@ -216,9 +213,16 @@ public CompletableFuture<Confirmation> send(UUID sessionIndex, Request request)
216213
throw new OccurenceConstraintException();
217214
}
218215

219-
String id = session.storeRequest(request);
220-
CompletableFuture<Confirmation> promise = promiseRepository.createPromise(id);
221-
session.sendRequest(featureOptional.get().getAction(), request, id);
216+
String requestUuid = session.storeRequest(request);
217+
CompletableFuture<Confirmation> promise = promiseRepository.createPromise(requestUuid);
218+
219+
// Clean up after the promise has completed, no matter if it was successful or had an error or a timeout.
220+
promise.whenComplete((confirmation, throwable) -> {
221+
session.removeRequest(requestUuid);
222+
promiseRepository.removePromise(requestUuid);
223+
});
224+
225+
session.sendRequest(featureOptional.get().getAction(), request, requestUuid);
222226
return promise;
223227
}
224228

ocpp-common/src/main/java/eu/chargetime/ocpp/Session.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ public String storeRequest(Request payload) {
114114
return queue.store(payload);
115115
}
116116

117+
/**
118+
* Remove a stored {@link Request} using a unique identifier.
119+
* If no request is found for the identifier this method has no effect.
120+
*
121+
* @param ticket unique identifier returned when {@link Request} was initially stored.
122+
*/
123+
public void removeRequest(String ticket) {
124+
queue.removeRequest(ticket);
125+
}
126+
117127
/**
118128
* Send a {@link Confirmation} to a {@link Request}
119129
*

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/TimeoutSessionDecorator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public void sendRequest(String action, Request payload, String uuid) {
9292
this.session.sendRequest(action, payload, uuid);
9393
}
9494

95+
@Override
96+
public void removeRequest(String ticket) {
97+
this.session.removeRequest(ticket);
98+
}
99+
95100
@Override
96101
public boolean completePendingPromise(String id, Confirmation confirmation) throws UnsupportedFeatureException, OccurenceConstraintException {
97102
return this.session.completePendingPromise(id, confirmation);

0 commit comments

Comments
 (0)