- while True:
- # We want to be kind to the IRC servers and not hold unused
- # sockets open forever, so they have a time-to-live. The
- # loop is coded this particular way so that we can drop
- # the actual server connection when its time-to-live
- # expires, then reconnect and resume transmission if the
- # queue fills up again.
- if not self.connection:
- self.connection = self.irker.irc.server()
- self.connection.context = self
- self.nick_trial = 1
- self.channels_joined = []
- # This will throw irc.client.ServerConnectionError on failure
- try:
- self.connection.connect(self.servername,
- self.port,
- nickname=self.nickname(),
- username="irker",
- ircname="irker relaying client")
- self.status = "handshaking"
- self.irker.debug(1, "XMIT_TTL bump (%s connection) at %s" % (self.servername, time.asctime()))
- self.last_xmit = time.time()
- except irc.client.ServerConnectionError:
- self.status = "disconnected"
- elif self.status == "handshaking":
- # Don't buzz on the empty-queue test while we're handshaking
- time.sleep(ANTI_BUZZ_DELAY)
- elif self.queue.empty():
- # Queue is empty, at some point we want to time out
- # the connection rather than holding a socket open in
- # the server forever.
- now = time.time()
- if now > self.last_xmit + XMIT_TTL \
- or now > self.last_ping + PING_TTL:
- self.irker.debug(1, "timing out inactive connection to %s at %s" % (self.servername, time.asctime()))
- self.connection.context = None
- self.connection.close()
- self.connection = None
- self.status = "disconnected"
- else:
- # Prevent this thread from hogging the CPU by pausing
- # for just a little bit after the queue-empty check.
- # As long as this is less that the duration of a human
- # reflex arc it is highly unlikely any human will ever
- # notice.
- time.sleep(ANTI_BUZZ_DELAY)
- elif self.status == "disconnected" \
- and time.time() > self.last_xmit + DISCONNECT_TTL:
- # Queue is nonempty, but the IRC server might be down. Letting
- # failed connections retain queue space forever would be a
- # memory leak.
- self.status = "expired"
- break
- elif self.status == "unseen" \
- and time.time() > self.last_xmit + UNSEEN_TTL:
- # Nasty people could attempt a denial-of-service
- # attack by flooding us with requests with invalid
- # servernames. We guard against this by rapidly
- # expiring connections that have a nonempty queue but
- # have never had a successful open.
- self.status = "expired"
- break
- elif self.status == "ready":
- (channel, message) = self.queue.get()
- if channel not in self.channels_joined:
- self.channels_joined.append(channel)
- self.connection.join(channel)
- for segment in message.split("\n"):
- self.connection.privmsg(channel, segment)
- self.last_xmit = time.time()
- self.irker.debug(1, "XMIT_TTL bump (%s transmission) at %s" % (self.servername, time.asctime()))
- self.queue.task_done()
- time.sleep(ANTI_FLOOD_DELAY)
+ try:
+ while True:
+ # We want to be kind to the IRC servers and not hold unused
+ # sockets open forever, so they have a time-to-live. The
+ # loop is coded this particular way so that we can drop
+ # the actual server connection when its time-to-live
+ # expires, then reconnect and resume transmission if the
+ # queue fills up again.
+ if self.queue.empty():
+ # Queue is empty, at some point we want to time out
+ # the connection rather than holding a socket open in
+ # the server forever.
+ now = time.time()
+ xmit_timeout = now > self.last_xmit + XMIT_TTL
+ ping_timeout = now > self.last_ping + PING_TTL
+ if (xmit_timeout or ping_timeout) and self.status != "disconnected":
+ self.irker.debug(1, "timing out connection to %s at %s (ping_timeout=%s, xmit_timeout=%s)" % (self.servername, time.asctime(), ping_timeout, xmit_timeout))
+ with self.irker.library_lock:
+ self.connection.context = None
+ self.connection.quit("transmission timeout")
+ self.connection.close()
+ self.connection = None
+ self.status = "disconnected"
+ else:
+ # Prevent this thread from hogging the CPU by pausing
+ # for just a little bit after the queue-empty check.
+ # As long as this is less that the duration of a human
+ # reflex arc it is highly unlikely any human will ever
+ # notice.
+ time.sleep(ANTI_BUZZ_DELAY)
+ elif not self.connection:
+ # Queue is nonempty but server isn't connected.
+ with self.irker.library_lock:
+ self.connection = self.irker.irc.server()
+ self.connection.context = self
+ # Try to avoid colliding with other instances
+ self.nick_trial = random.randint(1, 990)
+ self.channels_joined = {}
+ try:
+ # This will throw
+ # irc.client.ServerConnectionError on failure
+ self.connection.connect(self.servername,
+ self.port,
+ nickname=self.nickname(),
+ username="irker",
+ ircname="irker relaying client")
+ self.status = "handshaking"
+ self.irker.debug(1, "XMIT_TTL bump (%s connection) at %s" % (self.servername, time.asctime()))
+ self.last_xmit = time.time()
+ except irc.client.ServerConnectionError:
+ self.status = "disconnected"
+ elif self.status == "handshaking":
+ if time.time() > self.last_xmit + HANDSHAKE_TTL:
+ self.status = "expired"
+ break
+ else:
+ # Don't buzz on the empty-queue test while we're
+ # handshaking
+ time.sleep(ANTI_BUZZ_DELAY)
+ elif self.status == "disconnected" \
+ and time.time() > self.last_xmit + DISCONNECT_TTL:
+ # Queue is nonempty, but the IRC server might be
+ # down. Letting failed connections retain queue
+ # space forever would be a memory leak.
+ self.status = "expired"
+ break
+ elif self.status == "unseen" \
+ and time.time() > self.last_xmit + UNSEEN_TTL:
+ # Nasty people could attempt a denial-of-service
+ # attack by flooding us with requests with invalid
+ # servernames. We guard against this by rapidly
+ # expiring connections that have a nonempty queue but
+ # have never had a successful open.
+ self.status = "expired"
+ break
+ elif self.status == "ready":
+ with self.irker.library_lock:
+ (channel, message) = self.queue.get()
+ if channel not in self.channels_joined:
+ self.connection.join(channel)
+ self.irker.debug(1, "joining %s on %s." % (channel, self.servername))
+ for segment in message.split("\n"):
+ self.connection.privmsg(channel, segment)
+ time.sleep(ANTI_FLOOD_DELAY)
+ self.last_xmit = self.channels_joined[channel] = time.time()
+ self.irker.debug(1, "XMIT_TTL bump (%s transmission) at %s" % (self.servername, time.asctime()))
+ self.queue.task_done()
+ except:
+ (exc_type, _exc_value, exc_traceback) = sys.exc_info()
+ self.irker.logerr("exception %s in thread for %s" % \
+ (exc_type, self.servername))
+ # This is so we can see tracebacks for errors inside the thread
+ # when we need to be able to for debugging purposes.
+ if debuglvl > 0:
+ raise exc_type, _exc_value, exc_traceback