Skip to content

Commit

Permalink
fix(irc-puppet): pick a random IPv6 address to connect to IRC hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueBrain committed Mar 14, 2024
1 parent 43d3ce6 commit 0c4c4c4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions dibridge/irc_puppet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import irc.client_aio
import logging
import random
import socket


Expand Down Expand Up @@ -135,14 +136,28 @@ async def reclaim_nick(self):
self._client.nick(self._nickname)

async def connect(self):
self._log.info("Connecting to IRC from %s ...", self._ipv6_address)

local_addr = (str(self._ipv6_address), 0)

while self._reconnect:
# As per RFC, libc sorts IPv6 results in some complicated way. In result,
# even if the IRC host has multiple IPv6 addresses listed, we will pick
# almost always the same one. This gives unneeded pressure on a single
# host, instead of distributing the load. So instead, we do the lookup
# ourselves, and pick a random one.
ipv6s = await self.loop.getaddrinfo(self._irc_host, None, socket.AF_INET6, socket.SOCK_STREAM, socket.IPPROTO_TCP)

Check failure on line 147 in dibridge/irc_puppet.py

View workflow job for this annotation

GitHub Actions / Testing / Flake8 / Flake8

line too long (126 > 120 characters)
if not ipv6s:
self._log.warning("Failed DNS lookup, retrying in 5 seconds")
# When we can't connect, try again in 5 seconds.
await asyncio.sleep(5)
continue

irc_host_ipv6 = random.choice(ipv6s)[4][0]

self._log.info("Connecting to IRC from %s to %s (%s) ...", self._ipv6_address, self._irc_host, irc_host_ipv6)

Check failure on line 156 in dibridge/irc_puppet.py

View workflow job for this annotation

GitHub Actions / Testing / Flake8 / Flake8

line too long (121 > 120 characters)

try:
await self.connection.connect(
self._irc_host,
irc_host_ipv6,
self._irc_port,
self._nickname,
username=self._username,
Expand Down

0 comments on commit 0c4c4c4

Please sign in to comment.