-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FABN-910 Connect EventHubs up front in network api
Connect to the event hubs as early as possible when creating a network object to optimize the performance of the submitTransaction method when it is later called. Also disconnect the event hubs when the gateway object is disconnected to cleanup resources Change-Id: If271dd95bfee2e2893dbabc2acec33ca968232f5 Signed-off-by: andrew-coleman <[email protected]>
- Loading branch information
1 parent
399bd25
commit 828c718
Showing
11 changed files
with
334 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
fabric-network/lib/impl/event/defaulteventhandlermanager.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright 2018 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const EventHandlerStrategies = require('./defaulteventhandlerstrategies'); | ||
const TransactionEventHandler = require('./transactioneventhandler'); | ||
const EventHubFactory = require('./eventhubfactory'); | ||
const logger = require('../../logger').getLogger('DefaultEventHandlerManager'); | ||
|
||
class DefaultEventHandlerManager { | ||
|
||
constructor(network, mspId, options) { | ||
this.network = network; | ||
this.channel = network.channel; | ||
this.peerMap = network.peerMap; | ||
this.options = options; | ||
this.mspId = mspId; | ||
|
||
if (!this.options.strategy) { | ||
this.options.strategy = EventHandlerStrategies.MSPID_SCOPE_ALLFORTX; | ||
} | ||
|
||
logger.debug('constructor: mspId = %s, options = %O', mspId, this.options); | ||
} | ||
|
||
async initialize() { | ||
this.availableEventHubs = []; | ||
if (!this.initialized) { | ||
this.useFullBlocks = this.options.useFullBlocks || this.options.chaincodeEventsEnabled; | ||
if (this.useFullBlocks === null || this.useFullBlocks === undefined) { | ||
this.useFullBlocks = false; | ||
} | ||
|
||
logger.debug('initialize: useFullBlocks = %s', this.useFullBlocks); | ||
|
||
const eventHubFactory = new EventHubFactory(this.channel); | ||
this.eventStrategy = this.options.strategy(eventHubFactory, this.network, this.mspId); | ||
this.availableEventHubs = await this.eventStrategy.getConnectedEventHubs(); | ||
|
||
this.initialized = true; | ||
|
||
logger.debug('initialize: useFullBlocks = %j, availableEventHubs = %O', this.useFullBlocks, this.availableEventHubs); | ||
} | ||
} | ||
|
||
dispose() { | ||
logger.debug('dispose'); | ||
this.disconnectEventHubs(); | ||
this.availableEventHubs = []; | ||
this.initialized = false; | ||
} | ||
|
||
getEventHubs() { | ||
return this.availableEventHubs; | ||
} | ||
|
||
disconnectEventHubs() { | ||
for (const hub of this.availableEventHubs) { | ||
try { | ||
hub.disconnect(); | ||
} catch (error) { | ||
// | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* create an Tx Event handler for the specific txid | ||
* | ||
* @param {*} txid | ||
* @returns | ||
* @memberof DefaultEventHandlerFactory | ||
*/ | ||
createTxEventHandler(txid) { | ||
logger.debug('createTxEventHandler: txid = %s', txid); | ||
// pass in all available eventHubs to listen on, the handler decides when to resolve based on strategy | ||
// a TxEventHandler should check that the available ones are usable when appropriate. | ||
return new TransactionEventHandler(this, txid); | ||
} | ||
|
||
} | ||
|
||
module.exports = DefaultEventHandlerManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.