Skip to content

Commit

Permalink
Merge pull request #1305 from ainblockchain/bugfix/platfowner/bugfix
Browse files Browse the repository at this point in the history
Reduce channel id conflict probability
  • Loading branch information
platfowner authored Aug 16, 2024
2 parents f17957b + 2396a74 commit 3195d06
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion event-handler/event-channel-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const EventHandlerError = require('./event-handler-error');
const { EventHandlerErrorCode } = require('../common/result-code');
const BlockchainEvent = require('./blockchain-event');

const CHANNEL_ID_RANDOM_NUMBER_RANGE = 1000;

class EventChannelManager {
constructor(node) {
this.node = node;
Expand Down Expand Up @@ -107,7 +109,9 @@ class EventChannelManager {
`The number of event channels exceeds its limit ` +
`(${NodeConfigs.MAX_NUM_EVENT_CHANNELS})`);
}
const channelId = Date.now(); // NOTE: Only used in blockchain
// NOTE: Only used in blockchain
const channelId
= String(Date.now() + Math.floor(Math.random() * CHANNEL_ID_RANDOM_NUMBER_RANGE));
if (this.channels[channelId]) { // TODO(cshcomcom): Retry logic.
webSocket.terminate();
throw new EventHandlerError(EventHandlerErrorCode.DUPLICATED_CHANNEL_ID,
Expand Down

0 comments on commit 3195d06

Please sign in to comment.