我有一个无服务器的函数,它试图向Kafka发送一些数据。有时它可以工作,有时连接中断,数据丢失。
原因是kafka库没有引发异常,而是添加了错误日志。所以我不能在 try:except
.
以下是我在日志中经常遇到的错误:
<BrokerConnection node_id=11 host=... port=9092>: Error receiving network data closing socket
Traceback (most recent call last):
File "/var/task/kafka/conn.py", line 745, in _recv
data = self._sock.recv(SOCK_CHUNK_BYTES)
ConnectionResetError: [Errno 104] Connection reset by peer
以及功能 _recv
上述定义如下:
我仍然在寻找一个解决方案,但添加代码内try:except doesn不起作用。
def _recv(self):
responses = []
SOCK_CHUNK_BYTES = 4096
while True:
try:
data = self._sock.recv(SOCK_CHUNK_BYTES)
# We expect socket.recv to raise an exception if there is not
# enough data to read the full bytes_to_read
# but if the socket is disconnected, we will get empty data
# without an exception raised
if not data:
log.error('%s: socket disconnected', self)
self.close(error=Errors.ConnectionError('socket disconnected'))
break
else:
responses.extend(self.receive_bytes(data))
if len(data) < SOCK_CHUNK_BYTES:
break
except SSLWantReadError:
break
except ConnectionError as e:
if six.PY2 and e.errno == errno.EWOULDBLOCK:
break
log.exception('%s: Error receiving network data'
' closing socket', self)
self.close(error=Errors.ConnectionError(e))
break
except BlockingIOError:
if six.PY3:
break
raise
return responses
我希望当连接丢失时,会引发异常,应用程序会中断,但实际上只有错误日志,即使kafka部分失败,应用程序也会继续。
暂无答案!
目前还没有任何答案,快来回答吧!