2022-01-12 17:58:50 +01:00
|
|
|
import struct
|
|
|
|
import typing
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ElasticNotifierProtocol(asyncio.Protocol):
|
|
|
|
"""notifies the reader when ES has written updates"""
|
|
|
|
|
|
|
|
def __init__(self, listeners):
|
|
|
|
self._listeners = listeners
|
|
|
|
self.transport: typing.Optional[asyncio.Transport] = None
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
self.transport = transport
|
|
|
|
self._listeners.append(self)
|
2022-01-16 19:59:38 +01:00
|
|
|
log.info("got es notifier connection")
|
2022-01-12 17:58:50 +01:00
|
|
|
|
|
|
|
def connection_lost(self, exc) -> None:
|
|
|
|
self._listeners.remove(self)
|
|
|
|
self.transport = None
|
|
|
|
|
|
|
|
def send_height(self, height: int, block_hash: bytes):
|
2022-01-16 19:59:38 +01:00
|
|
|
log.info("notify es update '%s'", height)
|
|
|
|
self.transport.write(struct.pack(b'>Q32s', height, block_hash))
|
2022-01-12 17:58:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ElasticNotifierClientProtocol(asyncio.Protocol):
|
|
|
|
"""notifies the reader when ES has written updates"""
|
|
|
|
|
|
|
|
def __init__(self, notifications: asyncio.Queue):
|
|
|
|
self.notifications = notifications
|
|
|
|
self.transport: typing.Optional[asyncio.Transport] = None
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.transport and not self.transport.is_closing():
|
|
|
|
self.transport.close()
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
self.transport = transport
|
2022-01-16 19:59:38 +01:00
|
|
|
log.info("connected to es notifier")
|
2022-01-12 17:58:50 +01:00
|
|
|
|
|
|
|
def connection_lost(self, exc) -> None:
|
|
|
|
self.transport = None
|
|
|
|
|
|
|
|
def data_received(self, data: bytes) -> None:
|
2022-01-16 19:59:38 +01:00
|
|
|
try:
|
|
|
|
height, block_hash = struct.unpack(b'>Q32s', data)
|
|
|
|
except:
|
|
|
|
log.exception("failed to decode %s", (data or b'').hex())
|
|
|
|
raise
|
2022-01-12 17:58:50 +01:00
|
|
|
self.notifications.put_nowait((height, block_hash))
|