Skip to content

Commit 98b11d5

Browse files
authored
Merge pull request #81 from nazarkhanov/fix_bug_with_not_sent_logs
Add a wait until all data is sent, before closing socket on TCP transport
2 parents 40ab8e7 + 71d2e01 commit 98b11d5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

logstash_async/transport.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
from abc import ABC, abstractmethod
77
from typing import Iterator, Union
8+
import fcntl
89
import json
910
import logging
1011
import socket
1112
import ssl
13+
import struct
14+
import termios
15+
import time
1216

1317
from requests.auth import HTTPBasicAuth
1418
import pylogbeat
@@ -122,9 +126,19 @@ def _convert_data_to_send(self, data):
122126
def _close(self, force=False):
123127
if not self._keep_connection or force:
124128
if self._sock:
129+
while not self._is_sock_write_buff_empty():
130+
time.sleep(0.05)
125131
self._sock.close()
126132
self._sock = None
127133

134+
# ----------------------------------------------------------------------
135+
def _is_sock_write_buff_empty(self):
136+
socket_fd = self._sock.fileno()
137+
buffer_size = struct.pack('I', 0)
138+
ioctl_result = fcntl.ioctl(socket_fd, termios.TIOCOUTQ, buffer_size)
139+
buffer_size = struct.unpack('I', ioctl_result)[0]
140+
return not buffer_size
141+
128142
# ----------------------------------------------------------------------
129143
def close(self):
130144
self._close(force=True)

0 commit comments

Comments
 (0)