Skip to content

Commit

Permalink
fix: adjust cdk renewal logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliothmoon committed Jan 26, 2025
1 parent a3b3798 commit b562428
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
28 changes: 10 additions & 18 deletions src/main/kotlin/biz/cdkey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import model.entity.OperationLog
import org.ktorm.dsl.*
import utils.throwIf
import utils.throwIfNot
import utils.throwIfNullOrEmpty
import java.nio.ByteBuffer
import java.security.MessageDigest
import java.time.Duration
Expand Down Expand Up @@ -54,27 +55,18 @@ fun next(): String {
fun renewCDK(params: RenewParams): Resp {
with(params) {
cdk.isNullOrBlank().throwIf("cdk cannot be empty")
expireTime.throwIfNullOrEmpty("expireTime cannot be empty")
}
val cdk = params.cdk!!
if (params.duration <= 0) {
throw ServiceException("renew duration must be greater than 0")
}
val sql = """
update mirrorc_cdk
set expire_time = DATE_ADD(
IF(expire_time < CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
expire_time
),
interval ? second)
where `key` = ?;
""".trimIndent()
val row = DB.useConnection { c ->
c.prepareStatement(sql).use {
it.setLong(1, params.duration)
it.setString(2, cdk)
it.executeUpdate()
val expireTime = params.expireTime!!

expireTime.isBefore(LocalDateTime.now()).throwIf("expireTime cannot be set to past")

val row = DB.update(CDK) {
where {
CDK.key eq cdk
}
set(CDK.expireTime, expireTime)
}
(row > 0).throwIfNot("cdk renew failed")

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/model/param.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class RenewParams {
var cdk: String? = null

/**
* 续期时间 单位秒
* 续期后的过期时间
* */
var duration: Long = 0
var expireTime: LocalDateTime? = null
}

/**
Expand Down

0 comments on commit b562428

Please sign in to comment.