Skip to content

Commit

Permalink
Queue updates within 30 second to avoid network spam
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcutme committed Mar 30, 2016
1 parent c8b949c commit 0e13fbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Ui/UiWebsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,25 +319,39 @@ def actionSitePublish(self, to, privatekey=None, inner_path="content.json", sign
self.site.announce()

event_name = "publish %s %s" % (self.site.address, inner_path)
thread = RateLimit.callAsync(event_name, 7, self.site.publish, 5, inner_path) # Only publish once in 7 second to 5 peers
called_instantly = RateLimit.isAllowed(event_name, 30)
thread = RateLimit.callAsync(event_name, 30, self.site.publish, 5, inner_path) # Only publish once in 30 seconds to 5 peer
notification = "linked" not in dir(thread) # Only display notification on first callback
thread.linked = True
thread.link(lambda thread: self.cbSitePublish(to, thread, notification)) # At the end callback with request id and thread
if called_instantly: # Allowed to call instantly
# At the end callback with request id and thread
thread.link(lambda thread: self.cbSitePublish(to, thread, notification, callback=notification))
else:
self.cmd(
"notification",
["info", "Content publish queued for %.0f seconds." % RateLimit.delayLeft(event_name, 30), 5000]
)
self.response(to, "ok")
# At the end display notification
thread.link(lambda thread: self.cbSitePublish(to, thread, notification, callback=False))


# Callback of site publish
def cbSitePublish(self, to, thread, notification=True):
def cbSitePublish(self, to, thread, notification=True, callback=True):
site = self.site
published = thread.value
if published > 0: # Successfuly published
if notification:
self.cmd("notification", ["done", "Content published to %s peers." % published, 5000])
self.response(to, "ok")
site.updateWebsocket() # Send updated site data to local websocket clients
if callback:
self.response(to, "ok")
else:
if len(site.peers) == 0:
if sys.modules["main"].file_server.port_opened or sys.modules["main"].file_server.tor_manager.start_onions:
if notification:
self.cmd("notification", ["info", "No peers found, but your content is ready to access.", 5000])
if callback:
self.response(to, "ok")
else:
if notification:
Expand All @@ -346,6 +360,7 @@ def cbSitePublish(self, to, thread, notification=True):
"""Your network connection is restricted. Please, open <b>%s</b> port <br>
on your router to make your site accessible for everyone.""" % config.fileserver_port
])
if callback:
self.response(to, {"error": "Port not opened."})

else:
Expand Down
6 changes: 6 additions & 0 deletions src/util/RateLimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def isAllowed(event, allowed_again=10):
else:
return False

def delayLeft(event, allowed_again=10):
last_called = called_db.get(event)
if not last_called: # Its not called before
return 0
else:
return allowed_again - (time.time() - last_called)

def callQueue(event):
func, args, kwargs, thread = queue_db[event]
Expand Down

0 comments on commit 0e13fbf

Please sign in to comment.