Set TCP_NODELAY to avoid needless buffering (#554)

This commit is contained in:
J. Nick Koston 2023-11-27 09:25:49 -06:00 committed by GitHub
parent e98252ff17
commit d3c2861e4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ import asyncio
import contextlib
import errno
import logging
import socket
import struct
from abc import ABC, abstractmethod
from pprint import pformat as pf
@ -107,6 +108,12 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
task = asyncio.open_connection(self.host, self.port)
async with asyncio_timeout(timeout):
self.reader, self.writer = await task
sock: socket.socket = self.writer.get_extra_info("socket")
# Ensure our packets get sent without delay as we do all
# our writes in a single go and we do not want any buffering
# which would needlessly delay the request or risk overloading
# the buffer on the device
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
async def _execute_query(self, request: str) -> Dict:
"""Execute a query on the device and wait for the response."""