mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 22:51:32 +00:00
Refactor webide rate limiting
This commit is contained in:
parent
4d49c8d11b
commit
60bcb8c2b0
@ -1,6 +1,7 @@
|
|||||||
from time import time, sleep
|
from time import time, sleep
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import FileSystemEventHandler
|
from watchdog.events import FileSystemEventHandler
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
||||||
|
|
||||||
@ -8,23 +9,19 @@ from tfw.config.logs import logging
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class WebideReloadEventHandler(FileSystemEventHandler):
|
class RateLimiter:
|
||||||
def __init__(self, rate_per_second):
|
def __init__(self, rate_per_second):
|
||||||
super().__init__()
|
|
||||||
self.uplink = ServerUplinkConnector()
|
|
||||||
|
|
||||||
self.min_interval = 1 / float(rate_per_second)
|
self.min_interval = 1 / float(rate_per_second)
|
||||||
self.last_call = time()
|
self.last_call = time()
|
||||||
|
|
||||||
def on_modified(self, event):
|
def __call__(self, fun):
|
||||||
self.limit_rate()
|
@wraps(fun)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
self._limit_rate()
|
||||||
|
fun(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
log.debug(event)
|
def _limit_rate(self): #TODO: pls review me :3
|
||||||
anchor = 'anchor_webide'
|
|
||||||
self.uplink.send(anchor, {'anchor': anchor,
|
|
||||||
'data': {'command': 'reload'}})
|
|
||||||
|
|
||||||
def limit_rate(self): #TODO: pls review me :3
|
|
||||||
since_last_call = time() - self.last_call
|
since_last_call = time() - self.last_call
|
||||||
to_next_call = self.min_interval - since_last_call
|
to_next_call = self.min_interval - since_last_call
|
||||||
self.last_call = time()
|
self.last_call = time()
|
||||||
@ -32,10 +29,23 @@ class WebideReloadEventHandler(FileSystemEventHandler):
|
|||||||
sleep(to_next_call)
|
sleep(to_next_call)
|
||||||
|
|
||||||
|
|
||||||
|
class WebideReloadEventHandler(FileSystemEventHandler):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.uplink = ServerUplinkConnector()
|
||||||
|
|
||||||
|
@RateLimiter(rate_per_second=5)
|
||||||
|
def on_modified(self, event):
|
||||||
|
log.debug(event)
|
||||||
|
anchor = 'anchor_webide'
|
||||||
|
self.uplink.send(anchor, {'anchor': anchor,
|
||||||
|
'data': {'command': 'reload'}})
|
||||||
|
|
||||||
|
|
||||||
class DirectoryMonitor:
|
class DirectoryMonitor:
|
||||||
def __init__(self, directory):
|
def __init__(self, directory):
|
||||||
self.observer = Observer()
|
self.observer = Observer()
|
||||||
self.observer.schedule(WebideReloadEventHandler(rate_per_second=5), directory, recursive=True)
|
self.observer.schedule(WebideReloadEventHandler(), directory, recursive=True)
|
||||||
|
|
||||||
def watch(self):
|
def watch(self):
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
|
Loading…
Reference in New Issue
Block a user