How to make fabric-network addBlockListener reconnect automatically when losing connection to the peer?

364 views Asked by At

Using the fabric-network NodeJS sdk v2.2, I'm implementing a block listener that has to catch all blocks and parse them.

It should be resilient and automatically reconnect when the peer goes down or is unreachable for a moment. In previous versions of the SDK, we could just use the 'onError' callback and reconnect. Now, that part is abstracted away, you only have something like:

network.addBlockListener(async (blockEvent: BlockEvent) => {
  console.log(blockEvent.blockNumber);
});

When I kill the peer, the Gateway and Network are smart enough to reconnect automatically. The blocklistener however is lost. For completeness, here's the error logs when deleting the peer pod.

[ServiceEndpoint]: Error: Failed to connect before the deadline on Eventer- name: peer-0.peer.org1, url:grpcs://peer-0.peer.org1:7051, connected:false, connectAttempted:true
[ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer-0.peer.org1 url:grpcs://peer-0.peer.org1:7051 timeout:3000
[Eventer]: checkConnection[peer-0.peer.org1:3] Event Service grpcs://peer-0.peer.org1:7051 Connection check failed :: Error: Failed to connect before the deadline on Eventer- name: peer-0.peer.org1, url:grpcs://peer-0.peer.org1:7051, connected:false, connectAttempted:true
[EventService]: send[peer-0.peer.org1] - #4 - no targets started - Error: Event service peer-0.peer.org1 is not connected
[BlockEventSource]: Failed to start event service message=Event service peer-0.peer.org1 is not connected, stack=Error: Event service peer-0.peer.org1 is not connected
[ServiceEndpoint]: Error: Failed to connect before the deadline on Eventer- name: peer-0.peer.org1, url:grpcs://peer-0.peer.org1:7051, connected:false, connectAttempted:true
[ServiceEndpoint]: waitForReady - Failed to connect to remote gRPC server peer-0.peer.org1 url:grpcs://peer-0.peer.org1:7051 timeout:3000

After it restarted, I can execute transactions again but the block events aren't coming through anymore.

Is there a way to either catch a kind of 'disconnect' event, to easily create a custom block event listener with connection error handling or another way to reconnect blocklisteners automatically?

1

There are 1 answers

0
bestbeforetoday On

Peer disconnections should be handled transparently by the v2.2 SDK with no need for you to handle reconnection in your block listener. Ordering and deduplication of block events is also handled internally so your listener should be completely unaware of any failover between eventing peers.

If this is not working with the latest v2.2.x release then please raise a bug report in Jira with as much detail as possible to allow the issue to be reproduced: https://jira.hyperledger.org/projects/FABN