-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apply idle time check to event handler channels #1293
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92615b5
Add EVENT_HANDLER_CHANNEL_IDLE_TIME_LIMIT_SECS node config parameter
platfowner e0e280a
Terminate websocket when there are errors in making connection
platfowner 21f8d58
Add EVENT_HANDLER_CHANNEL_IDLE_CHECK_INTERVAL_MS node config param
platfowner 0586942
Add event handler channel idle checking logic
platfowner 3e28237
Add remoteUrl and idleTimeMs to channel info
platfowner 30cd90b
Fix buildRemoteUrlFromSocket()
platfowner ade1d72
Use external ip address in the network info
platfowner 942b884
Add channelLifeTimeMs and channelIdleTimeMs to channel status
platfowner b3fcce6
Apply life time check to event handler channels
platfowner 3d8d103
Remove some dup code
platfowner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
const logger = new (require('../logger'))('EVENT_CHANNEL_MANAGER'); | ||
const EventChannel = require('./event-channel'); | ||
const ws = require('ws'); | ||
const { getIpAddress } = require('../common/network-util'); | ||
const { | ||
BlockchainEventMessageTypes, | ||
NodeConfigs, | ||
|
@@ -21,10 +20,11 @@ class EventChannelManager { | |
this.channels = {}; // [channelId]: Channel | ||
this.filterIdToChannelId = {}; // [globalFilterId]: channelId | ||
this.heartbeatInterval = null; | ||
this.idleCheckInterval = null; | ||
} | ||
|
||
getNetworkInfo() { | ||
const ipAddr = (NodeConfigs.HOSTING_ENV === HostingEnvs.COMCOM || NodeConfigs.HOSTING_ENV === HostingEnvs.LOCAL) ? this.node.ipAddrInternal : this.node.ipAddrExternal; | ||
const ipAddr = this.node.ipAddrExternal; | ||
const eventHandlerUrl = new URL(`ws://${ipAddr}:${NodeConfigs.EVENT_HANDLER_PORT}`); | ||
return { | ||
url: eventHandlerUrl.toString(), | ||
|
@@ -35,10 +35,42 @@ class EventChannelManager { | |
} | ||
} | ||
|
||
getChannelStatus() { | ||
const channelStats = this.getChannelStats(); | ||
return { | ||
maxNumEventChannels: NodeConfigs.MAX_NUM_EVENT_CHANNELS, | ||
numEventChannels: this.getNumEventChannels(), | ||
channelIdleTimeLimitSecs: NodeConfigs.EVENT_HANDLER_CHANNEL_IDLE_TIME_LIMIT_SECS, | ||
maxChannelLifeTimeMs: channelStats.maxLifeTimeMs, | ||
maxChannelIdleTimeMs: channelStats.maxIdleTimeMs, | ||
channelInfo: this.getChannelInfo(), | ||
} | ||
} | ||
|
||
getNumEventChannels() { | ||
return Object.keys(this.channels).length; | ||
} | ||
|
||
getChannelStats() { | ||
let maxLifeTimeMs = 0; | ||
let maxIdleTimeMs = 0; | ||
for (const channel of Object.values(this.channels)) { | ||
const lifeTimeMs = channel.getLifeTimeMs(); | ||
const idleTimeMs = channel.getIdleTimeMs(); | ||
if (lifeTimeMs > maxLifeTimeMs) { | ||
maxLifeTimeMs = lifeTimeMs; | ||
} | ||
if (idleTimeMs > maxIdleTimeMs) { | ||
maxIdleTimeMs = idleTimeMs; | ||
} | ||
} | ||
|
||
return { | ||
maxLifeTimeMs: maxLifeTimeMs, | ||
maxIdleTimeMs: maxIdleTimeMs, | ||
} | ||
} | ||
|
||
getChannelInfo() { | ||
const channelInfo = {}; | ||
for (const [channelId, channel] of Object.entries(this.channels)) { | ||
|
@@ -61,18 +93,21 @@ class EventChannelManager { | |
this.handleConnection(ws); | ||
}); | ||
this.startHeartbeat(this.wsServer); | ||
this.startIdleCheck(); | ||
} | ||
|
||
handleConnection(webSocket) { | ||
const LOG_HEADER = 'handleConnection'; | ||
try { | ||
if (this.getNumEventChannels() >= NodeConfigs.MAX_NUM_EVENT_CHANNELS) { | ||
webSocket.terminate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
throw new EventHandlerError(EventHandlerErrorCode.EVENT_CHANNEL_EXCEEDS_SIZE_LIMIT, | ||
`The number of event channels exceeds its limit ` + | ||
`(${NodeConfigs.MAX_NUM_EVENT_CHANNELS})`); | ||
} | ||
const channelId = Date.now(); // NOTE: Only used in blockchain | ||
if (this.channels[channelId]) { // TODO(cshcomcom): Retry logic. | ||
webSocket.terminate(); | ||
throw new EventHandlerError(EventHandlerErrorCode.DUPLICATED_CHANNEL_ID, | ||
`Channel ID ${channelId} is already in use`); | ||
} | ||
|
@@ -99,6 +134,7 @@ class EventChannelManager { | |
if (messageType === BlockchainEventMessageTypes.PONG) { | ||
this.handlePong(webSocket); | ||
} else { | ||
channel.setLastMessagingTimeMs(Date.now()); | ||
this.handleMessage(channel, messageType, messageData); | ||
} | ||
} catch (err) { | ||
|
@@ -308,6 +344,7 @@ class EventChannelManager { | |
close() { | ||
const LOG_HEADER = 'close'; | ||
this.stopHeartbeat(); | ||
this.stopIdleCheck(); | ||
this.wsServer.close(() => { | ||
logger.info(`[${LOG_HEADER}] Closed event channel manager's socket`); | ||
}); | ||
|
@@ -316,7 +353,7 @@ class EventChannelManager { | |
closeChannel(channel) { | ||
const LOG_HEADER = 'closeChannel'; | ||
try { | ||
logger.info(`[${LOG_HEADER}] Close channel ${channel.id}`); | ||
logger.info(`[${LOG_HEADER}] Closing channel ${channel.id}`); | ||
channel.webSocket.terminate(); | ||
const filterIds = channel.getAllFilterIds(); | ||
for (const filterId of filterIds) { | ||
|
@@ -326,7 +363,7 @@ class EventChannelManager { | |
} | ||
delete this.channels[channel.id]; | ||
} catch (err) { | ||
logger.error(`[${LOG_HEADER}] Error while close channel (channelId: ${channel.id}, ` + | ||
logger.error(`[${LOG_HEADER}] Error while closing channel (channelId: ${channel.id}, ` + | ||
`message:${err.message})`); | ||
} | ||
} | ||
|
@@ -340,7 +377,25 @@ class EventChannelManager { | |
ws.isAlive = false; | ||
this.sendPing(ws); | ||
}); | ||
}, NodeConfigs.EVENT_HANDLER_HEARTBEAT_INTERVAL_MS || 15000); | ||
}, NodeConfigs.EVENT_HANDLER_HEARTBEAT_INTERVAL_MS); | ||
} | ||
|
||
startIdleCheck() { | ||
const LOG_HEADER = 'startIdleCheck'; | ||
this.idleCheckInterval = setInterval(() => { | ||
for (const channel of Object.values(this.channels)) { | ||
const idleTimeMs = channel.getIdleTimeMs(); | ||
if (idleTimeMs > NodeConfigs.EVENT_HANDLER_CHANNEL_IDLE_TIME_LIMIT_SECS * 1000) { | ||
logger.info(`[${LOG_HEADER}] Closing long-idle channel: ${JSON.stringify(channel.toObject())}`); | ||
this.closeChannel(channel); | ||
} | ||
const lifeTimeMs = channel.getLifeTimeMs(); | ||
if (lifeTimeMs > NodeConfigs.EVENT_HANDLER_CHANNEL_LIFE_TIME_LIMIT_SECS * 1000) { | ||
logger.info(`[${LOG_HEADER}] Closing long-life channel: ${JSON.stringify(channel.toObject())}`); | ||
this.closeChannel(channel); | ||
} | ||
} | ||
}, NodeConfigs.EVENT_HANDLER_CHANNEL_IDLE_CHECK_INTERVAL_MS); | ||
} | ||
|
||
sendPing(webSocket) { | ||
|
@@ -351,6 +406,10 @@ class EventChannelManager { | |
stopHeartbeat() { | ||
clearInterval(this.heartbeatInterval); | ||
} | ||
|
||
stopIdleCheck() { | ||
clearInterval(this.idleCheckInterval); | ||
} | ||
} | ||
|
||
module.exports = EventChannelManager; |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍