-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from boostcampwm-2024/dev-be
Dev be to release
- Loading branch information
Showing
11 changed files
with
543 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @fileoverview Functional batch processor factory | ||
*/ | ||
import { Namespace } from 'socket.io'; | ||
import { Logger } from '@nestjs/common'; | ||
|
||
interface BatchData { | ||
gameId: string; | ||
data: any; | ||
} | ||
|
||
/** | ||
* Creates a batch processor instance | ||
*/ | ||
export function createBatchProcessor(server: Namespace, eventName: string) { | ||
const logger = new Logger(`BatchProcessor:${eventName}`); | ||
// μ΄ λ³μλ€μ ν΄λ‘μ μ μν΄ λ³΄νΈλ¨ | ||
const batchMap = new Map<string, any[]>(); | ||
let isProcessing = false; | ||
|
||
/** | ||
* Pushes data to batch queue | ||
*/ | ||
const pushData = (gameId: string, data: any) => { | ||
if (!batchMap.has(gameId)) { | ||
batchMap.set(gameId, []); | ||
} | ||
batchMap.get(gameId).push(data); | ||
}; | ||
|
||
/** | ||
* Processes and emits all batched data | ||
*/ | ||
const processBatch = () => { | ||
if (isProcessing) { | ||
return; | ||
} | ||
|
||
isProcessing = true; | ||
try { | ||
batchMap.forEach((queue, gameId) => { | ||
if (queue.length > 0) { | ||
const batch = queue.splice(0, queue.length); | ||
server.to(gameId).emit(eventName, batch); | ||
logger.debug(`Processed ${batch.length} items for game ${gameId}`); | ||
} | ||
}); | ||
} catch (error) { | ||
logger.error(`Error processing batch: ${error.message}`); | ||
} finally { | ||
isProcessing = false; | ||
} | ||
}; | ||
|
||
/** | ||
* Starts automatic batch processing | ||
*/ | ||
const startProcessing = (interval: number = 100) => { | ||
setInterval(processBatch, interval); | ||
logger.verbose(`Started batch processor for ${eventName} with ${interval}ms interval`); | ||
}; | ||
|
||
/** | ||
* Clears all batched data | ||
*/ | ||
const clear = () => { | ||
batchMap.clear(); | ||
}; | ||
|
||
// μΈλΆμμ μ¬μ©ν μ μλ public λ©μλλ§ λ ΈμΆ | ||
return { | ||
pushData, | ||
processBatch, | ||
startProcessing, | ||
clear | ||
}; | ||
} |
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
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.