Skip to content

Commit

Permalink
refactor: 트랜잭션 범위 축소
Browse files Browse the repository at this point in the history
매수 거래 체결 트랜잭션의 범위를 최대한 줄였습니다.
그리고 커넥션 풀의 제한을 10 -> 100으로 늘렸습니다.
  • Loading branch information
SeungGwan123 committed Jan 22, 2025
1 parent afefd94 commit d0c6c08
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/configs/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function getTypeOrmConfig(): TypeOrmModuleOptions {
synchronize: process.env.DB_SYNCHRONIZE === 'true',
dropSchema: process.env.DB_DROPSCHEMA === 'true',
extra:{
connectionLimit: 10,
connectionLimit: 100,
}
};
}
2 changes: 1 addition & 1 deletion packages/server/src/trade-history/trade-history.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TradeHistory {
@Column('double')
quantity: number;

@Column({ type: 'timestamp' })
@Column({ type: 'timestamp', precision:6 })
createdAt: Date;

@CreateDateColumn({ type: 'timestamp' })
Expand Down
46 changes: 4 additions & 42 deletions packages/server/src/trade/trade-bid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
}
try {
let userTrade;
const startTime = new Date().getTime();
console.log(`${user.userId} start execution time: ${startTime} ms`); // 실행 시간 출력
const transactionResult = await this.executeTransaction(
async (queryRunner) => {
if (bidDto.receivedAmount <= 0) {
Expand Down Expand Up @@ -98,9 +96,6 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
};
},
);
const endTime = new Date().getTime(); // 종료 시간 기록
const duration = endTime - startTime; // 실행 시간 계산
console.log(`${user.userId} Transaction execution time: ${duration} ms`); // 실행 시간 출력

if (transactionResult.statusCode === 200) {
const tradeData: TradeDataRedis = {
Expand All @@ -115,7 +110,6 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
};
await this.redisRepository.createTrade(tradeData);
}
console.log(user.userId+"의 거래가 등록되었습니다.");
return transactionResult;
}catch(error){
console.log(error);
Expand Down Expand Up @@ -162,25 +156,6 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
for (const order of orderbook) {
try {
if (order.ask_price > bidDto.receivedPrice) break;
// const tradeResult = await this.executeTransaction(
// async (queryRunner) => {
// const account = await this.accountRepository.getAccount(userId, queryRunner);

// bidDto.accountBalance = account[typeGiven];
// bidDto.account = account;

// const remainingQuantity = await this.executeBidTrade(
// bidDto,
// order,
// user,
// account,
// queryRunner,
// );

// return !isMinimumQuantity(remainingQuantity);
// },
// );

const remainingQuantity = await this.executeBidTrade(
bidDto,
order,
Expand All @@ -197,7 +172,6 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
throw error;
}
}
this.logger.error(bidDto.tradeId+ " 매수 성공");
} finally {
delete this.isProcessing[bidDto.tradeId];
}
Expand Down Expand Up @@ -234,14 +208,16 @@ export class BidService extends TradeAskBidService implements OnModuleInit {

buyData.price = formatQuantity(ask_price * krw);

const result = await this.updateTradeData(tradeData, buyData, queryRunner);
await queryRunner.commitTransaction();
const tradeTime = new Date();
this.tradeHistoryRepository.createTradeHistory(
user,
buyData,
tradeTime
);

const result = await this.updateTradeData(tradeData, buyData, queryRunner);
await queryRunner.commitTransaction();


bidDto.account.availableKRW += formatQuantity(
(bidDto.receivedPrice - buyData.price) * buyData.quantity,
Expand Down Expand Up @@ -302,20 +278,6 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
const change = formatQuantity(
(bidDto.receivedPrice - buyData.price) * buyData.quantity,
);

// await this.accountRepository.updateAccountCurrency(
// typeGiven,
// -returnChange,
// userAccount.id,
// queryRunner,
// );

// await this.accountRepository.updateAccountCurrency(
// 'availableKRW',
// change,
// userAccount.id,
// queryRunner,
// );

await this.accountRepository.updateAccountCurrencyWithBid(
typeGiven,
Expand Down

0 comments on commit d0c6c08

Please sign in to comment.