Skip to content

Commit

Permalink
[Snorkell.ai]: Documentation for rpm_controller.py
Browse files Browse the repository at this point in the history
  • Loading branch information
penify-dev[bot] authored Jan 19, 2024
1 parent 33071ba commit 5cb906e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/crewai/utilities/rpm_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ class RPMController(BaseModel):

@model_validator(mode="after")
def reset_counter(self):
"""
Reset the counter and lock if max RPM is set.
:return: None
:raises: Any exceptions that may occur during the reset process.
"""


if self.max_rpm:
self._lock = threading.Lock()
self._reset_request_count()
return self

def check_or_wait(self):
"""
Check if the current RPM is less than the maximum RPM, and either increment the current RPM or wait for the next minute.
:return: True if the current RPM is less than the maximum RPM or after waiting for the next minute.
:raises: None
"""


if not self.max_rpm:
return True
Expand All @@ -41,18 +55,49 @@ def check_or_wait(self):
return True

def stop_rpm_counter(self):
"""
Stop the RPM counter.
This method cancels the timer used for the RPM counter if it is running.
Raises:
None
Returns:
None
"""


if self._timer:
self._timer.cancel()
self._timer = None

def _wait_for_next_minute(self):
"""
Wait for the next minute and reset the current RPM to 0.
This method sleeps for 60 seconds and then resets the current RPM to 0 within a thread-safe context.
Raises:
Any exceptions raised by time.sleep() or by acquiring the lock.
"""


time.sleep(60)
with self._lock:
self._current_rpm = 0

def _reset_request_count(self):
"""
Reset the request count and start a new timer for resetting the count after 60 seconds.
This method resets the request count to 0 and starts a new timer to reset the count after 60 seconds.
Raises:
<Exception Type>: <Description of the exception raised>
"""


with self._lock:
self._current_rpm = 0
Expand Down

0 comments on commit 5cb906e

Please sign in to comment.