Skip to content

Commit 5da1dd7

Browse files
Add max_connections Parameter to Updater.start_webhook (python-telegram-bot#2547)
* Include max_connections args * Update docs & add test Co-authored-by: Hinrich Mahler <[email protected]>
1 parent 46cdeb4 commit 5da1dd7

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

telegram/ext/updater.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ def start_webhook(
363363
force_event_loop: bool = None,
364364
drop_pending_updates: bool = None,
365365
ip_address: str = None,
366+
max_connections: int = 40,
366367
) -> Optional[Queue]:
367368
"""
368369
Starts a small http server to listen for updates via webhook. If :attr:`cert`
@@ -411,6 +412,11 @@ def start_webhook(
411412
Since version 13.6, ``tornade>=6.1`` is required, which resolves the former
412413
issue.
413414
415+
max_connections (:obj:`int`, optional): Passed to
416+
:meth:`telegram.Bot.set_webhook`.
417+
418+
.. versionadded:: 13.6
419+
414420
Returns:
415421
:obj:`Queue`: The update queue that can be filled from the main thread.
416422
@@ -459,6 +465,7 @@ def start_webhook(
459465
allowed_updates,
460466
ready=webhook_ready,
461467
ip_address=ip_address,
468+
max_connections=max_connections,
462469
)
463470

464471
self.logger.debug('Waiting for Dispatcher and Webhook to start')
@@ -592,6 +599,7 @@ def _start_webhook(
592599
allowed_updates,
593600
ready=None,
594601
ip_address=None,
602+
max_connections: int = 40,
595603
):
596604
self.logger.debug('Updater thread started (webhook)')
597605

@@ -632,6 +640,7 @@ def _start_webhook(
632640
allowed_updates=allowed_updates,
633641
cert=cert_file,
634642
ip_address=ip_address,
643+
max_connections=max_connections,
635644
)
636645
if cert_file is not None:
637646
cert_file.close()
@@ -652,6 +661,7 @@ def _bootstrap(
652661
cert=None,
653662
bootstrap_interval=5,
654663
ip_address=None,
664+
max_connections: int = 40,
655665
):
656666
retries = [0]
657667

@@ -672,6 +682,7 @@ def bootstrap_set_webhook():
672682
allowed_updates=allowed_updates,
673683
ip_address=ip_address,
674684
drop_pending_updates=drop_pending_updates,
685+
max_connections=max_connections,
675686
)
676687
return False
677688

tests/test_updater.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,42 @@ def webhook_server_init(*args):
319319
updater.stop()
320320
assert self.test_flag == [True, True]
321321

322+
@pytest.mark.parametrize('pass_max_connections', [True, False])
323+
def test_webhook_max_connections(self, monkeypatch, updater, pass_max_connections):
324+
q = Queue()
325+
max_connections = 42
326+
327+
def set_webhook(**kwargs):
328+
print(kwargs)
329+
self.test_flag = kwargs.get('max_connections') == (
330+
max_connections if pass_max_connections else 40
331+
)
332+
return True
333+
334+
monkeypatch.setattr(updater.bot, 'set_webhook', set_webhook)
335+
monkeypatch.setattr(updater.bot, 'delete_webhook', lambda *args, **kwargs: True)
336+
monkeypatch.setattr('telegram.ext.Dispatcher.process_update', lambda _, u: q.put(u))
337+
338+
ip = '127.0.0.1'
339+
port = randrange(1024, 49152) # Select random port
340+
if pass_max_connections:
341+
updater.start_webhook(ip, port, webhook_url=None, max_connections=max_connections)
342+
else:
343+
updater.start_webhook(ip, port, webhook_url=None)
344+
345+
sleep(0.2)
346+
347+
# Now, we send an update to the server via urlopen
348+
update = Update(
349+
1,
350+
message=Message(1, None, Chat(1, ''), from_user=User(1, '', False), text='Webhook 2'),
351+
)
352+
self._send_webhook_msg(ip, port, update.to_json())
353+
sleep(0.2)
354+
assert q.get(False) == update
355+
updater.stop()
356+
assert self.test_flag is True
357+
322358
@pytest.mark.parametrize(('error',), argvalues=[(TelegramError(''),)], ids=('TelegramError',))
323359
def test_bootstrap_retries_success(self, monkeypatch, updater, error):
324360
retries = 2

0 commit comments

Comments
 (0)