From 98f2cfa1610f43aac5910a25c955cd1bb7516ab3 Mon Sep 17 00:00:00 2001 From: Yaroslav Svitlytskyi <53532703+YarikRevich@users.noreply.github.com> Date: Sun, 9 Jun 2024 02:15:30 +0200 Subject: [PATCH] fix: added throttle for requests (#9) --- Scripts/cli/src/client/client.py | 11 +++++++++++ Scripts/cli/src/tools/__init__.py | 4 +++- Scripts/cli/src/tools/throttle.py | 10 ++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Scripts/cli/src/tools/throttle.py diff --git a/Scripts/cli/src/client/client.py b/Scripts/cli/src/client/client.py index 29e3a1e..385f72d 100644 --- a/Scripts/cli/src/client/client.py +++ b/Scripts/cli/src/client/client.py @@ -18,6 +18,8 @@ from ..dto import SettingsTypeCompound from ..dto import SetSettingsDto +from ..tools import perform_request_await + class Client: """Represents client used to connect to remote device via serial port.""" @@ -51,6 +53,9 @@ def __send_data_bus_request_content(self, type: DataBus.DataType) -> Response.Re data = request_container.SerializeToString() self.connection.write(data_length) + + perform_request_await() + self.connection.write(data) result_length_raw = self.connection.read(3) @@ -122,6 +127,9 @@ def __send_info_bus_request_content(self, type: InfoBus.InfoType) -> Response.Re data = request_container.SerializeToString() self.connection.write(data_length) + + perform_request_await() + self.connection.write(data) result_length_raw = self.connection.read(3) @@ -193,6 +201,9 @@ def __send_settings_bus_request_content(self, type: SettingsBus.SettingsType) -> data = request_container.SerializeToString() self.connection.write(data_length) + + perform_request_await() + self.connection.write(data) result_length_raw = self.connection.read(3) diff --git a/Scripts/cli/src/tools/__init__.py b/Scripts/cli/src/tools/__init__.py index acac466..6221ad3 100644 --- a/Scripts/cli/src/tools/__init__.py +++ b/Scripts/cli/src/tools/__init__.py @@ -1 +1,3 @@ -from .prettifier import print_output \ No newline at end of file +from .prettifier import print_output + +from .throttle import perform_request_await \ No newline at end of file diff --git a/Scripts/cli/src/tools/throttle.py b/Scripts/cli/src/tools/throttle.py new file mode 100644 index 0000000..a72c5eb --- /dev/null +++ b/Scripts/cli/src/tools/throttle.py @@ -0,0 +1,10 @@ +import time + +# Represents throttle request duration time. +THROTTLE_REQUEST_DURATION: float = 0.3 + + +def perform_request_await() -> None: + """Performs throttle request operation.""" + + time.sleep(THROTTLE_REQUEST_DURATION)