From 55d4ecdd5080fccaff9f8f93712d90f6468ccd10 Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Fri, 8 Jul 2016 11:34:12 -0400 Subject: [PATCH] Cascade IRC connections. This makes it so that, if we are joining 12 channels as 12 bots, we won't try to do them all at the same time. We'll connect to one, and then once that is started we'll wait 1 second and then start the second connection. The start of that one will wait 1 second and trigger the third, etc... --- fedmsg/consumers/ircbot.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/fedmsg/consumers/ircbot.py b/fedmsg/consumers/ircbot.py index 4f4be4ed..77dbb91a 100644 --- a/fedmsg/consumers/ircbot.py +++ b/fedmsg/consumers/ircbot.py @@ -28,6 +28,7 @@ from fedmsg.meta import _ import copy +import functools import re import time import pygments @@ -148,7 +149,7 @@ class Fedmsg2IRCFactory(protocol.ClientFactory): protocol = make_irc_client def __init__(self, channel, nickname, filters, - pretty, terse, short, rate, parent_consumer): + pretty, terse, short, rate, parent_consumer, ready): self.channel = channel self.nickname = nickname self.filters = filters @@ -157,8 +158,16 @@ def __init__(self, channel, nickname, filters, self.short = short self.rate = rate self.parent_consumer = parent_consumer + self.ready = ready self.log = logging.getLogger("moksha.hub") + def startedConnecting(self, connector): + if self.ready: + # If we're joining 12 channels, join one of them first. Once + # joining, wait one second and start joining the second one. That + # one should trigger joining the third one... + reactor.callLater(1, self.ready) + def clientConnectionLost(self, connector, reason): if self.parent_consumer.die: self.log.info("Lost connection. Not reconnecting to IRC.") @@ -196,6 +205,7 @@ def __init__(self, hub): return irc_settings = hub.config.get('irc') + callback = None # Keep track of the last factory we created for settings in irc_settings: network = settings.get('network', 'irc.freenode.net') port = settings.get('port', 6667) @@ -216,9 +226,22 @@ def __init__(self, hub): filters = self.compile_filters(settings.get('filters', None)) + factory = Fedmsg2IRCFactory( - channel, nickname, filters, pretty, terse, short, rate, self) - reactor.connectTCP(network, port, factory, timeout=timeout) + channel, nickname, filters, + pretty, terse, short, rate, + self, ready=callback, + ) + callback = functools.partial( + reactor.connectTCP, + network, port, factory, + timeout=timeout, + ) + + # Call only the very last one. + # When it is done, it will call the second to last one, which when it + # is done will call the third to last one, etc.. + callback() def add_irc_client(self, client): self.irc_clients.append(client)