This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
WIP: Decorators - Load Balance #204
Draft
fbadiola
wants to merge
2
commits into
altangent:master
Choose a base branch
from
fbadiola:feature/limit-subscriptions-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const { EventEmitter } = require('events'); | ||
|
||
const CHANNELS = [ | ||
'connecting', | ||
'connected', | ||
'disconnected', | ||
'reconnecting', | ||
'closing', | ||
'closed', | ||
'error', | ||
'trade', | ||
'ticker', | ||
'candle', | ||
'l2update', | ||
'l2snapshot', | ||
]; | ||
|
||
/** | ||
* AbstractClient will expose all the methods that need to be populated by specific implementations. | ||
* Also it have some utils and should had the repeated code across clients. | ||
*/ | ||
class AbstractClient extends EventEmitter { | ||
static relay(source, dest, channels = CHANNELS) { | ||
for (const channel of channels) { | ||
source.on(channel, (...args) => dest.emit(channel, ...args)); | ||
} | ||
} | ||
|
||
static destroy(source, channels = CHANNELS) { | ||
for (const channel of channels) { | ||
source.removeAllListeners(channel); | ||
} | ||
} | ||
|
||
subscribeTicker(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
unsubscribeTicker(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
subscribeCandles(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
unsubscribeCandles(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
subscribeTrades(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
unsubscribeTrades(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
subscribeLevel2Snapshots(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
unsubscribeLevel2Snapshots(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
subscribeLevel2Updates(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
unsubscribeLevel2Updates(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
subscribeLevel3Updates(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
|
||
unsubscribeLevel3Updates(market) { | ||
throw new TypeError(`Not implemented. Market: ${JSON.stringify(market)}`); | ||
} | ||
} | ||
|
||
module.exports = { | ||
CHANNELS, | ||
AbstractClient, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { AbstractClient } = require('../abstract-client'); | ||
|
||
const EnumDirection = { | ||
ADD: 0x00, | ||
SUB: 0x01, | ||
}; | ||
|
||
class ClientWithCounter extends AbstractClient { | ||
constructor(client) { | ||
super(); | ||
this._client = client; | ||
this._counter = 0; | ||
|
||
// Relay events from original client | ||
AbstractClient.relay(this._client, this); | ||
} | ||
|
||
_checkCounter(direction, isOk) { | ||
if (isOk) { | ||
const n = (direction === EnumDirection.ADD) ? 1 : -1; | ||
this._counter = this._counter + n; | ||
} | ||
if (this._counter <= 0) { | ||
// Remove relay listeners before close. | ||
AbstractClient.destroy(this); | ||
this._client.close(); | ||
} | ||
} | ||
|
||
getCounter() { | ||
return this._counter; | ||
} | ||
|
||
subscribeTicker(market) { | ||
return this._checkCounter(EnumDirection.ADD, this._client.subscribeTicker(market)); | ||
} | ||
|
||
unsubscribeTicker(market) { | ||
return this._checkCounter(EnumDirection.SUB, this._client.unsubscribeTicker(market)); | ||
} | ||
|
||
subscribeCandles(market) { | ||
return this._checkCounter(EnumDirection.ADD, this._client.subscribeCandles(market)); | ||
} | ||
|
||
unsubscribeCandles(market) { | ||
return this._checkCounter(EnumDirection.SUB, this._client.unsubscribeCandles(market)); | ||
} | ||
|
||
subscribeTrades(market) { | ||
return this._checkCounter(EnumDirection.ADD, this._client.subscribeTrades(market)); | ||
} | ||
|
||
unsubscribeTrades(market) { | ||
return this._checkCounter(EnumDirection.SUB, this._client.unsubscribeTrades(market)); | ||
} | ||
|
||
subscribeLevel2Snapshots(market) { | ||
return this._checkCounter(EnumDirection.ADD, this._client.subscribeLevel2Snapshots(market)); | ||
} | ||
|
||
unsubscribeLevel2Snapshots(market) { | ||
return this._checkCounter(EnumDirection.SUB, this._client.unsubscribeLevel2Snapshots(market)); | ||
} | ||
|
||
subscribeLevel2Updates(market) { | ||
return this._checkCounter(EnumDirection.ADD, this._client.subscribeLevel2Updates(market)); | ||
} | ||
|
||
unsubscribeLevel2Updates(market) { | ||
return this._checkCounter(EnumDirection.SUB, this._client.unsubscribeLevel2Updates(market)); | ||
} | ||
|
||
subscribeLevel3Updates(market) { | ||
return this._checkCounter(EnumDirection.ADD, this._client.subscribeLevel3Updates(market)); | ||
} | ||
|
||
unsubscribeLevel3Updates(market) { | ||
return this._checkCounter(EnumDirection.SUB, this._client.unsubscribeLevel3Updates(market)); | ||
} | ||
} | ||
|
||
module.exports = { | ||
ClientWithCounter, | ||
}; |
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,7 @@ | ||
const ClientWithCounter = require('./client-with-counter'); | ||
const balanced = require('./load-balance'); | ||
|
||
module.exports = { | ||
...ClientWithCounter, | ||
balanced, | ||
}; |
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,7 @@ | ||
const LoadBalanceClient = require('./load-balance-client'); | ||
const strategies = require('./strategies'); | ||
|
||
module.exports = { | ||
...LoadBalanceClient, | ||
strategies, | ||
}; |
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,89 @@ | ||
const { AbstractClient } = require('../../abstract-client'); | ||
const { FillHolesStrategy } = require('./strategies'); | ||
|
||
const EnumAction = { | ||
ADD: 0x00, | ||
SUB: 0x01, | ||
}; | ||
|
||
/** | ||
* LoadBalanceClient create clients on-the-fly with a maximum subscriptions | ||
*/ | ||
class LoadBalanceClient extends AbstractClient { | ||
/** | ||
* @param {{ clientFactory: (clientOptions) => Client}, maxSubscriptions: number, strategy: (clientFactory) => Strategy }} options | ||
* @param {clientOptions: any} clientOptions | ||
*/ | ||
constructor(options, clientArgs) { | ||
super(); | ||
this._options = options; | ||
this._options.clientArgs = clientArgs; | ||
this._strategy = typeof options.strategy === 'function' ? options.strategy(this._options) : new FillHolesStrategy(this._options); | ||
|
||
this._clients = new WeakMap(); | ||
} | ||
|
||
static create(clientFactory, options) { | ||
return class extends LoadBalanceClient { | ||
constructor(...clientArgs) { | ||
super({ ...options, clientFactory }, clientArgs); | ||
} | ||
}; | ||
} | ||
|
||
_resolve(action, market, fn) { | ||
const method = action === EnumAction.ADD ? 'take' : 'leave'; | ||
const client = this._strategy[method](market.id); | ||
|
||
// Unsubscribe could be undefined if we doesnt subscribed yet. | ||
if (!client) { | ||
return false; | ||
} | ||
|
||
// On subscribe, if client is new, relay their events. | ||
// Destroy must handled Strategy when leaves all channels. | ||
if (action === EnumAction.ADD && !this._clients.has(client)) { | ||
this._clients.set(client, true); | ||
AbstractClient.relay(client, this); | ||
} | ||
|
||
return fn(client); | ||
} | ||
|
||
_subscribe(market, fn) { | ||
return this._resolve(EnumAction.ADD, market, fn); | ||
} | ||
|
||
_unsubscribe(market, fn) { | ||
return this._resolve(EnumAction.SUB, market, fn); | ||
} | ||
|
||
subscribeTicker(market) { | ||
return this._subscribe(market, client => client.subscribeTicker(market)); | ||
} | ||
|
||
unsubscribeTicker(market) { | ||
return this._unsubscribe(market, client => client.unsubscribeTicker(market)); | ||
} | ||
|
||
subscribeCandles(market) { | ||
return this._subscribe(market, client => client.subscribeCandles(market)); | ||
} | ||
|
||
unsubscribeCandles(market) { | ||
return this._unsubscribe(market, client => client.unsubscribeCandles(market)); | ||
} | ||
|
||
subscribeTrades(market) { | ||
return this._subscribe(market, client => client.subscribeTrades(market)); | ||
} | ||
|
||
unsubscribeTrades(market) { | ||
return this._unsubscribe(market, client => client.unsubscribeTrades(market)); | ||
} | ||
} | ||
|
||
|
||
module.exports = { | ||
LoadBalanceClient | ||
}; |
18 changes: 18 additions & 0 deletions
18
src/decorators/load-balance/strategies/abstract-strategy.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,18 @@ | ||
class Strategy { | ||
constructor(options) { | ||
this._options = options; | ||
this._clientFactory = options.clientFactory; | ||
} | ||
|
||
take() { | ||
throw new Error('not implemented'); | ||
} | ||
|
||
leave() { | ||
throw new Error('not implemented'); | ||
} | ||
} | ||
|
||
module.exports = { | ||
Strategy, | ||
}; |
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.
Nice! I like this pattern for forward event emission.