From 390efb4c38a7a13ea798cd633c6a3aed05afc05b Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 22 Jul 2022 11:16:46 -0700 Subject: [PATCH] [srp-client] add random short delay before sending update message (#7863) This commit updates the `Srp::Client` to add a randomly chosen short delay before delay wait time before sending an update message. This replaces the current model which was adding a fixed short delay. Selecting the delay randomly helps in situations where some common event triggers multiple SRP clients on Thread mesh to send an SRP update(e.g., new OMR prefix causing new addresses, or a server entry change on Thread Network Data). --- .../config/openthread-core-config-check.h | 5 ++++ src/core/config/srp_client.h | 24 +++++++++++++++---- src/core/net/srp_client.cpp | 2 +- src/core/net/srp_client.hpp | 3 ++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/core/config/openthread-core-config-check.h b/src/core/config/openthread-core-config-check.h index 1536ec2ff..4cf712178 100644 --- a/src/core/config/openthread-core-config-check.h +++ b/src/core/config/openthread-core-config-check.h @@ -627,4 +627,9 @@ #error "OPENTHREAD_CONFIG_BORDER_ROUTING_VICARIOUS_RS_ENABLE was removed and no longer supported" #endif +#ifdef OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_DELAY +#error "OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_DELAY was replaced with "\ + "OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY and OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MAX_DELAY" +#endif + #endif // OPENTHREAD_CORE_CONFIG_CHECK_H_ diff --git a/src/core/config/srp_client.h b/src/core/config/srp_client.h index e13ed7d78..4bef258ed 100644 --- a/src/core/config/srp_client.h +++ b/src/core/config/srp_client.h @@ -214,9 +214,11 @@ #endif /** - * @def OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_DELAY + * @def OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY * - * Specifies the (short) delay (in msec) after an update is required before SRP client sends the update message. + * Specifies the minimum value (in msec) for the short random delay wait time before sending an update message. + * + * The random delay is chosen uniformly from the min up to max value `OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MAX_DELAY`. * * When there is a change (e.g., a new service is added/removed) that requires an update, the SRP client will wait for * a short delay before preparing and sending an SRP update message to server. This allows user to provide more change @@ -225,8 +227,22 @@ * delay timer. * */ -#ifndef OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_DELAY -#define OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_DELAY 10 +#ifndef OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY +#define OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY 10 +#endif + +/** + * @def OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY + * + * Specifies the maximum value (in msec) for the short random delay wait time before sending an update message. + * + * The random delay is chosen uniformly from the min `OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY` up to max value. + * + * See `OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY` for more details. + * + */ +#ifndef OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MAX_DELAY +#define OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MAX_DELAY 700 #endif /** diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index 61b1f77cf..5bf507be1 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -690,7 +690,7 @@ void Client::SetState(State aState) break; case kStateToUpdate: - mTimer.Start(kUpdateTxDelay); + mTimer.Start(Random::NonCrypto::GetUint32InRange(kUpdateTxMinDelay, kUpdateTxMaxDelay)); break; case kStateUpdating: diff --git a/src/core/net/srp_client.hpp b/src/core/net/srp_client.hpp index fb367719d..46bdc3369 100644 --- a/src/core/net/srp_client.hpp +++ b/src/core/net/srp_client.hpp @@ -777,7 +777,8 @@ private: // delay as specified by `kUpdateTxDelay` before sending an SRP // update to server. This allows the user to provide more change // that are then all sent in same update message. - static constexpr uint32_t kUpdateTxDelay = OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_DELAY; // in msec. + static constexpr uint32_t kUpdateTxMinDelay = OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MIN_DELAY; // in msec. + static constexpr uint32_t kUpdateTxMaxDelay = OPENTHREAD_CONFIG_SRP_CLIENT_UPDATE_TX_MAX_DELAY; // in msec. // ------------------------------- // Retry related constants