mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 14:51:22 +00:00
commit
e5576775ef
1
lib/tfw/components/decorators/__init__.py
Normal file
1
lib/tfw/components/decorators/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .rate_limiter import RateLimiter
|
22
lib/tfw/components/decorators/rate_limiter.py
Normal file
22
lib/tfw/components/decorators/rate_limiter.py
Normal file
@ -0,0 +1,22 @@
|
||||
from functools import wraps
|
||||
from time import time, sleep
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
def __init__(self, rate_per_second):
|
||||
self.min_interval = 1 / float(rate_per_second)
|
||||
self.last_call = time()
|
||||
|
||||
def __call__(self, fun):
|
||||
@wraps(fun)
|
||||
def wrapper(*args, **kwargs):
|
||||
self._limit_rate()
|
||||
fun(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
def _limit_rate(self):
|
||||
since_last_call = time() - self.last_call
|
||||
to_next_call = self.min_interval - since_last_call
|
||||
self.last_call = time()
|
||||
if to_next_call > 0:
|
||||
sleep(to_next_call)
|
@ -2,13 +2,12 @@ from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
||||
from tfw.util import RateLimiter
|
||||
from tfw.components.decorators import RateLimiter
|
||||
|
||||
from tfw.config.logs import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
||||
class WebideReloadEventHandler(FileSystemEventHandler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
1
lib/tfw/components/mixins/__init__.py
Normal file
1
lib/tfw/components/mixins/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .supervisor_mixin import SupervisorMixin
|
20
lib/tfw/components/mixins/supervisor_mixin.py
Normal file
20
lib/tfw/components/mixins/supervisor_mixin.py
Normal file
@ -0,0 +1,20 @@
|
||||
import xmlrpc.client
|
||||
from contextlib import suppress
|
||||
from xmlrpc.client import Fault as SupervisorFault
|
||||
|
||||
from tfw.config import tfwenv
|
||||
|
||||
|
||||
class SupervisorMixin:
|
||||
supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
|
||||
|
||||
def stop_process(self):
|
||||
with suppress(SupervisorFault):
|
||||
self.supervisor.stopProcess(self.process_name)
|
||||
|
||||
def start_process(self):
|
||||
self.supervisor.startProcess(self.process_name)
|
||||
|
||||
def restart_process(self):
|
||||
self.stop_process()
|
||||
self.start_process()
|
@ -1,7 +1,7 @@
|
||||
from os.path import splitext, isfile, join, relpath
|
||||
from glob import glob
|
||||
|
||||
from tfw.util import SupervisorMixin
|
||||
from tfw.components.mixins import SupervisorMixin
|
||||
from tfw.event_handler_base import EventHandlerBase
|
||||
from tfw.components.directory_monitor import DirectoryMonitor
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
from tfw.event_handler_base import EventHandlerBase
|
||||
from tfw.util import SupervisorMixin
|
||||
from tfw.components.mixins import SupervisorMixin
|
||||
from tfw.config import tfwenv
|
||||
from tfw.config.logs import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
@ -3,7 +3,7 @@ from zmq.eventloop import ioloop
|
||||
from zmq.eventloop.zmqstream import ZMQStream
|
||||
|
||||
from tfw.config import tfwenv
|
||||
from tfw.util import ZMQConnectorBase
|
||||
from tfw.networking.zmq_connector_base import ZMQConnectorBase
|
||||
|
||||
ioloop.install()
|
||||
|
||||
|
@ -4,7 +4,7 @@ from zmq.eventloop.zmqstream import ZMQStream
|
||||
|
||||
from tfw.networking.serialization import serialize_all
|
||||
from tfw.config import tfwenv
|
||||
from tfw.util import ZMQConnectorBase
|
||||
from tfw.networking.zmq_connector_base import ZMQConnectorBase
|
||||
|
||||
|
||||
class ServerDownlinkConnector(ZMQConnectorBase):
|
||||
|
@ -5,7 +5,7 @@ from tfw.networking.serialization import serialize_all
|
||||
from tfw.config import tfwenv
|
||||
from tfw.config.logs import logging
|
||||
log = logging.getLogger(__name__)
|
||||
from tfw.util import ZMQConnectorBase
|
||||
from tfw.networking.zmq_connector_base import ZMQConnectorBase
|
||||
|
||||
|
||||
class EventHandlerDownlinkConnector(ZMQConnectorBase):
|
||||
|
@ -2,7 +2,7 @@ import zmq
|
||||
from zmq.eventloop import ioloop
|
||||
|
||||
from tfw.config import tfwenv
|
||||
from tfw.util import ZMQConnectorBase
|
||||
from tfw.networking.zmq_connector_base import ZMQConnectorBase
|
||||
from tfw.networking.serialization import serialize_all, deserialize_all
|
||||
|
||||
ioloop.install()
|
||||
|
6
lib/tfw/networking/zmq_connector_base.py
Normal file
6
lib/tfw/networking/zmq_connector_base.py
Normal file
@ -0,0 +1,6 @@
|
||||
import zmq
|
||||
|
||||
|
||||
class ZMQConnectorBase:
|
||||
def __init__(self, zmq_context=None):
|
||||
self._zmq_context = zmq_context or zmq.Context.instance()
|
@ -1,55 +0,0 @@
|
||||
import xmlrpc.client, zmq
|
||||
from contextlib import suppress
|
||||
from xmlrpc.client import Fault as SupervisorFault
|
||||
from time import time, sleep
|
||||
from functools import wraps
|
||||
|
||||
from tfw.config import tfwenv
|
||||
|
||||
|
||||
def create_source_code_response_data(filename, content, language):
|
||||
return {
|
||||
'filename': filename,
|
||||
'content': content,
|
||||
'language': language
|
||||
}
|
||||
|
||||
|
||||
class SupervisorMixin:
|
||||
supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
|
||||
|
||||
def stop_process(self):
|
||||
with suppress(SupervisorFault):
|
||||
self.supervisor.stopProcess(self.process_name)
|
||||
|
||||
def start_process(self):
|
||||
self.supervisor.startProcess(self.process_name)
|
||||
|
||||
def restart_process(self):
|
||||
self.stop_process()
|
||||
self.start_process()
|
||||
|
||||
|
||||
class ZMQConnectorBase:
|
||||
def __init__(self, zmq_context=None):
|
||||
self._zmq_context = zmq_context or zmq.Context.instance()
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
def __init__(self, rate_per_second):
|
||||
self.min_interval = 1 / float(rate_per_second)
|
||||
self.last_call = time()
|
||||
|
||||
def __call__(self, fun):
|
||||
@wraps(fun)
|
||||
def wrapper(*args, **kwargs):
|
||||
self._limit_rate()
|
||||
fun(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
def _limit_rate(self):
|
||||
since_last_call = time() - self.last_call
|
||||
to_next_call = self.min_interval - since_last_call
|
||||
self.last_call = time()
|
||||
if to_next_call > 0:
|
||||
sleep(to_next_call)
|
Loading…
Reference in New Issue
Block a user