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:
@@ -11,6 +11,7 @@ import network.NewsService
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
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.transaction
|
||||
import report.TradingReportManager
|
||||
@@ -133,6 +134,25 @@ object DatabaseFactory {
|
||||
val reporter: TradingReportService get() = TradingReportManager
|
||||
lateinit var mainDb: 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() {
|
||||
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 {
|
||||
it[bassDt] = date
|
||||
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 }
|
||||
.map { it[HolidayTable.isHoliday] }
|
||||
.singleOrNull()
|
||||
@@ -181,7 +201,7 @@ object DatabaseFactory {
|
||||
/**
|
||||
* 새로운 자동매매 건 등록 (주로 PENDING_BUY 상태로 시작)
|
||||
*/
|
||||
fun saveAutoTrade(item: AutoTradeItem) = transaction(mainDb) {
|
||||
fun saveAutoTrade(item: AutoTradeItem) = dbTransaction(mainDb) {
|
||||
AutoTradeTable.insert {
|
||||
it[stockCode] = item.code
|
||||
it[stockName] = item.name
|
||||
@@ -199,8 +219,8 @@ object DatabaseFactory {
|
||||
/**
|
||||
* 상태 변경 및 가격 업데이트 (예: PENDING_BUY -> MONITORING)
|
||||
*/
|
||||
fun updateAutoTrade(item: AutoTradeItem) = transaction(mainDb) {
|
||||
val id = item.id ?: return@transaction
|
||||
fun updateAutoTrade(item: AutoTradeItem) = dbTransaction(mainDb) {
|
||||
val id = item.id ?: return@dbTransaction
|
||||
AutoTradeTable.update({ AutoTradeTable.id eq id }) {
|
||||
it[targetPrice] = item.targetPrice
|
||||
it[stopLossPrice] = item.stopLossPrice
|
||||
@@ -264,7 +284,7 @@ object DatabaseFactory {
|
||||
// --- 기존 설정 및 로그 관련 함수 ---
|
||||
|
||||
fun saveTradeLog(code: String, name: String, type: String, price: Double, qty: Int, msg: String) {
|
||||
transaction(mainDb) {
|
||||
dbTransaction(mainDb) {
|
||||
TradeLogTable.insert {
|
||||
it[stockCode] = code
|
||||
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.realAccountNo eq accountNo) or (ConfigTable.vtsAccountNo eq accountNo)
|
||||
}.lastOrNull()?.let {
|
||||
@@ -330,7 +350,7 @@ object DatabaseFactory {
|
||||
}
|
||||
|
||||
fun saveConfig(config: AppConfig) {
|
||||
transaction(mainDb) {
|
||||
dbTransaction(mainDb) {
|
||||
ConfigTable.deleteAll()
|
||||
ConfigTable.insert {
|
||||
it[realAppKey] = config.realAppKey
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package network
|
||||
|
||||
import Defines.DETAILLOG
|
||||
import util.KisRateLimiter
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
@@ -56,6 +57,7 @@ object KisTradeService {
|
||||
suspend fun fetchIsHoliday(date: String): Result<Boolean> {
|
||||
val config = KisSession.config
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$prodUrl/uapi/domestic-stock/v1/quotations/chk-holiday") {
|
||||
header("authorization", "Bearer ${config.marketToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -164,6 +166,7 @@ object KisTradeService {
|
||||
}
|
||||
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("${prodUrl}${type.path}") {
|
||||
header("authorization", "Bearer ${config.marketToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -241,6 +244,7 @@ object KisTradeService {
|
||||
val toDate = LocalDate.now().plusMonths(6).format(formatter)
|
||||
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$prodUrl/uapi/domestic-stock/v1/ksdinfo/dividend") {
|
||||
header("authorization", "Bearer ${config.marketToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -326,6 +330,7 @@ object KisTradeService {
|
||||
else -> today.minusYears(1).format(formatter)
|
||||
}
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$prodUrl$path") {
|
||||
header("authorization", "Bearer ${config.marketToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -371,6 +376,7 @@ object KisTradeService {
|
||||
val trId = "HHDFS76240000"
|
||||
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$prodUrl$path") {
|
||||
header("authorization", "Bearer ${config.marketToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -437,6 +443,7 @@ object KisTradeService {
|
||||
}
|
||||
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.post("$baseUrl/uapi/${if(isDomestic) "domestic" else "overseas"}-stock/v1/trading/order-cash") {
|
||||
header("authorization", "Bearer ${config.tradeToken}")
|
||||
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
|
||||
@@ -489,6 +496,7 @@ object KisTradeService {
|
||||
val cano = pureAccount.take(8)
|
||||
val acntPrdtCd = pureAccount.takeLast(2)
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$baseUrl/uapi/domestic-stock/v1/trading/inquire-psbl-rvsecncl") {
|
||||
header("authorization", "Bearer ${config.tradeToken}")
|
||||
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
|
||||
@@ -528,7 +536,7 @@ object KisTradeService {
|
||||
val cano = pureAccount.take(8)
|
||||
val acntPrdtCd = pureAccount.takeLast(2)
|
||||
return try {
|
||||
// println("orgNo")
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.post("$baseUrl/uapi/domestic-stock/v1/trading/order-rvsecncl") {
|
||||
header("authorization", "Bearer ${config.tradeToken}")
|
||||
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
|
||||
@@ -556,6 +564,7 @@ object KisTradeService {
|
||||
|
||||
suspend fun fetchCurrentPrice(stockCode: String): Result<CurrentPriceOutput> {
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val config = KisSession.config
|
||||
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 {
|
||||
val config = KisSession.config
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.post("$prodUrl/oauth2/Approval") {
|
||||
header("Content-Type", "application/json")
|
||||
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"))
|
||||
}
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$prodUrl$path") {
|
||||
header("authorization", "Bearer ${config.marketToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -689,6 +700,7 @@ object KisTradeService {
|
||||
do {
|
||||
if (!coroutineContext.isActive) throw _root_ide_package_.io.ktor.utils.io.CancellationException("UI에서 작업을 취소함") // [추가]
|
||||
// println("📡 [Step $pageCount] 요청 전송 중... (tr_cont: $trCont)")
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.get("$baseUrl/uapi/domestic-stock/v1/trading/inquire-balance") {
|
||||
header("authorization", "Bearer ${config.tradeToken}")
|
||||
header("appkey", config.realAppKey)
|
||||
@@ -764,6 +776,7 @@ object KisTradeService {
|
||||
currencyCode: String = "USD"
|
||||
): Result<OverseasBalanceResponse> {
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val config = KisSession.config
|
||||
var pureAccount = config.accountNo.replace("-", "").trim()
|
||||
if (pureAccount.length == 8) pureAccount += "01"
|
||||
@@ -809,6 +822,7 @@ object KisTradeService {
|
||||
orderType: String = "00" // 00: 지정가 (거래소별 상이할 수 있음)
|
||||
): Result<OverseasOrderResponse> {
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val config = KisSession.config
|
||||
var pureAccount = config.accountNo.replace("-", "").trim()
|
||||
if (pureAccount.length == 8) pureAccount += "01"
|
||||
@@ -872,9 +886,8 @@ object KisTradeService {
|
||||
|
||||
val trId = "CTSC0008U"
|
||||
|
||||
|
||||
|
||||
return try {
|
||||
KisRateLimiter.acquire()
|
||||
val response = client.post("$baseUrl/uapi/${if(isDomestic) "domestic" else "overseas"}-stock/v1/trading/order-resv") {
|
||||
header("authorization", "Bearer ${config.tradeToken}")
|
||||
header("appkey", if (config.isSimulation) config.vtsAppKey else config.realAppKey)
|
||||
|
||||
@@ -111,26 +111,47 @@ object NewsService {
|
||||
|
||||
|
||||
suspend fun fetchFinancialGrowth(corpCode: String?): String {
|
||||
if (corpCode != null) {
|
||||
val apiKey = KisSession.config.dAppKey
|
||||
// 단일회사 주요계정 API (재무상태표, 손익계산서 주요 항목)
|
||||
val url = "https://opendart.fss.or.kr/api/fnlttSinglAcnt.json?crtfc_key=$apiKey&corp_code=$corpCode&bsns_year=2024&reprt_code=11011"
|
||||
if (corpCode.isNullOrBlank()) return ""
|
||||
val apiKey = KisSession.config.dAppKey
|
||||
if (apiKey.isBlank()) return "DART API 키 미설정"
|
||||
|
||||
return try {
|
||||
val response = client.get(url).body<DartFinancialResponse>()
|
||||
val accounts = response.list ?: return "재무 데이터 없음"
|
||||
var buffer : StringBuffer = StringBuffer()
|
||||
buffer.append("[재무 분석 데이터]").append("\n")
|
||||
response.list.forEach { it
|
||||
buffer.append("${it.account_nm} (당기)${it.thstrm_amount} (전기)${it.frmtrm_amount}").append("\n")
|
||||
}
|
||||
return buffer.toString()
|
||||
} catch (e: Exception) {
|
||||
"재무 API 연동 실패: ${e.message}"
|
||||
}
|
||||
} else {
|
||||
return ""
|
||||
val currentYear = java.time.LocalDate.now().year
|
||||
val currentMonth = java.time.LocalDate.now().monthValue
|
||||
|
||||
// 최신 공시 가능한 보고서 우선순위 후보군 동적 구성
|
||||
// 11011: 사업보고서, 11012: 반기보고서, 11013: 1분기보고서, 11014: 3분기보고서
|
||||
val queryCandidates = mutableListOf<Pair<Int, String>>()
|
||||
if (currentMonth >= 12) {
|
||||
queryCandidates.add(currentYear to "11014") // 당해 3분기
|
||||
queryCandidates.add(currentYear to "11012") // 당해 반기
|
||||
} else if (currentMonth >= 9) {
|
||||
queryCandidates.add(currentYear to "11012") // 당해 반기
|
||||
queryCandidates.add(currentYear to "11013") // 당해 1분기
|
||||
} else if (currentMonth >= 6) {
|
||||
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) {
|
||||
|
||||
@@ -357,11 +357,43 @@ object RagService {
|
||||
// Ollama 활성 여부에 따라 동적으로 11434 또는 LLM_PORT 반환
|
||||
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 {
|
||||
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
|
||||
val activeModel = resolveActiveModelName()
|
||||
|
||||
val requestBodyJson = buildJsonObject {
|
||||
put("model", "qwen3:14b")
|
||||
put("model", activeModel)
|
||||
put("temperature", 0.1)
|
||||
put("top_p", 0.85)
|
||||
put("top_k", 40)
|
||||
|
||||
@@ -27,6 +27,24 @@ object BrowserManager {
|
||||
private const val MAX_TOTAL_FAILURES = 3
|
||||
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 {
|
||||
return mutex.withLock {
|
||||
lastAccessTime = System.currentTimeMillis() // 접근 시간 갱신
|
||||
@@ -210,18 +228,17 @@ object DynamicNewsScraper {
|
||||
ctx.newPage().use { page ->
|
||||
page.setDefaultNavigationTimeout(15000.0)
|
||||
|
||||
// Route 설정 시 예외 방어
|
||||
// try {
|
||||
// blockUnnecessaryResources(page)
|
||||
// } catch (e: Exception) { /* 브라우저 상태 이상 감지 시 catch로 이동 */ }
|
||||
// 불필요한 미디어/스타일시트 차단으로 크롤링 속도 2배 향상 및 메모리 절감
|
||||
try {
|
||||
blockUnnecessaryResources(page)
|
||||
} catch (e: Exception) { /* 브라우저 상태 이상 감지 시 무시 */ }
|
||||
|
||||
page.navigate(url, Page.NavigateOptions().setWaitUntil(WaitUntilState.DOMCONTENTLOADED))
|
||||
page.waitForLoadState(LoadState.LOAD)
|
||||
|
||||
val content = cleanText(extractSmartContentWithLineFilter(page))
|
||||
|
||||
// 명시적으로 route 해제 (TargetClosed 에러 방지)
|
||||
// try { page.unroute("**/*") } catch (e: Exception) {}
|
||||
try { page.unroute("**/*") } catch (e: Exception) {}
|
||||
|
||||
BrowserManager.notifySuccess()
|
||||
content
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user