Skip to content

Commit

Permalink
fix(Queue): exec 메서드 수정
Browse files Browse the repository at this point in the history
- 유효하지 않은(입금 두개 이상) 요청이 들어올 경우, queue에서 같은 id의 deposit 요청 제거 후 실행되도록 수정
  • Loading branch information
Jong1co committed Apr 27, 2024
1 parent 54e66af commit ad058a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ export class App {
}

async start() {
await this.bankAccountService.deposit(1, 5000);
await Promise.all([
this.bankAccountService.deposit(1, 5000),
this.bankAccountService.withdrawal(1, 5000),
this.bankAccountService.deposit(1, 5000),
]);

const balance = await this.bankAccountService.getBalance(1);

return balance;
}
}

const app = new App();

Promise.all([app.start(), app.start()]);
(async () => {
const result = await app.start();
console.log(result);
})();
38 changes: 26 additions & 12 deletions src/service/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BankAccount } from "../repository/BankAccountRepository";
import { sleep } from "../utils/sleep";

export type BankAccountUpdateAction = "deposit" | "withdrawal";

Expand All @@ -9,7 +10,7 @@ export type BankAccountQueueNode = {
};

export interface BankAccountServiceQueue {
isValid: () => void;
isValid: (id: number) => void;
push: (node: BankAccountQueueNode) => Promise<any>;
dequeue: () => () => Promise<BankAccount>;
exec: () => void;
Expand All @@ -33,21 +34,21 @@ export class BankAccountServiceQueueImpl implements BankAccountServiceQueue {
}, {} as Record<number, number>);
};

isValid = () => {
isValid = (id: number) => {
const depositActionCountMap = this.getDepositActionCount(this.queue);

const isInvalidAction = Object.values(depositActionCountMap).some(
(count) => count > 1
);

if (isInvalidAction) {
throw new Error("같은 아이디로 동시에 입금할 수 없습니다.");
}
console.log(this.queue);
return !(depositActionCountMap[id] > 1);
// const isInvalidAction = Object.values(depositActionCountMap).some(
// (count) => count > 1
// );

// if (isInvalidAction) {
// throw new Error("같은 아이디로 동시에 입금할 수 없습니다.");
// }
};

async push(node: BankAccountQueueNode) {
this.queue.push(node);
this.isValid();

if (!this.isExec) {
this.isExec = true;
Expand All @@ -62,7 +63,20 @@ export class BankAccountServiceQueueImpl implements BankAccountServiceQueue {

// 재귀함수 사용
async exec() {
const { func } = this.queue[0];
const { func, id } = this.queue[0];

await sleep(0);

if (!this.isValid(id)) {
this.queue = this.queue.filter(
(node) => !(node.id === id && node.action === "deposit")
);

if (this.queue.length > 0) {
this.exec();
}
return;
}

await func().then(() => {
this.dequeue();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sleep.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function sleep(ms: number) {
export async function sleep(ms: number) {
const wakeUpTime = Date.now() + ms;
while (Date.now() < wakeUpTime) {}
}

0 comments on commit ad058a4

Please sign in to comment.