feat(i18n): 번역 제공자 호출에 취소 제어와 시간 제한을 추가한다

This commit is contained in:
2026-11-18 12:29:01 +09:00
parent d436fd6bff
commit a3c76a1bbe
14 changed files with 398 additions and 16 deletions
@@ -8,15 +8,24 @@ import software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageRe
class AwsLanguageDetectionProvider(private val client: ComprehendClient) : LanguageDetectionProvider {
override val providerId = LanguageProviderId.AWS
override fun detect(query: String): String? {
override fun detect(query: String): String? = detect(query, null)
override fun detect(query: String, control: TranslationCallControl?): String? {
if (query.isBlank()) return null
return try {
// shortcut: 감지는 앞 100000 UTF-8 바이트만 판정하므로 전체 본문과 결과가 다를 수 있음, 전체 본문 기준 품질이 요구되면 별도 감지 정책 승인 후 업그레이드
val text = takeTextPrefixByCodePoint(query, MAX_REQUEST_TEXT_BYTES, ::utf8CodePointSize)
val request = DetectDominantLanguageRequest.builder().text(text).build()
client.detectDominantLanguage(request).languages().firstOrNull()?.languageCode()?.takeIf { it.isNotBlank() }
} catch (_: Exception) {
val request = DetectDominantLanguageRequest.builder().text(text)
control?.let { call ->
request.overrideConfiguration {
it.putExecutionAttribute(AwsTranslationCallInterceptor.CONTROL, call)
.apiCallTimeout(call.remainingTimeout()).apiCallAttemptTimeout(call.remainingTimeout())
}
}
client.detectDominantLanguage(request.build()).languages().firstOrNull()?.languageCode()?.takeIf { it.isNotBlank() }
} catch (error: Exception) {
error.translationCallStop()?.let { throw it }
null
}
}
@@ -22,11 +22,13 @@ class AwsLanguageProviderConfiguration(
fun comprehendClient(): ComprehendClient = ComprehendClient.builder()
.credentialsProvider(credentialsProvider)
.region(region)
.overrideConfiguration { it.addExecutionInterceptor(AwsTranslationCallInterceptor()) }
.build()
@Bean(destroyMethod = "close")
fun translateClient(): TranslateClient = TranslateClient.builder()
.credentialsProvider(credentialsProvider)
.region(region)
.overrideConfiguration { it.addExecutionInterceptor(AwsTranslationCallInterceptor()) }
.build()
}
@@ -0,0 +1,16 @@
package kr.co.vividnext.sodalive.i18n.translation
import software.amazon.awssdk.core.interceptor.Context
import software.amazon.awssdk.core.interceptor.ExecutionAttribute
import software.amazon.awssdk.core.interceptor.ExecutionAttributes
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor
class AwsTranslationCallInterceptor : ExecutionInterceptor {
override fun beforeTransmission(context: Context.BeforeTransmission, executionAttributes: ExecutionAttributes) {
executionAttributes.getAttribute(CONTROL)?.beforeCall()
}
companion object {
val CONTROL = ExecutionAttribute<TranslationCallControl>("TranslationSelectionCallControl")
}
}
@@ -25,18 +25,25 @@ class AwsTranslationService(private val client: TranslateClient) : TranslationPr
for (text in request.texts) {
val translatedText = StringBuilder()
for (chunk in splitTextByCodePoint(text, MAX_REQUEST_TEXT_BYTES, ::utf8CodePointSize)) {
val sdkRequest = TranslateTextRequest.builder()
val builder = TranslateTextRequest.builder()
.sourceLanguageCode(request.sourceLanguage)
.targetLanguageCode(request.targetLanguage)
.text(chunk)
.build()
val translated = client.translateText(sdkRequest)?.translatedText()
request.callControl?.let { control ->
builder.overrideConfiguration {
it.putExecutionAttribute(AwsTranslationCallInterceptor.CONTROL, control)
.apiCallTimeout(control.remainingTimeout()).apiCallAttemptTimeout(control.remainingTimeout())
}
}
val translated = client.translateText(builder.build())?.translatedText()
if (translated.isNullOrBlank()) return TranslateResult(emptyList())
translatedText.append(translated)
}
translatedTexts.add(translatedText.toString())
}
} catch (_: Exception) {
} catch (error: Exception) {
error.translationCallStop()?.let { throw it }
if (request.callControl != null) throw TranslationProviderFailed()
return TranslateResult(emptyList())
}
return TranslateResult(translatedTexts)
@@ -4,4 +4,9 @@ interface LanguageDetectionProvider {
val providerId: LanguageProviderId
fun detect(query: String): String?
fun detect(query: String, control: TranslationCallControl?): String? {
control?.beforeCall()
return detect(query)
}
}
@@ -0,0 +1,12 @@
package kr.co.vividnext.sodalive.i18n.translation
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.web.client.RestTemplate
internal fun papagoAdminTransport(connectTimeoutMs: Int, readTimeoutMs: Int): RestTemplate =
RestTemplate(
SimpleClientHttpRequestFactory().apply {
setConnectTimeout(connectTimeoutMs)
setReadTimeout(readTimeoutMs)
}
)
@@ -13,20 +13,30 @@ class PapagoLanguageDetectionProvider(
@Value("\${cloud.naver.papago-client-id}")
private val papagoClientId: String,
@Value("\${cloud.naver.papago-client-secret}")
private val papagoClientSecret: String
private val papagoClientSecret: String,
@Value("\${sodalive.translation-selection.connect-timeout-ms:3000}")
private val adminConnectTimeoutMs: Int = 3000,
@Value("\${sodalive.translation-selection.read-timeout-ms:15000}")
private val adminReadTimeoutMs: Int = 15000
) : LanguageDetectionProvider {
private val restTemplate = RestTemplate()
private val adminRestTemplate = papagoAdminTransport(adminConnectTimeoutMs, adminReadTimeoutMs)
private val papagoDetectUrl = "https://papago.apigw.ntruss.com/langs/v1/dect"
override val providerId = LanguageProviderId.PAPAGO
override fun detect(query: String): String? {
override fun detect(query: String): String? = detect(query, null)
override fun detect(query: String, control: TranslationCallControl?): String? {
val headers = HttpHeaders().apply {
contentType = MediaType.APPLICATION_FORM_URLENCODED
set("X-NCP-APIGW-API-KEY-ID", papagoClientId)
set("X-NCP-APIGW-API-KEY", papagoClientSecret)
}
val body = LinkedMultiValueMap<String, String>().apply { add("query", query) }
val response = restTemplate.postForEntity(
"https://papago.apigw.ntruss.com/langs/v1/dect",
control?.beforeCall()
val transport = if (control == null) restTemplate else adminRestTemplate
val response = transport.postForEntity(
papagoDetectUrl,
HttpEntity(body, headers),
PapagoLanguageDetectResponse::class.java
)
@@ -14,9 +14,14 @@ class PapagoTranslationService(
private val papagoClientId: String,
@Value("\${cloud.naver.papago-client-secret}")
private val papagoClientSecret: String
private val papagoClientSecret: String,
@Value("\${sodalive.translation-selection.connect-timeout-ms:3000}")
private val adminConnectTimeoutMs: Int = 3000,
@Value("\${sodalive.translation-selection.read-timeout-ms:15000}")
private val adminReadTimeoutMs: Int = 15000
) : TranslationProvider {
private val restTemplate: RestTemplate = RestTemplate()
private val adminRestTemplate = papagoAdminTransport(adminConnectTimeoutMs, adminReadTimeoutMs)
private val papagoTranslateUrl = "https://papago.apigw.ntruss.com/nmt/v1/translation"
@@ -53,7 +58,9 @@ class PapagoTranslationService(
val requestEntity = HttpEntity(body, headers)
val response = restTemplate.postForEntity<PapagoTranslationResponse>(
request.callControl?.beforeCall()
val transport = if (request.callControl == null) restTemplate else adminRestTemplate
val response = transport.postForEntity<PapagoTranslationResponse>(
papagoTranslateUrl,
requestEntity
)
@@ -69,7 +76,9 @@ class PapagoTranslationService(
translatedText.append(translated)
}
translatedTexts.add(translatedText.toString())
} catch (_: Exception) {
} catch (error: Exception) {
error.translationCallStop()?.let { throw it }
if (request.callControl != null) throw TranslationProviderFailed()
return TranslateResult(emptyList())
}
}
@@ -4,7 +4,8 @@ data class TranslateRequest(
val texts: List<String>,
val sourceLanguage: String,
val targetLanguage: String,
val rejectUnchangedAwsResult: Boolean = false
val rejectUnchangedAwsResult: Boolean = false,
val callControl: TranslationCallControl? = null
)
data class TranslateResult(
@@ -0,0 +1,29 @@
package kr.co.vividnext.sodalive.i18n.translation
import java.time.Duration
enum class TranslationCallStopReason { OWNERSHIP_LOST, BUDGET_EXCEEDED }
class TranslationCallStopped(val reason: TranslationCallStopReason) : RuntimeException(reason.name)
class TranslationProviderFailed : RuntimeException("PROVIDER_FAILED")
class TranslationCallControl(
private val authorizeDispatch: () -> Boolean,
budget: Duration = Duration.ofSeconds(15),
private val nanoTime: () -> Long = System::nanoTime
) {
private val deadline = nanoTime() + budget.toNanos()
fun beforeCall() {
remainingTimeout()
if (!authorizeDispatch()) throw TranslationCallStopped(TranslationCallStopReason.OWNERSHIP_LOST)
}
fun remainingTimeout(): Duration {
val remaining = deadline - nanoTime()
if (remaining <= 0) throw TranslationCallStopped(TranslationCallStopReason.BUDGET_EXCEEDED)
return Duration.ofNanos(remaining)
}
}
fun Throwable.translationCallStop(): TranslationCallStopped? =
generateSequence(this) { it.cause }.filterIsInstance<TranslationCallStopped>().firstOrNull()