@@ -13,6 +13,8 @@ const EventHandlerError = require('./event-handler-error');
13
13
const { EventHandlerErrorCode } = require ( '../common/result-code' ) ;
14
14
const BlockchainEvent = require ( './blockchain-event' ) ;
15
15
16
+ const CHANNEL_ID_RANDOM_NUMBER_RANGE = 1000 ;
17
+
16
18
class EventChannelManager {
17
19
constructor ( node ) {
18
20
this . node = node ;
@@ -107,7 +109,9 @@ class EventChannelManager {
107
109
`The number of event channels exceeds its limit ` +
108
110
`(${ NodeConfigs . MAX_NUM_EVENT_CHANNELS } )` ) ;
109
111
}
110
- const channelId = Date . now ( ) ; // NOTE: Only used in blockchain
112
+ // NOTE: Only used in blockchain
113
+ const channelId
114
+ = String ( Date . now ( ) + Math . floor ( Math . random ( ) * CHANNEL_ID_RANDOM_NUMBER_RANGE ) ) ;
111
115
if ( this . channels [ channelId ] ) { // TODO(cshcomcom): Retry logic.
112
116
webSocket . terminate ( ) ;
113
117
throw new EventHandlerError ( EventHandlerErrorCode . DUPLICATED_CHANNEL_ID ,
@@ -236,25 +240,15 @@ class EventChannelManager {
236
240
237
241
deregisterFilter ( channel , clientFilterId ) {
238
242
const filter = this . node . eh . deregisterEventFilter ( clientFilterId , channel . id ) ;
243
+ if ( ! filter ) {
244
+ return ;
245
+ }
239
246
channel . deleteEventFilterId ( filter . id ) ;
240
247
delete this . filterIdToChannelId [ filter . id ] ;
241
248
}
242
249
243
250
deregisterFilterAndEmitEvent ( channel , clientFilterId , filterDeletionReason ) {
244
- const LOG_HEADER = 'deregisterFilterAndEmitEvent' ;
245
- try {
246
- this . deregisterFilter ( channel , clientFilterId ) ;
247
- } catch ( err ) {
248
- logger . error ( `[${ LOG_HEADER } ] Can't deregister event filter ` +
249
- `(clientFilterId: ${ clientFilterId } , channelId: ${ channel . id } , ` +
250
- `err: ${ err . message } at ${ err . stack } )` ) ;
251
- throw new EventHandlerError (
252
- EventHandlerErrorCode . FAILED_TO_DEREGISTER_FILTER ,
253
- `Failed to deregister filter with filter ID: ${ clientFilterId } ` +
254
- `due to error: ${ err . message } ` ,
255
- clientFilterId
256
- ) ;
257
- }
251
+ this . deregisterFilter ( channel , clientFilterId ) ;
258
252
const blockchainEvent = new BlockchainEvent (
259
253
BlockchainEventTypes . FILTER_DELETED ,
260
254
{
@@ -344,6 +338,9 @@ class EventChannelManager {
344
338
// TODO(ehgmsdk20): reuse same object for memory
345
339
const eventObj = event . toObject ( ) ;
346
340
const clientFilterId = this . node . eh . getClientFilterIdFromGlobalFilterId ( eventFilterId ) ;
341
+ if ( ! clientFilterId ) {
342
+ return ;
343
+ }
347
344
Object . assign ( eventObj , { filter_id : clientFilterId } ) ;
348
345
this . transmitEventObj ( channel , eventObj ) ;
349
346
}
@@ -369,20 +366,18 @@ class EventChannelManager {
369
366
370
367
closeChannel ( channel ) {
371
368
const LOG_HEADER = 'closeChannel' ;
372
- try {
373
- logger . info ( `[${ LOG_HEADER } ] Closing channel ${ channel . id } ` ) ;
374
- channel . webSocket . terminate ( ) ;
375
- const filterIds = channel . getAllFilterIds ( ) ;
376
- for ( const filterId of filterIds ) {
377
- const clientFilterId = this . node . eh . getClientFilterIdFromGlobalFilterId ( filterId ) ;
378
- // NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
379
- this . deregisterFilter ( channel , clientFilterId ) ;
369
+ logger . info ( `[${ LOG_HEADER } ] Closing channel ${ channel . id } ` ) ;
370
+ channel . webSocket . terminate ( ) ;
371
+ const filterIds = channel . getAllFilterIds ( ) ;
372
+ for ( const filterId of filterIds ) {
373
+ const clientFilterId = this . node . eh . getClientFilterIdFromGlobalFilterId ( filterId ) ;
374
+ if ( ! clientFilterId ) {
375
+ continue ;
380
376
}
381
- delete this . channels [ channel . id ] ;
382
- } catch ( err ) {
383
- logger . error ( `[${ LOG_HEADER } ] Error while closing channel (channelId: ${ channel . id } , ` +
384
- `message:${ err . message } )` ) ;
377
+ // NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
378
+ this . deregisterFilter ( channel , clientFilterId ) ;
385
379
}
380
+ delete this . channels [ channel . id ] ;
386
381
}
387
382
388
383
startHeartbeat ( wsServer ) {
0 commit comments