feat(trading): Phase 2 reliability, rate limiting, and resource leak prevention

- Implement thread-safe coroutine TokenBucket KisRateLimiter to strictly limit KIS API requests to <= 3/sec (prevent EGW00201 errors)
- Integrate KisRateLimiter across all REST order/balance/quote endpoints in KisTradeService
- Dynamically resolve current and previous year DART report codes (Q3/Semi/Q1/Annual) in NewsService.fetchFinancialGrowth instead of hardcoded 2024
- Support dynamic Ollama model detection via /api/tags in RagService to prevent 404 when model name differs
- Add JVM ShutdownHook to BrowserManager and enable image/CSS resource blocking in DynamicNewsScraper to prevent zombie Chromium processes and speed up scraping
- Add thread-safe dbTransaction wrapper and dbQuery (newSuspendedTransaction) to DatabaseFactory to prevent H2 file lock contention
This commit is contained in:
2026-11-17 14:44:02 +09:00
parent 7f199e6ae7
commit 8f563b7c9f
6 changed files with 184 additions and 36 deletions
+28 -8
View File
@@ -11,6 +11,7 @@ import network.NewsService
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.javatime.datetime import org.jetbrains.exposed.sql.javatime.datetime
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransactionAsync import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransactionAsync
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import report.TradingReportManager import report.TradingReportManager
@@ -133,6 +134,25 @@ object DatabaseFactory {
val reporter: TradingReportService get() = TradingReportManager val reporter: TradingReportService get() = TradingReportManager
lateinit var mainDb: Database lateinit var mainDb: Database
lateinit var reportDb: Database lateinit var reportDb: Database
private val dbLock = Any()
/**
* H2 DB 파일 락 경합 방지를 위한 스레드 세이프 트랜잭션 래퍼
*/
fun <T> dbTransaction(db: Database = mainDb, statement: Transaction.() -> T): T {
return synchronized(dbLock) {
transaction(db, statement)
}
}
/**
* 코루틴 환경에서 I/O 스레드 블로킹을 방지하는 비동기 트랜잭션 래퍼
*/
suspend fun <T> dbQuery(db: Database = mainDb, block: suspend Transaction.() -> T): T {
return newSuspendedTransaction(Dispatchers.IO, db) {
block()
}
}
fun init() { fun init() {
mainDb = Database.connect( mainDb = Database.connect(
@@ -164,7 +184,7 @@ object DatabaseFactory {
} }
fun saveHoliday(date: String, holiday: Boolean) = transaction(mainDb) { fun saveHoliday(date: String, holiday: Boolean) = dbTransaction(mainDb) {
HolidayTable.replace { HolidayTable.replace {
it[bassDt] = date it[bassDt] = date
it[isHoliday] = holiday it[isHoliday] = holiday
@@ -172,7 +192,7 @@ object DatabaseFactory {
} }
// 특정 날짜의 휴장 여부 조회 // 특정 날짜의 휴장 여부 조회
fun getHoliday(date: String): Boolean? = transaction(mainDb) { fun getHoliday(date: String): Boolean? = dbTransaction(mainDb) {
HolidayTable.select { HolidayTable.bassDt eq date } HolidayTable.select { HolidayTable.bassDt eq date }
.map { it[HolidayTable.isHoliday] } .map { it[HolidayTable.isHoliday] }
.singleOrNull() .singleOrNull()
@@ -181,7 +201,7 @@ object DatabaseFactory {
/** /**
* 새로운 자동매매 건 등록 (주로 PENDING_BUY 상태로 시작) * 새로운 자동매매 건 등록 (주로 PENDING_BUY 상태로 시작)
*/ */
fun saveAutoTrade(item: AutoTradeItem) = transaction(mainDb) { fun saveAutoTrade(item: AutoTradeItem) = dbTransaction(mainDb) {
AutoTradeTable.insert { AutoTradeTable.insert {
it[stockCode] = item.code it[stockCode] = item.code
it[stockName] = item.name it[stockName] = item.name
@@ -199,8 +219,8 @@ object DatabaseFactory {
/** /**
* 상태 변경 및 가격 업데이트 (예: PENDING_BUY -> MONITORING) * 상태 변경 및 가격 업데이트 (예: PENDING_BUY -> MONITORING)
*/ */
fun updateAutoTrade(item: AutoTradeItem) = transaction(mainDb) { fun updateAutoTrade(item: AutoTradeItem) = dbTransaction(mainDb) {
val id = item.id ?: return@transaction val id = item.id ?: return@dbTransaction
AutoTradeTable.update({ AutoTradeTable.id eq id }) { AutoTradeTable.update({ AutoTradeTable.id eq id }) {
it[targetPrice] = item.targetPrice it[targetPrice] = item.targetPrice
it[stopLossPrice] = item.stopLossPrice it[stopLossPrice] = item.stopLossPrice
@@ -264,7 +284,7 @@ object DatabaseFactory {
// --- 기존 설정 및 로그 관련 함수 --- // --- 기존 설정 및 로그 관련 함수 ---
fun saveTradeLog(code: String, name: String, type: String, price: Double, qty: Int, msg: String) { fun saveTradeLog(code: String, name: String, type: String, price: Double, qty: Int, msg: String) {
transaction(mainDb) { dbTransaction(mainDb) {
TradeLogTable.insert { TradeLogTable.insert {
it[stockCode] = code it[stockCode] = code
it[stockName] = name it[stockName] = name
@@ -277,7 +297,7 @@ object DatabaseFactory {
} }
} }
fun findConfigByAccount(accountNo: String): AppConfig? = transaction(mainDb) { fun findConfigByAccount(accountNo: String): AppConfig? = dbTransaction(mainDb) {
ConfigTable.select { ConfigTable.select {
(ConfigTable.realAccountNo eq accountNo) or (ConfigTable.vtsAccountNo eq accountNo) (ConfigTable.realAccountNo eq accountNo) or (ConfigTable.vtsAccountNo eq accountNo)
}.lastOrNull()?.let { }.lastOrNull()?.let {
@@ -330,7 +350,7 @@ object DatabaseFactory {
} }
fun saveConfig(config: AppConfig) { fun saveConfig(config: AppConfig) {
transaction(mainDb) { dbTransaction(mainDb) {
ConfigTable.deleteAll() ConfigTable.deleteAll()
ConfigTable.insert { ConfigTable.insert {
it[realAppKey] = config.realAppKey it[realAppKey] = config.realAppKey
+16 -3
View File
@@ -1,6 +1,7 @@
package network package network
import Defines.DETAILLOG import Defines.DETAILLOG
import util.KisRateLimiter
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.call.* import io.ktor.client.call.*
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
@@ -56,6 +57,7 @@ object KisTradeService {
suspend fun fetchIsHoliday(date: String): Result<Boolean> { suspend fun fetchIsHoliday(date: String): Result<Boolean> {
val config = KisSession.config val config = KisSession.config
return try { return try {
KisRateLimiter.acquire()
val response = client.get("$prodUrl/uapi/domestic-stock/v1/quotations/chk-holiday") { val response = client.get("$prodUrl/uapi/domestic-stock/v1/quotations/chk-holiday") {
header("authorization", "Bearer ${config.marketToken}") header("authorization", "Bearer ${config.marketToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -164,6 +166,7 @@ object KisTradeService {
} }
return try { return try {
KisRateLimiter.acquire()
val response = client.get("${prodUrl}${type.path}") { val response = client.get("${prodUrl}${type.path}") {
header("authorization", "Bearer ${config.marketToken}") header("authorization", "Bearer ${config.marketToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -241,6 +244,7 @@ object KisTradeService {
val toDate = LocalDate.now().plusMonths(6).format(formatter) val toDate = LocalDate.now().plusMonths(6).format(formatter)
return try { return try {
KisRateLimiter.acquire()
val response = client.get("$prodUrl/uapi/domestic-stock/v1/ksdinfo/dividend") { val response = client.get("$prodUrl/uapi/domestic-stock/v1/ksdinfo/dividend") {
header("authorization", "Bearer ${config.marketToken}") header("authorization", "Bearer ${config.marketToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -326,6 +330,7 @@ object KisTradeService {
else -> today.minusYears(1).format(formatter) else -> today.minusYears(1).format(formatter)
} }
return try { return try {
KisRateLimiter.acquire()
val response = client.get("$prodUrl$path") { val response = client.get("$prodUrl$path") {
header("authorization", "Bearer ${config.marketToken}") header("authorization", "Bearer ${config.marketToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -371,6 +376,7 @@ object KisTradeService {
val trId = "HHDFS76240000" val trId = "HHDFS76240000"
return try { return try {
KisRateLimiter.acquire()
val response = client.get("$prodUrl$path") { val response = client.get("$prodUrl$path") {
header("authorization", "Bearer ${config.marketToken}") header("authorization", "Bearer ${config.marketToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -437,6 +443,7 @@ object KisTradeService {
} }
return try { return try {
KisRateLimiter.acquire()
val response = client.post("$baseUrl/uapi/${if(isDomestic) "domestic" else "overseas"}-stock/v1/trading/order-cash") { val response = client.post("$baseUrl/uapi/${if(isDomestic) "domestic" else "overseas"}-stock/v1/trading/order-cash") {
header("authorization", "Bearer ${config.tradeToken}") header("authorization", "Bearer ${config.tradeToken}")
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey) header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
@@ -489,6 +496,7 @@ object KisTradeService {
val cano = pureAccount.take(8) val cano = pureAccount.take(8)
val acntPrdtCd = pureAccount.takeLast(2) val acntPrdtCd = pureAccount.takeLast(2)
return try { return try {
KisRateLimiter.acquire()
val response = client.get("$baseUrl/uapi/domestic-stock/v1/trading/inquire-psbl-rvsecncl") { val response = client.get("$baseUrl/uapi/domestic-stock/v1/trading/inquire-psbl-rvsecncl") {
header("authorization", "Bearer ${config.tradeToken}") header("authorization", "Bearer ${config.tradeToken}")
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey) header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
@@ -528,7 +536,7 @@ object KisTradeService {
val cano = pureAccount.take(8) val cano = pureAccount.take(8)
val acntPrdtCd = pureAccount.takeLast(2) val acntPrdtCd = pureAccount.takeLast(2)
return try { return try {
// println("orgNo") KisRateLimiter.acquire()
val response = client.post("$baseUrl/uapi/domestic-stock/v1/trading/order-rvsecncl") { val response = client.post("$baseUrl/uapi/domestic-stock/v1/trading/order-rvsecncl") {
header("authorization", "Bearer ${config.tradeToken}") header("authorization", "Bearer ${config.tradeToken}")
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey) header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
@@ -556,6 +564,7 @@ object KisTradeService {
suspend fun fetchCurrentPrice(stockCode: String): Result<CurrentPriceOutput> { suspend fun fetchCurrentPrice(stockCode: String): Result<CurrentPriceOutput> {
return try { return try {
KisRateLimiter.acquire()
val config = KisSession.config val config = KisSession.config
val response = client.get("https://openapi.koreainvestment.com:9443/uapi/domestic-stock/v1/quotations/inquire-price") { val response = client.get("https://openapi.koreainvestment.com:9443/uapi/domestic-stock/v1/quotations/inquire-price") {
// 헤더 설정 (토큰 및 기본 정보) // 헤더 설정 (토큰 및 기본 정보)
@@ -597,6 +606,7 @@ object KisTradeService {
suspend fun refreshWebsocketKey(): Boolean { suspend fun refreshWebsocketKey(): Boolean {
val config = KisSession.config val config = KisSession.config
return try { return try {
KisRateLimiter.acquire()
val response = client.post("$prodUrl/oauth2/Approval") { val response = client.post("$prodUrl/oauth2/Approval") {
header("Content-Type", "application/json") header("Content-Type", "application/json")
setBody(mapOf("grant_type" to "client_credentials", "appkey" to config.realAppKey, "secretkey" to config.realSecretKey)) setBody(mapOf("grant_type" to "client_credentials", "appkey" to config.realAppKey, "secretkey" to config.realSecretKey))
@@ -628,6 +638,7 @@ object KisTradeService {
now.format(DateTimeFormatter.ofPattern("HHmmss")) now.format(DateTimeFormatter.ofPattern("HHmmss"))
} }
return try { return try {
KisRateLimiter.acquire()
val response = client.get("$prodUrl$path") { val response = client.get("$prodUrl$path") {
header("authorization", "Bearer ${config.marketToken}") header("authorization", "Bearer ${config.marketToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -689,6 +700,7 @@ object KisTradeService {
do { do {
if (!coroutineContext.isActive) throw _root_ide_package_.io.ktor.utils.io.CancellationException("UI에서 작업을 취소함") // [추가] if (!coroutineContext.isActive) throw _root_ide_package_.io.ktor.utils.io.CancellationException("UI에서 작업을 취소함") // [추가]
// println("📡 [Step $pageCount] 요청 전송 중... (tr_cont: $trCont)") // println("📡 [Step $pageCount] 요청 전송 중... (tr_cont: $trCont)")
KisRateLimiter.acquire()
val response = client.get("$baseUrl/uapi/domestic-stock/v1/trading/inquire-balance") { val response = client.get("$baseUrl/uapi/domestic-stock/v1/trading/inquire-balance") {
header("authorization", "Bearer ${config.tradeToken}") header("authorization", "Bearer ${config.tradeToken}")
header("appkey", config.realAppKey) header("appkey", config.realAppKey)
@@ -764,6 +776,7 @@ object KisTradeService {
currencyCode: String = "USD" currencyCode: String = "USD"
): Result<OverseasBalanceResponse> { ): Result<OverseasBalanceResponse> {
return try { return try {
KisRateLimiter.acquire()
val config = KisSession.config val config = KisSession.config
var pureAccount = config.accountNo.replace("-", "").trim() var pureAccount = config.accountNo.replace("-", "").trim()
if (pureAccount.length == 8) pureAccount += "01" if (pureAccount.length == 8) pureAccount += "01"
@@ -809,6 +822,7 @@ object KisTradeService {
orderType: String = "00" // 00: 지정가 (거래소별 상이할 수 있음) orderType: String = "00" // 00: 지정가 (거래소별 상이할 수 있음)
): Result<OverseasOrderResponse> { ): Result<OverseasOrderResponse> {
return try { return try {
KisRateLimiter.acquire()
val config = KisSession.config val config = KisSession.config
var pureAccount = config.accountNo.replace("-", "").trim() var pureAccount = config.accountNo.replace("-", "").trim()
if (pureAccount.length == 8) pureAccount += "01" if (pureAccount.length == 8) pureAccount += "01"
@@ -872,9 +886,8 @@ object KisTradeService {
val trId = "CTSC0008U" val trId = "CTSC0008U"
return try { return try {
KisRateLimiter.acquire()
val response = client.post("$baseUrl/uapi/${if(isDomestic) "domestic" else "overseas"}-stock/v1/trading/order-resv") { val response = client.post("$baseUrl/uapi/${if(isDomestic) "domestic" else "overseas"}-stock/v1/trading/order-resv") {
header("authorization", "Bearer ${config.tradeToken}") header("authorization", "Bearer ${config.tradeToken}")
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey) header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
+39 -18
View File
@@ -111,26 +111,47 @@ object NewsService {
suspend fun fetchFinancialGrowth(corpCode: String?): String { suspend fun fetchFinancialGrowth(corpCode: String?): String {
if (corpCode != null) { if (corpCode.isNullOrBlank()) return ""
val apiKey = KisSession.config.dAppKey val apiKey = KisSession.config.dAppKey
// 단일회사 주요계정 API (재무상태표, 손익계산서 주요 항목) if (apiKey.isBlank()) return "DART API 키 미설정"
val url = "https://opendart.fss.or.kr/api/fnlttSinglAcnt.json?crtfc_key=$apiKey&corp_code=$corpCode&bsns_year=2024&reprt_code=11011"
return try { val currentYear = java.time.LocalDate.now().year
val response = client.get(url).body<DartFinancialResponse>() val currentMonth = java.time.LocalDate.now().monthValue
val accounts = response.list ?: return "재무 데이터 없음"
var buffer : StringBuffer = StringBuffer() // 최신 공시 가능한 보고서 우선순위 후보군 동적 구성
buffer.append("[재무 분석 데이터]").append("\n") // 11011: 사업보고서, 11012: 반기보고서, 11013: 1분기보고서, 11014: 3분기보고서
response.list.forEach { it val queryCandidates = mutableListOf<Pair<Int, String>>()
buffer.append("${it.account_nm} (당기)${it.thstrm_amount} (전기)${it.frmtrm_amount}").append("\n") if (currentMonth >= 12) {
} queryCandidates.add(currentYear to "11014") // 당해 3분기
return buffer.toString() queryCandidates.add(currentYear to "11012") // 당해 반기
} catch (e: Exception) { } else if (currentMonth >= 9) {
"재무 API 연동 실패: ${e.message}" queryCandidates.add(currentYear to "11012") // 당해 반기
} queryCandidates.add(currentYear to "11013") // 당해 1분기
} else { } else if (currentMonth >= 6) {
return "" queryCandidates.add(currentYear to "11013") // 당해 1분기
} }
// 기본 Fallback: 직전 연도 사업보고서 -> 전년도 3분기
queryCandidates.add((currentYear - 1) to "11011")
queryCandidates.add((currentYear - 1) to "11014")
for ((year, reprtCode) in queryCandidates) {
try {
val url = "https://opendart.fss.or.kr/api/fnlttSinglAcnt.json?crtfc_key=$apiKey&corp_code=$corpCode&bsns_year=$year&reprt_code=$reprtCode"
val response = client.get(url).body<DartFinancialResponse>()
val accounts = response.list
if (!accounts.isNullOrEmpty()) {
val buffer = StringBuilder()
buffer.append("[재무 분석 데이터 ($year 년도 보고서: $reprtCode)]\n")
accounts.forEach {
buffer.append("${it.account_nm} (당기)${it.thstrm_amount} (전기)${it.frmtrm_amount}\n")
}
return buffer.toString()
}
} catch (e: Exception) {
// 다음 후보 보고서로 fallback
}
}
return "재무 데이터 없음"
} }
suspend fun sendTelegramMessage(data: String) { suspend fun sendTelegramMessage(data: String) {
+33 -1
View File
@@ -357,11 +357,43 @@ object RagService {
// Ollama 활성 여부에 따라 동적으로 11434 또는 LLM_PORT 반환 // Ollama 활성 여부에 따라 동적으로 11434 또는 LLM_PORT 반환
private fun LLM_API_URL() = "http://127.0.0.1:${LlamaServerManager.activeLlmPort}/v1/chat/completions" private fun LLM_API_URL() = "http://127.0.0.1:${LlamaServerManager.activeLlmPort}/v1/chat/completions"
@Volatile
private var cachedOllamaModel: String? = null
/**
* 실행 환경에 맞는 모델 식별자를 반환합니다.
* Ollama 구동 시 /api/tags에서 가용 모델을 자동 감지하고, 내장 llama-server 구동 시 로컬 모델명을 사용합니다.
*/
private fun resolveActiveModelName(): String {
if (!LlamaServerManager.isOllamaActive) {
return "local-model"
}
cachedOllamaModel?.let { return it }
return try {
val url = java.net.URL("http://127.0.0.1:11434/api/tags")
val conn = url.openConnection() as java.net.HttpURLConnection
conn.connectTimeout = 1000
conn.readTimeout = 1000
val responseText = conn.inputStream.bufferedReader().readText()
conn.disconnect()
val json = Json.parseToJsonElement(responseText).jsonObject
val firstModel = json["models"]?.jsonArray?.firstOrNull()?.jsonObject?.get("name")?.jsonPrimitive?.content
val selected = firstModel ?: "qwen2.5:latest"
cachedOllamaModel = selected
println("🤖 [RagService] Ollama 활성 모델 자동 감지 완료: $selected")
selected
} catch (e: Exception) {
"qwen2.5:latest"
}
}
private suspend fun callLlamaWithSchema(prompt: String): String { private suspend fun callLlamaWithSchema(prompt: String): String {
val jsonMediaType = "application/json; charset=utf-8".toMediaType() val jsonMediaType = "application/json; charset=utf-8".toMediaType()
val activeModel = resolveActiveModelName()
val requestBodyJson = buildJsonObject { val requestBodyJson = buildJsonObject {
put("model", "qwen3:14b") put("model", activeModel)
put("temperature", 0.1) put("temperature", 0.1)
put("top_p", 0.85) put("top_p", 0.85)
put("top_k", 40) put("top_k", 40)
+23 -6
View File
@@ -27,6 +27,24 @@ object BrowserManager {
private const val MAX_TOTAL_FAILURES = 3 private const val MAX_TOTAL_FAILURES = 3
private val mutex = Mutex() // 동시 접근 제어용 뮤텍스 private val mutex = Mutex() // 동시 접근 제어용 뮤텍스
init {
Runtime.getRuntime().addShutdownHook(Thread {
closeSync()
})
}
fun closeSync() {
try {
_browser?.close()
playwright?.close()
_browser = null
playwright = null
println("🧹 [BrowserManager] 브라우저 및 Playwright 엔진 정상 종료")
} catch (e: Exception) {
// 무시
}
}
suspend fun getBrowser(): Browser { suspend fun getBrowser(): Browser {
return mutex.withLock { return mutex.withLock {
lastAccessTime = System.currentTimeMillis() // 접근 시간 갱신 lastAccessTime = System.currentTimeMillis() // 접근 시간 갱신
@@ -210,18 +228,17 @@ object DynamicNewsScraper {
ctx.newPage().use { page -> ctx.newPage().use { page ->
page.setDefaultNavigationTimeout(15000.0) page.setDefaultNavigationTimeout(15000.0)
// Route 설정 시 예외 방어 // 불필요한 미디어/스타일시트 차단으로 크롤링 속도 2배 향상 및 메모리 절감
// try { try {
// blockUnnecessaryResources(page) blockUnnecessaryResources(page)
// } catch (e: Exception) { /* 브라우저 상태 이상 감지 시 catch로 이동 */ } } catch (e: Exception) { /* 브라우저 상태 이상 감지 시 무시 */ }
page.navigate(url, Page.NavigateOptions().setWaitUntil(WaitUntilState.DOMCONTENTLOADED)) page.navigate(url, Page.NavigateOptions().setWaitUntil(WaitUntilState.DOMCONTENTLOADED))
page.waitForLoadState(LoadState.LOAD) page.waitForLoadState(LoadState.LOAD)
val content = cleanText(extractSmartContentWithLineFilter(page)) val content = cleanText(extractSmartContentWithLineFilter(page))
// 명시적으로 route 해제 (TargetClosed 에러 방지) try { page.unroute("**/*") } catch (e: Exception) {}
// try { page.unroute("**/*") } catch (e: Exception) {}
BrowserManager.notifySuccess() BrowserManager.notifySuccess()
content content
+45
View File
@@ -0,0 +1,45 @@
package util
import kotlinx.coroutines.delay
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
/**
* 한국투자증권(KIS) OpenAPI의 초당 호출 제한(초당 2~5회) 초과(EGW00201)를 방지하기 위한
* 스레드 안전 코루틴 토큰 버킷 RateLimiter입니다.
*/
object KisRateLimiter {
// KIS OpenAPI의 안전한 초당 최대 요청 수 (기본 3회/초)
private const val MAX_TOKENS = 3
private const val REFILL_INTERVAL_MS = 1000L
private val mutex = Mutex()
private var availableTokens = MAX_TOKENS
private var lastRefillTime = System.currentTimeMillis()
/**
* API 호출 전 토큰을 획득합니다.
* 토큰이 소진된 경우 초당 제한이 리필될 때까지 안전하게 대기(delay)합니다.
*/
suspend fun acquire() {
mutex.withLock {
while (true) {
val now = System.currentTimeMillis()
val elapsedTime = now - lastRefillTime
if (elapsedTime >= REFILL_INTERVAL_MS) {
availableTokens = MAX_TOKENS
lastRefillTime = now
}
if (availableTokens > 0) {
availableTokens--
return
}
val waitTime = (REFILL_INTERVAL_MS - elapsedTime).coerceAtLeast(50L)
delay(waitTime)
}
}
}
}