Skip to content

Commit 9f506a1

Browse files
committed
add locks in ChargePointRepositoryImpl.isRegistered(..)
1 parent c06d73f commit 9f506a1

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/main/java/de/rwth/idsg/steve/repository/impl/ChargePointRepositoryImpl.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.rwth.idsg.steve.repository.impl;
22

3+
import com.google.common.util.concurrent.Striped;
34
import de.rwth.idsg.steve.SteveException;
45
import de.rwth.idsg.steve.ocpp.OcppProtocol;
56
import de.rwth.idsg.steve.ocpp.OcppTransport;
@@ -31,6 +32,7 @@
3132
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Map;
35+
import java.util.concurrent.locks.Lock;
3436
import java.util.stream.Collectors;
3537

3638
import static de.rwth.idsg.steve.SteveConfiguration.CONFIG;
@@ -49,6 +51,8 @@
4951
public class ChargePointRepositoryImpl implements ChargePointRepository {
5052

5153
private final boolean autoRegisterUnknownStations = CONFIG.getOcpp().isAutoRegisterUnknownStations();
54+
private final Striped<Lock> isRegisteredLocks = Striped.lock(16);
55+
5256
private final DSLContext ctx;
5357
private final AddressRepository addressRepository;
5458

@@ -60,24 +64,30 @@ public ChargePointRepositoryImpl(DSLContext ctx, AddressRepository addressReposi
6064

6165
@Override
6266
public boolean isRegistered(String chargeBoxId) {
63-
// 1. exit if already registered
64-
if (isRegisteredInternal(chargeBoxId)) {
65-
return true;
66-
}
67+
Lock l = isRegisteredLocks.get(chargeBoxId);
68+
l.lock();
69+
try {
70+
// 1. exit if already registered
71+
if (isRegisteredInternal(chargeBoxId)) {
72+
return true;
73+
}
6774

68-
// 2. ok, this chargeBoxId is unknown. exit if auto-register is disabled
69-
if (!autoRegisterUnknownStations) {
70-
return false;
71-
}
75+
// 2. ok, this chargeBoxId is unknown. exit if auto-register is disabled
76+
if (!autoRegisterUnknownStations) {
77+
return false;
78+
}
7279

73-
// 3. chargeBoxId is unknown and auto-register is enabled. insert chargeBoxId
74-
try {
75-
addChargePoint(Collections.singletonList(chargeBoxId));
76-
log.warn("Auto-registered unknown chargebox '{}'", chargeBoxId);
77-
return true;
78-
} catch (Exception e) {
79-
log.error("Failed to auto-register unknown chargebox '" + chargeBoxId + "'", e);
80-
return false;
80+
// 3. chargeBoxId is unknown and auto-register is enabled. insert chargeBoxId
81+
try {
82+
addChargePoint(Collections.singletonList(chargeBoxId));
83+
log.warn("Auto-registered unknown chargebox '{}'", chargeBoxId);
84+
return true;
85+
} catch (Exception e) {
86+
log.error("Failed to auto-register unknown chargebox '" + chargeBoxId + "'", e);
87+
return false;
88+
}
89+
} finally {
90+
l.unlock();
8191
}
8292
}
8393

0 commit comments

Comments
 (0)