-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In-memory fallback queue and no manual reconnecting of Kafka in Usage…
… service (#6531)
- Loading branch information
1 parent
457fc3d
commit 2507a9b
Showing
7 changed files
with
228 additions
and
127 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
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,89 @@ | ||
import pLimit from 'p-limit'; | ||
import type { ServiceLogger } from '@hive/service-common'; | ||
|
||
// Average message size is ~800kb | ||
// 1000 messages = 800mb | ||
const MAX_QUEUE_SIZE = 1000; | ||
|
||
export function createFallbackQueue(config: { | ||
send: (msgValue: Buffer<ArrayBufferLike>, numOfOperations: number) => Promise<void>; | ||
logger: ServiceLogger; | ||
}) { | ||
const queue: [Buffer<ArrayBufferLike>, number][] = []; | ||
|
||
async function flushSingle() { | ||
const msg = queue.shift(); | ||
if (!msg) { | ||
return; | ||
} | ||
|
||
try { | ||
const [msgValue, numOfOperations] = msg; | ||
await config.send(msgValue, numOfOperations); | ||
} catch (error) { | ||
if (error instanceof Error && 'type' in error && error.type === 'MESSAGE_TOO_LARGE') { | ||
config.logger.error('Message too large, dropping message'); | ||
return; | ||
} | ||
|
||
config.logger.error( | ||
{ | ||
error: error instanceof Error ? error.message : String(error), | ||
}, | ||
'Failed to flush message, adding back to fallback queue', | ||
); | ||
queue.push(msg); | ||
} | ||
} | ||
|
||
let timeoutId: ReturnType<typeof setTimeout> | null = null; | ||
|
||
function schedule() { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
} | ||
|
||
timeoutId = setTimeout(async () => { | ||
await flushSingle(); | ||
schedule(); | ||
}, 200); | ||
} | ||
|
||
return { | ||
start() { | ||
schedule(); | ||
}, | ||
stop() { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
} | ||
|
||
const limit = pLimit(10); | ||
return Promise.allSettled( | ||
queue.map(msgValue => | ||
limit(() => | ||
config.send(msgValue[0], msgValue[1]).catch(error => { | ||
config.logger.error( | ||
{ | ||
error: error instanceof Error ? error.message : String(error), | ||
}, | ||
'Failed to flush message before stopping', | ||
); | ||
}), | ||
), | ||
), | ||
); | ||
}, | ||
add(msgValue: Buffer<ArrayBufferLike>, numOfOperations: number) { | ||
if (queue.length >= MAX_QUEUE_SIZE) { | ||
config.logger.error('Queue is full, dropping oldest message'); | ||
queue.shift(); | ||
} | ||
|
||
queue.push([msgValue, numOfOperations]); | ||
}, | ||
size() { | ||
return queue.length; | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.