-
Notifications
You must be signed in to change notification settings - Fork 309
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
[DI] Batch outgoing http requests #5007
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict' | ||
|
||
class JSONQueue { | ||
constructor ({ size, timeout, onFlush }) { | ||
this._maxSize = size | ||
this._timeout = timeout | ||
this._onFlush = onFlush | ||
this._reset() | ||
} | ||
|
||
_reset () { | ||
clearTimeout(this._timer) | ||
this._timer = null | ||
this._partialJson = null | ||
} | ||
|
||
_flush () { | ||
const json = `${this._partialJson}]` | ||
this._reset() | ||
this._onFlush(json) | ||
} | ||
|
||
add (str, size = Buffer.byteLength(str)) { | ||
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. I think it better to pass just the json and remove the size param. 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. This is purely a performance optimization. In the snapshot code, the size is already calculated since we need to know if it's larger than 1MB. So to not calculate it twice we allow it to be passed in. It's not user generated input. |
||
if (this._timer === null) { | ||
this._partialJson = `[${str}` | ||
this._timer = setTimeout(() => this._flush(), this._timeout) | ||
} else if (Buffer.byteLength(this._partialJson) + size + 2 > this._maxSize) { | ||
this._flush() | ||
this.add(str, size) | ||
} else { | ||
this._partialJson += `,${str}` | ||
} | ||
} | ||
} | ||
|
||
module.exports = JSONQueue |
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.
nit: maybe JSONBuffer is better as this is not really a queue API
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.
Yeah I couldn't decide if I should call it a buffer or a queue... was considering both names. In Node.js there's a
Buffer
class which is different, so the term is a bit overloaded in Node.js. But I'll consider changing it... you're probably right