-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DI] Batch outgoing http requests (#5007)
- Loading branch information
Showing
13 changed files
with
483 additions
and
185 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
36 changes: 36 additions & 0 deletions
36
packages/dd-trace/src/debugger/devtools_client/json-buffer.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,36 @@ | ||
'use strict' | ||
|
||
class JSONBuffer { | ||
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) | ||
} | ||
|
||
write (str, size = Buffer.byteLength(str)) { | ||
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.write(str, size) | ||
} else { | ||
this._partialJson += `,${str}` | ||
} | ||
} | ||
} | ||
|
||
module.exports = JSONBuffer |
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
45 changes: 45 additions & 0 deletions
45
packages/dd-trace/test/debugger/devtools_client/json-buffer.spec.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,45 @@ | ||
'use strict' | ||
|
||
require('../../setup/mocha') | ||
|
||
const JSONBuffer = require('../../../src/debugger/devtools_client/json-buffer') | ||
|
||
const MAX_SAFE_SIGNED_INTEGER = 2 ** 31 - 1 | ||
|
||
describe('JSONBuffer', () => { | ||
it('should call onFlush with the expected payload when the timeout is reached', function (done) { | ||
const onFlush = (json) => { | ||
const diff = Date.now() - start | ||
expect(json).to.equal('[{"message":1},{"message":2},{"message":3}]') | ||
expect(diff).to.be.within(95, 110) | ||
done() | ||
} | ||
|
||
const jsonBuffer = new JSONBuffer({ size: Infinity, timeout: 100, onFlush }) | ||
|
||
const start = Date.now() | ||
jsonBuffer.write(JSON.stringify({ message: 1 })) | ||
jsonBuffer.write(JSON.stringify({ message: 2 })) | ||
jsonBuffer.write(JSON.stringify({ message: 3 })) | ||
}) | ||
|
||
it('should call onFlush with the expected payload when the size is reached', function (done) { | ||
const expectedPayloads = [ | ||
'[{"message":1},{"message":2}]', | ||
'[{"message":3},{"message":4}]' | ||
] | ||
|
||
const onFlush = (json) => { | ||
expect(json).to.equal(expectedPayloads.shift()) | ||
if (expectedPayloads.length === 0) done() | ||
} | ||
|
||
const jsonBuffer = new JSONBuffer({ size: 30, timeout: MAX_SAFE_SIGNED_INTEGER, onFlush }) | ||
|
||
jsonBuffer.write(JSON.stringify({ message: 1 })) // size: 15 | ||
jsonBuffer.write(JSON.stringify({ message: 2 })) // size: 29 | ||
jsonBuffer.write(JSON.stringify({ message: 3 })) // size: 15 (flushed, and re-added) | ||
jsonBuffer.write(JSON.stringify({ message: 4 })) // size: 29 | ||
jsonBuffer.write(JSON.stringify({ message: 5 })) // size: 15 (flushed, and re-added) | ||
}) | ||
}) |
Oops, something went wrong.