Skip to content

Commit ee34d57

Browse files
authored
Warn on small con_pool_size during custom initalization of Updater (python-telegram-bot#793)
fixes python-telegram-bot#787
1 parent 5d7c6ad commit ee34d57

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

telegram/ext/updater.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ def __init__(self,
9595
if (token is not None) and (bot is not None):
9696
raise ValueError('`token` and `bot` are mutually exclusive')
9797

98+
self.logger = logging.getLogger(__name__)
99+
100+
con_pool_size = workers + 4
101+
98102
if bot is not None:
99103
self.bot = bot
104+
if bot.request.con_pool_size < con_pool_size:
105+
self.logger.warning(
106+
'Connection pool of Request object is smaller than optimal value (%s)',
107+
con_pool_size)
100108
else:
101109
# we need a connection pool the size of:
102110
# * for each of the workers
@@ -107,7 +115,7 @@ def __init__(self,
107115
if request_kwargs is None:
108116
request_kwargs = {}
109117
if 'con_pool_size' not in request_kwargs:
110-
request_kwargs['con_pool_size'] = workers + 4
118+
request_kwargs['con_pool_size'] = con_pool_size
111119
self._request = Request(**request_kwargs)
112120
self.bot = Bot(token, base_url, request=self._request)
113121
self.user_sig_handler = user_sig_handler
@@ -121,7 +129,6 @@ def __init__(self,
121129
workers=workers,
122130
exception_event=self.__exception_event)
123131
self.last_update_id = 0
124-
self.logger = logging.getLogger(__name__)
125132
self.running = False
126133
self.is_idle = False
127134
self.httpd = None

telegram/utils/request.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def __init__(self,
8686
sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30))
8787
sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 8))
8888

89+
self._con_pool_size = con_pool_size
90+
8991
kwargs = dict(
9092
maxsize=con_pool_size,
9193
cert_reqs='CERT_REQUIRED',
@@ -126,6 +128,11 @@ def __init__(self,
126128

127129
self._con_pool = mgr
128130

131+
@property
132+
def con_pool_size(self):
133+
"""The size of the connection pool used."""
134+
return self._con_pool_size
135+
129136
def stop(self):
130137
self._con_pool.clear()
131138

0 commit comments

Comments
 (0)