diff -uNr a/logotron/MANIFEST.TXT b/logotron/MANIFEST.TXT --- a/logotron/MANIFEST.TXT b22dbb3579aab3629907f6ee3690c6efa9fc883f48f95591db1242755c5cbb34a75cced595562444b472b1de5448d9652c9b7335e28ffd2531550a1af670da84 +++ b/logotron/MANIFEST.TXT 3e4be8754ace445890e17db34676ec4e885e05173534d9a1bd244b1257d9261a7db79807fa956ab84d538be8e4993bdccd4f114db726e38398c0e2069c0007f5 @@ -8,3 +8,4 @@ 590714 znc2tmsr_etc lobbes "Converter of znc logs to the tmsr format used by the logotron. Small fixes to eat_dump.py." 593779 uptimefix_bye_cache lobbes "Fix in bot.py for global time_last_conn. Remove Cache from reader.py. Small README fix re: create database." 594463 raw_line_fix lobbes "Fix to raw line export in reader.py to remove semi-colon from right side of nick on action lines" +594806 active_disconnect bvt "Close current connection before opening a new one. Disable Nagle's algorithm. s/Listen/Receive/." diff -uNr a/logotron/bot.py b/logotron/bot.py --- a/logotron/bot.py 19772a67f431072de4c51c6600d860c8cbb77f4e5172f4c20f43078565eda7c07c599e165ee588552741a703cbd13b1043d6c2850858cbe2cc9d3f5249f54c8f +++ b/logotron/bot.py 76b64752340996cf7c8219f2ddf28d55b6122ecab06fa30bca38f91ac2d82abedbb5a7076f7159496cd57471c9550b0a4b921d6bd6756d8a1006875f37c58c6b @@ -124,15 +124,32 @@ # Used to compute 'uptime' time_last_conn = datetime.now() -# Init socket: -sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - -# Set keepalive: -sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) +# Socket will be here: +sock = None; # Initially we are not connected to anything connected = False +def init_socket(): + global sock + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + # Disable Nagle's algorithm for transmit operations + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + # Disable Nagle's algorithm for receive operation, Linux-only + try: + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_QUICKACK, 1) + except Exception as e: + logging.warning(e) + + connected = False + +def deinit_socket(): + global connected + global sock + sock.close() + connected = False + # Connect to given host:port; return whether connected def connect(host, port): logging.info("Connecting to %s:%s" % (host, port)) @@ -171,7 +188,7 @@ sock.send(message.encode("utf-8")) except (socket.timeout, socket.error) as e: logging.warning("Socket could not send! Disconnecting.") - connected = False + deinit_socket() return False except Exception as e: logging.exception(e) @@ -219,6 +236,10 @@ def irc(): global connected global time_last_conn + global sock + + # Initialize a socket + init_socket() # Connect to one among the specified servers, in given priority : while not connected: @@ -246,20 +267,20 @@ try: data = sock.recv(Buf_Size) except socket.timeout as e: - logging.debug("Listen timed out") + logging.debug("Receive timed out") continue except socket.error as e: - logging.warning("Listen socket error, disconnecting.") - connected = False + logging.warning("Receive socket error, disconnecting.") + deinit_socket() continue except Exception as e: logging.exception(e) - connected = False + deinit_socket() continue else: if len(data) == 0: - logging.warning("Listen socket closed, disconnecting.") - connected = False + logging.warning("Receive socket closed, disconnecting.") + deinit_socket() continue try: try: