Skip to content

Commit 68a7d9f

Browse files
committed
Make sure to hint Telegram servers that we reuse connections
refs python-telegram-bot#495 refs python-telegram-bot#364
1 parent 48bcc31 commit 68a7d9f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

telegram/utils/request.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ def _request_wrapper(self, *args, **kwargs):
128128
TelegramError
129129
130130
"""
131+
# Make sure to hint Telegram servers that we reuse connections by sending
132+
# "Connection: keep-alive" in the HTTP headers.
133+
if 'headers' not in kwargs:
134+
kwargs['headers'] = {}
135+
kwargs['headers']['connection'] = 'keep-alive'
136+
131137
try:
132138
resp = self._con_pool.request(*args, **kwargs)
133139
except urllib3.exceptions.TimeoutError:
@@ -196,11 +202,8 @@ def post(self, url, data, timeout=None):
196202

197203
if InputFile.is_inputfile(data):
198204
data = InputFile(data)
199-
result = self._request_wrapper('POST',
200-
url,
201-
body=data.to_form(),
202-
headers=data.headers,
203-
**urlopen_kwargs)
205+
result = self._request_wrapper(
206+
'POST', url, body=data.to_form(), headers=data.headers, **urlopen_kwargs)
204207
else:
205208
data = json.dumps(data)
206209
result = self._request_wrapper(

0 commit comments

Comments
 (0)