Skip to content

Commit

Permalink
Merge pull request #1117 from OSGP/feature/SMHE-1775_permit_denied_delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kroesctrl authored Nov 16, 2023
2 parents f1d6a19 + 262eb69 commit 2390b8e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package org.opensmartgridplatform.adapter.protocol.dlms.application.config;

import java.security.SecureRandom;
import java.time.Duration;
import org.opensmartgridplatform.shared.wsheaderattribute.priority.MessagePriorityEnum;
import org.opensmartgridplatform.throttling.ThrottlingClient;
import org.opensmartgridplatform.throttling.api.ThrottlingConfig;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -14,6 +16,7 @@

@Configuration
public class ThrottlingClientConfig {
private static final SecureRandom random = new SecureRandom();

@Value("${throttling.client.enabled:false}")
private boolean clientEnabled;
Expand All @@ -36,8 +39,14 @@ public class ThrottlingClientConfig {
@Value("#{T(java.time.Duration).parse('${throttling.service.timeout:PT30S}')}")
private Duration timeout;

@Value("#{T(java.time.Duration).parse('${throttling.rejected.delay:PT10S}')}")
private Duration permitRejectedDelay;
@Value("#{T(java.time.Duration).parse('${throttling.rejected.min.delay:PT50S}')}")
private Duration permitRejectedMinDelay;

@Value("#{T(java.time.Duration).parse('${throttling.rejected.max.delay:PT70S}')}")
private Duration permitRejectedMaxDelay;

@Value("#{T(java.time.Duration).parse('${throttling.rejected.high.prio.delay:PT2S}')}")
private Duration permitRejectedHighPrioDelay;

public boolean clientEnabled() {
return this.clientEnabled;
Expand All @@ -63,7 +72,14 @@ public ThrottlingClient throttlingClient() {
*
* @return delay
*/
public Duration permitRejectedDelay() {
return this.permitRejectedDelay;
public Duration permitRejectedDelay(final int messagePriority) {
if (messagePriority > MessagePriorityEnum.DEFAULT.getPriority()) {
return this.permitRejectedHighPrioDelay;
}
final long minMillis =
Math.min(this.permitRejectedMinDelay.toMillis(), this.permitRejectedMaxDelay.toMillis());
final long maxMillis =
Math.max(this.permitRejectedMinDelay.toMillis(), this.permitRejectedMaxDelay.toMillis());
return Duration.ofMillis(this.random.nextLong(minMillis, maxMillis));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public void run() {
RecoverKeyProcess.this.run();
}
},
this.throttlingClientConfig.permitRejectedDelay().toMillis());
this.throttlingClientConfig
.permitRejectedDelay(this.messageMetadata.getMessagePriority())
.toMillis());

this.deviceKeyProcessingService.stopProcessing(this.deviceIdentification);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ public void processMessage(final ObjectMessage message) throws JMSException {
* Throttling permit for network access not granted, send the request back to the queue to be
* picked up again a little later by the message listener for device requests.
*/
final Duration permitRejectDelay =
this.throttlingClientConfig.permitRejectedDelay(messageMetadata.getMessagePriority());
log.info(
"Throttling permit was denied for deviceIdentification {} for network segment ({}, {}) for {}. retry message in {} ms",
messageMetadata.getDeviceIdentification(),
exception.getBaseTransceiverStationId(),
exception.getCellId(),
exception.getConfigurationName(),
this.throttlingClientConfig.permitRejectedDelay().toMillis());
this.deviceRequestMessageSender.send(
messageObject, messageMetadata, this.throttlingClientConfig.permitRejectedDelay());
permitRejectDelay.toMillis());
this.deviceRequestMessageSender.send(messageObject, messageMetadata, permitRejectDelay);

} catch (final DeviceKeyProcessAlreadyRunningException exception) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ throttling.configuration.name=CDMA
throttling.configuration.max.concurrency=1000
throttling.service.url=http://localhost:9090
throttling.service.timeout=PT30S
throttling.rejected.delay=PT10S
throttling.rejected.min.delay=PT50S
throttling.rejected.max.delay=PT70S
throttling.rejected.high.prio.delay=PT2S
# Configuration for the throttling service on the GPRS network
throttling.max.open.connections=1000
throttling.max.new.connection.requests=30
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.opensmartgridplatform.adapter.protocol.dlms.application.config;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import java.time.Duration;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.opensmartgridplatform.shared.wsheaderattribute.priority.MessagePriorityEnum;
import org.springframework.test.util.ReflectionTestUtils;

class ThrottlingClientConfigTest {
private final Duration permitRejectedMinDelay = Duration.parse("PT50S");
private final Duration permitRejectedMaxDelay = Duration.parse("PT70S");
private final Duration permitRejectedHighPrioDelay = Duration.parse("PT2S");

@ParameterizedTest
@EnumSource(MessagePriorityEnum.class)
void testHttpComponentsMessageSender(final MessagePriorityEnum messagePriority) {
final ThrottlingClientConfig throttlingClientConfig = new ThrottlingClientConfig();

ReflectionTestUtils.setField(
throttlingClientConfig,
"permitRejectedMinDelay",
this.permitRejectedMinDelay,
Duration.class);
ReflectionTestUtils.setField(
throttlingClientConfig,
"permitRejectedMaxDelay",
this.permitRejectedMaxDelay,
Duration.class);
ReflectionTestUtils.setField(
throttlingClientConfig,
"permitRejectedHighPrioDelay",
this.permitRejectedHighPrioDelay,
Duration.class);

final Duration result =
throttlingClientConfig.permitRejectedDelay(messagePriority.getPriority());
if (messagePriority.getPriority() > MessagePriorityEnum.DEFAULT.getPriority()) {
assertThat(result).isEqualTo(this.permitRejectedHighPrioDelay);
} else {
assertThat(result).isBetween(this.permitRejectedMinDelay, this.permitRejectedMaxDelay);
}
}
}

0 comments on commit 2390b8e

Please sign in to comment.