#26 - fix the boolean config value conversion, organize all config values to single class
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from asyncio import AbstractEventLoop
|
||||
from threading import Thread
|
||||
|
||||
from aio_pika import Message
|
||||
from aio_pika.abc import AbstractRobustChannel, ExchangeType
|
||||
from aio_pika.abc import AbstractRobustChannel
|
||||
|
||||
from amqp.adapter.logging_utils import logging_info, logging_warning
|
||||
from amqp.config.amq_configuration import AMQConfiguration
|
||||
from amqp.model.model import ScalingRequestAlert
|
||||
from amqp.router.utils import await_future, await_result
|
||||
|
||||
# Duration in seconds between backpressure reports
|
||||
REPORT_WINDOW_SEC = 10
|
||||
# Number of reports without activity to trigger idle event
|
||||
NUM_REPORTS_FOR_IDLE = 3
|
||||
|
||||
|
||||
class ScaleRequestV1:
|
||||
|
||||
@@ -61,15 +55,20 @@ class BackpressureHandler:
|
||||
last_backpressure_event_time = 0
|
||||
last_backpressure_event = ScalingRequestAlert.UPDATE
|
||||
|
||||
parallel_workers = int(os.getenv("PARALLEL_WORKERS", default=0))
|
||||
swarm_service_id = os.getenv("SWARM_SERVICE_ID", default="clever-amqp-service")
|
||||
swarm_task_id = os.getenv("SWARM_TASK_ID", default="python-adapter")
|
||||
|
||||
def __init__(self, channel: AbstractRobustChannel, loop: AbstractEventLoop):
|
||||
def __init__(
|
||||
self,
|
||||
channel: AbstractRobustChannel,
|
||||
loop: AbstractEventLoop,
|
||||
config: AMQConfiguration,
|
||||
):
|
||||
self.lock = None
|
||||
self.channel = channel
|
||||
self.loop = loop
|
||||
self.config = config
|
||||
self.exchange = None
|
||||
self.swarm_service_id = self.config.amq_adapter.swarm_service_id
|
||||
self.swarm_task_id = self.config.amq_adapter.swarm_task_id
|
||||
self.parallel_workers = self.config.backpressure.threshold_threads
|
||||
|
||||
def increase_parallel_executions(self):
|
||||
"""Increase the number of parallel executions"""
|
||||
@@ -120,7 +119,8 @@ class BackpressureHandler:
|
||||
self.last_backpressure_event = ScalingRequestAlert.OVERLOAD
|
||||
|
||||
def backpressure_monitor_loop(self):
|
||||
global REPORT_WINDOW_SEC
|
||||
_time_window_sec = self.config.backpressure.time_window
|
||||
_idle_duration_millis = 1000 * self.config.backpressure.idle_duration
|
||||
_loop = asyncio.new_event_loop()
|
||||
|
||||
async def _backpressure_monitor():
|
||||
@@ -143,7 +143,7 @@ class BackpressureHandler:
|
||||
if (
|
||||
self.current_parallel_executions >= self.parallel_workers
|
||||
and time.time() - self.last_backpressure_event_time
|
||||
> REPORT_WINDOW_SEC
|
||||
> _time_window_sec
|
||||
and self.last_backpressure_event != ScalingRequestAlert.OVERLOAD
|
||||
):
|
||||
# Trigger the overload event
|
||||
@@ -153,7 +153,7 @@ class BackpressureHandler:
|
||||
elif (
|
||||
self.current_parallel_executions == 0
|
||||
and time.time() - self.last_data_message_time
|
||||
> NUM_REPORTS_FOR_IDLE * REPORT_WINDOW_SEC
|
||||
>= _idle_duration_millis
|
||||
and self.last_backpressure_event != ScalingRequestAlert.IDLE
|
||||
):
|
||||
# Trigger the idle event
|
||||
@@ -165,7 +165,7 @@ class BackpressureHandler:
|
||||
if (
|
||||
self.current_parallel_executions > 0
|
||||
and time.time() - self.last_backpressure_event_time
|
||||
> REPORT_WINDOW_SEC
|
||||
> _time_window_sec
|
||||
):
|
||||
# Trigger the update event
|
||||
_scaling_request: ScaleRequestV1 = ScaleRequestV1(
|
||||
@@ -179,7 +179,7 @@ class BackpressureHandler:
|
||||
await self.publish_backpressure_request(_scaling_request)
|
||||
self.last_backpressure_event_time = time.time()
|
||||
self.last_backpressure_event = ScalingRequestAlert.UPDATE
|
||||
await asyncio.sleep(REPORT_WINDOW_SEC)
|
||||
await asyncio.sleep(_time_window_sec)
|
||||
|
||||
_loop.run_until_complete(_backpressure_monitor())
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ Methods here will invoke the actual CleverThis Service code, which are passed as
|
||||
import asyncio
|
||||
import inspect
|
||||
import json
|
||||
import logging
|
||||
from asyncio import AbstractEventLoop
|
||||
from typing import Dict, List, Callable, Coroutine, Any
|
||||
|
||||
@@ -141,23 +140,9 @@ class CleverThisServiceAdapter:
|
||||
self.service_host = self.amq_configuration.dispatch.service_host
|
||||
self.service_port = self.amq_configuration.dispatch.service_port
|
||||
self.loop: AbstractEventLoop | None = None
|
||||
try:
|
||||
self.require_authenticated_user = (
|
||||
self.amq_configuration.amq_adapter.require_authenticated_user
|
||||
and isinstance(
|
||||
self.amq_configuration.amq_adapter.require_authenticated_user, bool
|
||||
)
|
||||
or eval(
|
||||
str(self.amq_configuration.amq_adapter.require_authenticated_user)
|
||||
.strip()
|
||||
.capitalize()
|
||||
or "True"
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
self.require_authenticated_user = (
|
||||
True # Enforce authenticated user as default
|
||||
)
|
||||
self.require_authenticated_user = (
|
||||
self.amq_configuration.amq_adapter.require_authenticated_user
|
||||
)
|
||||
|
||||
def get_content_type(self, message_headers: Dict[str, List[str]]) -> str:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user