Skip to content

Commit

Permalink
[ML] Explain Log Rate Spikes: Fix slowness of flushFix random paylo…
Browse files Browse the repository at this point in the history
…ad generation. (#160645)

The random payload for the proxy flushing fix was regenerated for every
push to the stream. This turned out to be quite slow. This PR updates
the logic to create the payload only once and reuse it for every push.

Note: Further testing in Cloud showed the differences there are not as
big as in my local testing, might be related to entropy for
`crypto.randomBytes`.
  • Loading branch information
walterra authored Jun 30, 2023
1 parent df408bd commit 9eb6d1e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions x-pack/packages/ml/aiops_utils/src/stream_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export function streamFactory<T = unknown>(
): StreamFactoryReturnType<T> {
let streamType: StreamType;
const isCompressed = compressOverride && acceptCompression(headers);
const flushPayload = flushFix
? crypto.randomBytes(FLUSH_PAYLOAD_SIZE).toString('hex')
: undefined;

const stream = isCompressed ? zlib.createGzip() : new UncompressedResponseStream();

Expand Down Expand Up @@ -149,9 +152,7 @@ export function streamFactory<T = unknown>(
? `${JSON.stringify({
...d,
// This is a temporary fix for response streaming with proxy configurations that buffer responses up to 4KB in size.
...(flushFix
? { flushPayload: crypto.randomBytes(FLUSH_PAYLOAD_SIZE).toString('hex') }
: {}),
...(flushFix ? { flushPayload } : {}),
})}${DELIMITER}`
: d;

Expand Down

0 comments on commit 9eb6d1e

Please sign in to comment.