-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: balanced pool * fixup * fixup
- Loading branch information
Showing
5 changed files
with
294 additions
and
284 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
'use strict' | ||
|
||
const Dispatcher = require('./dispatcher') | ||
const { | ||
ClientDestroyedError, | ||
ClientClosedError, | ||
InvalidArgumentError | ||
} = require('./core/errors') | ||
const FixedQueue = require('./node/fixed-queue') | ||
const { kSize, kRunning, kPending, kBusy } = require('./core/symbols') | ||
|
||
const kClients = Symbol('clients') | ||
const kNeedDrain = Symbol('needDrain') | ||
const kQueue = Symbol('queue') | ||
const kDestroyed = Symbol('destroyed') | ||
const kClosedPromise = Symbol('closed promise') | ||
const kClosedResolve = Symbol('closed resolve') | ||
const kOnDrain = Symbol('onDrain') | ||
const kOnConnect = Symbol('onConnect') | ||
const kOnDisconnect = Symbol('onDisconnect') | ||
const kOnConnectionError = Symbol('onConnectionError') | ||
const kQueued = Symbol('queued') | ||
const kDispatch = Symbol('dispatch') | ||
|
||
class PoolBase extends Dispatcher { | ||
constructor () { | ||
super() | ||
|
||
this[kQueue] = new FixedQueue() | ||
this[kClosedPromise] = null | ||
this[kClosedResolve] = null | ||
this[kDestroyed] = false | ||
this[kClients] = [] | ||
this[kNeedDrain] = false | ||
this[kQueued] = 0 | ||
|
||
const pool = this | ||
|
||
this[kOnDrain] = function onDrain (origin, targets) { | ||
const queue = pool[kQueue] | ||
|
||
let needDrain = false | ||
|
||
while (!needDrain) { | ||
const item = queue.shift() | ||
if (!item) { | ||
break | ||
} | ||
pool[kQueued]-- | ||
needDrain = !this.dispatch(item.opts, item.handler) | ||
} | ||
|
||
this[kNeedDrain] = needDrain | ||
|
||
if (!this[kNeedDrain] && pool[kNeedDrain]) { | ||
pool[kNeedDrain] = false | ||
pool.emit('drain', origin, [pool, ...targets]) | ||
} | ||
|
||
if (pool[kClosedResolve] && queue.isEmpty()) { | ||
Promise | ||
.all(pool[kClients].map(c => c.close())) | ||
.then(pool[kClosedResolve]) | ||
} | ||
} | ||
|
||
this[kOnConnect] = (origin, targets) => { | ||
pool.emit('connect', origin, [pool, ...targets]) | ||
} | ||
|
||
this[kOnDisconnect] = (origin, targets, err) => { | ||
pool.emit('disconnect', origin, [pool, ...targets], err) | ||
} | ||
|
||
this[kOnConnectionError] = (origin, targets, err) => { | ||
pool.emit('connectionError', origin, [pool, ...targets], err) | ||
} | ||
} | ||
|
||
get [kBusy] () { | ||
return this[kNeedDrain] | ||
} | ||
|
||
get [kPending] () { | ||
let ret = this[kQueued] | ||
for (const { [kPending]: pending } of this[kClients]) { | ||
ret += pending | ||
} | ||
return ret | ||
} | ||
|
||
get [kRunning] () { | ||
let ret = 0 | ||
for (const { [kRunning]: running } of this[kClients]) { | ||
ret += running | ||
} | ||
return ret | ||
} | ||
|
||
get [kSize] () { | ||
let ret = this[kQueued] | ||
for (const { [kSize]: size } of this[kClients]) { | ||
ret += size | ||
} | ||
return ret | ||
} | ||
|
||
get destroyed () { | ||
return this[kDestroyed] | ||
} | ||
|
||
get closed () { | ||
return this[kClosedPromise] != null | ||
} | ||
|
||
close (cb) { | ||
try { | ||
if (this[kDestroyed]) { | ||
throw new ClientDestroyedError() | ||
} | ||
|
||
if (!this[kClosedPromise]) { | ||
if (this[kQueue].isEmpty()) { | ||
this[kClosedPromise] = Promise.all(this[kClients].map(c => c.close())) | ||
} else { | ||
this[kClosedPromise] = new Promise((resolve) => { | ||
this[kClosedResolve] = resolve | ||
}) | ||
} | ||
this[kClosedPromise] = this[kClosedPromise].then(() => { | ||
this[kDestroyed] = true | ||
}) | ||
} | ||
|
||
if (cb) { | ||
this[kClosedPromise].then(() => cb(null, null)) | ||
} else { | ||
return this[kClosedPromise] | ||
} | ||
} catch (err) { | ||
if (cb) { | ||
cb(err) | ||
} else { | ||
return Promise.reject(err) | ||
} | ||
} | ||
} | ||
|
||
destroy (err, cb) { | ||
this[kDestroyed] = true | ||
|
||
if (typeof err === 'function') { | ||
cb = err | ||
err = null | ||
} | ||
|
||
if (!err) { | ||
err = new ClientDestroyedError() | ||
} | ||
|
||
while (true) { | ||
const item = this[kQueue].shift() | ||
if (!item) { | ||
break | ||
} | ||
item.handler.onError(err) | ||
} | ||
|
||
const promise = Promise.all(this[kClients].map(c => c.destroy(err))) | ||
if (cb) { | ||
promise.then(() => cb(null, null)) | ||
} else { | ||
return promise | ||
} | ||
} | ||
|
||
dispatch (opts, handler) { | ||
if (!handler || typeof handler !== 'object') { | ||
throw new InvalidArgumentError('handler must be an object') | ||
} | ||
|
||
try { | ||
if (this[kDestroyed]) { | ||
throw new ClientDestroyedError() | ||
} | ||
|
||
if (this[kClosedPromise]) { | ||
throw new ClientClosedError() | ||
} | ||
|
||
const dispatcher = this[kDispatch]() | ||
|
||
if (!dispatcher) { | ||
this[kNeedDrain] = true | ||
this[kQueue].push({ opts, handler }) | ||
this[kQueued]++ | ||
} else if (!dispatcher.dispatch(opts, handler)) { | ||
dispatcher[kNeedDrain] = true | ||
// TODO: return this[kClients].some(dispatcher => !dispatcher[kNeedDrain]) | ||
} | ||
} catch (err) { | ||
if (typeof handler.onError !== 'function') { | ||
throw new InvalidArgumentError('invalid onError method') | ||
} | ||
|
||
handler.onError(err) | ||
} | ||
|
||
return !this[kNeedDrain] | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
PoolBase, | ||
kClients, | ||
kNeedDrain, | ||
kOnDrain, | ||
kOnConnect, | ||
kOnDisconnect, | ||
kDispatch, | ||
kOnConnectionError | ||
} |
Oops, something went wrong.