Skip to content

Commit

Permalink
Use a thread pool and add timeout to web requests
Browse files Browse the repository at this point in the history
  • Loading branch information
luk3yx committed Sep 26, 2021
1 parent 1b2a7fe commit d78a867
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lurklite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# lurklite core
#

import os, miniirc, re, sys, time
import concurrent.futures, os, miniirc, re, sys, time
import lurklite.tempcmds as tempcmds
static_cmds = None

# The version
miniirc.version = f'lurklite v0.4.19 (powered by {miniirc.version})'
miniirc.version = f'lurklite v0.4.20 (powered by {miniirc.version})'

# Throw errors
class BotError(Exception):
Expand Down Expand Up @@ -209,6 +209,8 @@ def __init__(self, config, *, debug=False):
self.disable_yay = self._conf_bool('core', 'disable_yay')
self.disable_ouch = self._conf_bool('core', 'disable_ouch')

thread_pool = concurrent.futures.ThreadPoolExecutor(32)

# Get the IRC servers to connect to
_servers = {}
kwargs = None
Expand Down Expand Up @@ -238,7 +240,7 @@ def __init__(self, config, *, debug=False):
# Create the IRC object
irc = miniirc.IRC(c['ip'], int(c['port']), c['nick'],
c['channels'].split(','), auto_connect=False,
debug=debug, **kwargs)
debug=debug, executor=thread_pool, **kwargs)
_servers[section] = irc

# Add the ignores list
Expand All @@ -265,7 +267,7 @@ def __init__(self, config, *, debug=False):

# Create the Discord object
irc = miniirc_discord.Discord(c['token'], 0,
c.get('nick', '???'), debug=debug, **kw)
c.get('nick', '???'), debug=debug, executor=thread_pool, **kw)
_servers['Discord'] = irc

# Add the ignores list
Expand All @@ -284,7 +286,5 @@ def __init__(self, config, *, debug=False):
f'{exc.__class__.__name__}: {exc}')
irc.debug('Finished connecting to servers!')

# miniirc update reminder™
assert miniirc.ver >= (1,4,0), 'lurklite requires miniirc >= v1.4.0!'
if miniirc.ver < (1,6,2):
print('You are not running the latest version of miniirc™.')
# Ensure miniirc isn't outdated
assert miniirc.ver >= (1,7,0), 'lurklite requires miniirc >= v1.7.0!'
3 changes: 2 additions & 1 deletion lurklite/tempcmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ def _command_url(irc, hostmask, channel, code, args):
code = code.format(*[web_quote(a) for a in args],
args = web_quote(' '.join(args)), nick = web_quote(hostmask[0]))

data = urllib.request.urlopen(code).read().decode('utf-8', 'replace')
with urllib.request.urlopen(code, timeout=5) as res:
data = res.read().decode('utf-8', 'replace')

while data[-1:] in '\r\n':
data = data[:-1]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name = 'lurklite',
version = '0.4.19',
version = '0.4.20',
packages = ['lurklite'],
author = 'luk3yx',
description = 'A miniirc-based IRC bot.',
Expand All @@ -18,7 +18,7 @@

long_description = desc,
long_description_content_type = 'text/markdown',
install_requires = ['miniirc>=1.4.0', 'msgpack'],
install_requires = ['miniirc>=1.7.0', 'msgpack'],
python_requires = '>=3.6',

classifiers = [
Expand Down

0 comments on commit d78a867

Please sign in to comment.