mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-23 02:41:55 +00:00
Implement basic rate limiting for webide autoreload magic
This commit is contained in:
parent
955e1c1cf3
commit
dceb4b2b7e
@ -1,3 +1,4 @@
|
||||
from time import time
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
@ -8,21 +9,32 @@ log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WebideReloadEventHandler(FileSystemEventHandler):
|
||||
def __init__(self):
|
||||
def __init__(self, rate_per_second):
|
||||
super().__init__()
|
||||
self.uplink = ServerUplinkConnector()
|
||||
|
||||
self.min_interval = 1 / float(rate_per_second)
|
||||
self.last_call = time()
|
||||
|
||||
def on_modified(self, event):
|
||||
if self.limit_rate(): return
|
||||
|
||||
log.debug(event)
|
||||
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
|
||||
to_next_call = self.min_interval - since_last_call
|
||||
self.last_call = time()
|
||||
return to_next_call > 0
|
||||
|
||||
|
||||
class DirectoryMonitor:
|
||||
def __init__(self, directory):
|
||||
self.observer = Observer()
|
||||
self.observer.schedule(WebideReloadEventHandler(), directory, recursive=True)
|
||||
self.observer.schedule(WebideReloadEventHandler(rate_per_second=5), directory, recursive=True)
|
||||
|
||||
def watch(self):
|
||||
self.observer.start()
|
||||
|
Loading…
Reference in New Issue
Block a user