Skip to content

Commit

Permalink
Add message sending throttling.
Browse files Browse the repository at this point in the history
  • Loading branch information
shizmob committed Jan 30, 2014
1 parent 89bcc07 commit c2e27a4
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pydle/connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import os.path as path
import itertools
import time

import socket
import ssl
Expand All @@ -24,6 +25,9 @@
BUFFER_SIZE = 4096
TIMEOUT = 0.5

MESSAGE_THROTTLE_TIME = 2
MESSAGE_THROTTLE_TRESHOLD = 3


class NotConnected(Exception):
pass
Expand All @@ -41,6 +45,8 @@ def __init__(self, hostname, port, tls=False, tls_verify=True, encoding='utf-8',
self.port = port
self.ping_timeout = ping_timeout
self.encoding = encoding
self.unthrottled_messages = 0
self.last_message_sent = None

self.tls = tls
self.tls_context = None
Expand Down Expand Up @@ -80,6 +86,8 @@ def connect(self):
self.buffer = b''
with self.message_lock:
self.message_queue = collections.deque()
self.unthrottled_messages = 0
self.last_message_sent = None

def setup_tls(self):
""" Transform our regular socket into a TLS socket. """
Expand Down Expand Up @@ -257,7 +265,24 @@ def get_message(self, types=None, retry=False):
def send_message(self, command, *params, source=None):
""" Send a message to the other endpoint. """
message = protocol.construct(command, *params, source=source)
return self.send_string(message)
with self.message_lock:
# Should we throttle?
if self.last_message_sent and time.time() - self.last_message_sent < MESSAGE_THROTTLE_TIME:
if self.unthrottled_messages >= MESSAGE_THROTTLE_TRESHOLD:
# Enough messages unthrottled; we should throttle.
self.last_message_sent += MESSAGE_THROTTLE_TIME
timer = threading.Timer(self.last_message_sent - time.time(), self.send_string, [ message ])
timer.start()
else:
# We can still afford to not throttle.
self.unthrottled_messages += 1
self.last_message_sent = time.time()
return self.send_string(message)
else:
# We don't need to throttle.
self.unthrottled_messages = 0
self.last_message_sent = time.time()
return self.send_string(message)

def wait_for_message(self, types=None):
""" Wait until a message has arrived. """
Expand Down

0 comments on commit c2e27a4

Please sign in to comment.