Skip to content

Commit

Permalink
Cascade IRC connections.
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
ralphbean committed Jul 8, 2016
1 parent 9889351 commit 55d4ecd
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions fedmsg/consumers/ircbot.py
Expand Up @@ -28,6 +28,7 @@
from fedmsg.meta import _

import copy
import functools
import re
import time
import pygments
Expand Down Expand Up @@ -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
Expand All @@ -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.")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 55d4ecd

Please sign in to comment.