added lock for onSent rpc

This commit is contained in:
YevhenBondarenko 2021-09-29 13:12:04 +03:00
parent b5da59e3b3
commit 183f3547a3
5 changed files with 29 additions and 7 deletions

View File

@ -119,7 +119,7 @@ public class LwM2mClient implements Serializable {
@Getter @Getter
@Setter @Setter
private Integer lastSentRpcId; private UUID lastSentRpcId;
public Object clone() throws CloneNotSupportedException { public Object clone() throws CloneNotSupportedException {
return super.clone(); return super.clone();

View File

@ -319,6 +319,10 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im
Registration registration = client.getRegistration(); Registration registration = client.getRegistration();
try { try {
logService.log(client, String.format("[%s][%s] Sending request: %s to %s", registration.getId(), registration.getSocketAddress(), request.getClass().getSimpleName(), pathToStringFunction.apply(request))); logService.log(client, String.format("[%s][%s] Sending request: %s to %s", registration.getId(), registration.getSocketAddress(), request.getClass().getSimpleName(), pathToStringFunction.apply(request)));
if (!callback.onSent(request)) {
return;
}
context.getServer().send(registration, request, timeoutInMs, response -> { context.getServer().send(registration, request, timeoutInMs, response -> {
executor.submit(() -> { executor.submit(() -> {
try { try {
@ -330,7 +334,6 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im
} }
}); });
}, e -> handleDownlinkError(client, request, callback, e)); }, e -> handleDownlinkError(client, request, callback, e));
callback.onSent(request);
} catch (Exception e) { } catch (Exception e) {
handleDownlinkError(client, request, callback, e); handleDownlinkError(client, request, callback, e);
} }

View File

@ -17,7 +17,9 @@ package org.thingsboard.server.transport.lwm2m.server.downlink;
public interface DownlinkRequestCallback<R, T> { public interface DownlinkRequestCallback<R, T> {
default void onSent(R request){}; default boolean onSent(R request){
return true;
};
void onSuccess(R request, T response); void onSuccess(R request, T response);

View File

@ -95,8 +95,11 @@ public class DefaultLwM2MRpcRequestHandler implements LwM2MRpcRequestHandler {
this.sendErrorRpcResponse(sessionInfo, rpcRequest.getRequestId(), ResponseCode.INTERNAL_SERVER_ERROR, "Registration is empty"); this.sendErrorRpcResponse(sessionInfo, rpcRequest.getRequestId(), ResponseCode.INTERNAL_SERVER_ERROR, "Registration is empty");
return; return;
} }
if (client.getLastSentRpcId() != null && client.getLastSentRpcId().equals(rpcRequest.getRequestId())) { UUID rpcId = new UUID(rpcRequest.getRequestIdMSB(), rpcRequest.getRequestIdLSB());
log.info("[{}] Rpc has already sent!", rpcRequest.getRequestId());
if (rpcId.equals(client.getLastSentRpcId())) {
log.debug("[{}]][{}] Rpc has already sent!", client.getEndpoint(), rpcId);
return;
} }
try { try {
if (operationType.isHasObjectId()) { if (operationType.isHasObjectId()) {

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.server.transport.lwm2m.server.rpc; package org.thingsboard.server.transport.lwm2m.server.rpc;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.request.exception.ClientSleepingException; import org.eclipse.leshan.core.request.exception.ClientSleepingException;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
@ -26,8 +27,10 @@ import org.thingsboard.server.gen.transport.TransportProtos;
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient;
import org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCallback; import org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCallback;
import java.util.UUID;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@Slf4j
public abstract class RpcDownlinkRequestCallbackProxy<R, T> implements DownlinkRequestCallback<R, T> { public abstract class RpcDownlinkRequestCallbackProxy<R, T> implements DownlinkRequestCallback<R, T> {
private final TransportService transportService; private final TransportService transportService;
@ -44,9 +47,20 @@ public abstract class RpcDownlinkRequestCallbackProxy<R, T> implements DownlinkR
} }
@Override @Override
public void onSent(R request) { public boolean onSent(R request) {
client.setLastSentRpcId(this.request.getRequestId()); client.lock();
try {
UUID rpcId = new UUID(this.request.getRequestIdMSB(), this.request.getRequestIdLSB());
if (rpcId.equals(client.getLastSentRpcId())) {
log.debug("[{}]][{}] Rpc has already sent!", client.getEndpoint(), rpcId);
return false;
}
client.setLastSentRpcId(rpcId);
} finally {
client.unlock();
}
transportService.process(client.getSession(), this.request, RpcStatus.SENT, TransportServiceCallback.EMPTY); transportService.process(client.getSession(), this.request, RpcStatus.SENT, TransportServiceCallback.EMPTY);
return true;
} }
@Override @Override