Skip to content

Commit

Permalink
Rework receive buffer size increase
Browse files Browse the repository at this point in the history
  • Loading branch information
denis.kashitsyn committed Jun 18, 2024
1 parent 5311c88 commit d0f0392
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions python/mujinasync/asynctcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,9 @@ def SpinOnce(self, timeout=0):
socketConnections = {}
for serverClient in self._servers + self._clients:
for connection in serverClient._connections:
if connection.receiveBuffer.size < connection.receiveBuffer.capacity:
rsockets.append(connection.connectionSocket)
if connection.receiveBuffer.size >= connection.receiveBuffer.capacity:
connection.receiveBuffer.capacity *= 2
rsockets.append(connection.connectionSocket)
if connection.sendBuffer.size > 0:
wsockets.append(connection.connectionSocket)
xsockets.append(connection.connectionSocket)
Expand Down Expand Up @@ -364,30 +365,20 @@ def SpinOnce(self, timeout=0):
continue

serverClient, connection = socketConnections[rsocket]
totalReceived = 0
while True:
try:
received = rsocket.recv_into(connection.receiveBuffer.writeView)
if received == 0:
break
connection.receiveBuffer.size += received
totalReceived += received
if connection.receiveBuffer.capacity - connection.receiveBuffer.size < connection.receiveBuffer.capacity:
connection.receiveBuffer.capacity *= 2
except socket.error as e:
if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK):
connection.closeType = 'Immediate'
log.exception('error while trying to receive from connection %s: %s', connection, e)
break

if connection.closeType == 'Immediate':
try:
received = rsocket.recv_into(connection.receiveBuffer.writeView)
except socket.error as e:
if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK):
connection.closeType = 'Immediate'
log.exception('error while trying to receive from connection %s: %s', connection, e)
continue

if totalReceived == 0:
if received == 0:
connection.closeType = 'AfterSend'
log.debug('received nothing from connection, maybe closed: %s', connection)
continue

connection.receiveBuffer.size += received
receivedConnections.append((serverClient, connection))

# handle sockets that can write
Expand Down

0 comments on commit d0f0392

Please sign in to comment.