Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
perf(redis): Switch RedisQueue to use evalsha (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert authored Aug 17, 2018
1 parent 84562f6 commit 96da589
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,19 @@ class RedisQueue(
private val unackedKey = "$queueName.unacked"
private val messagesKey = "$queueName.messages"
private val locksKey = "$queueName.locks"

// TODO: use AttemptsAttribute instead
private val attemptsKey = "$queueName.attempts"

// Internal ObjectMapper that enforces deterministic property ordering for use only in hashing.
private val hashObjectMapper = ObjectMapper().copy().apply {
enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
}

private lateinit var readMessageWithLockScriptSha: String

init {
pool.resource.use { redis ->
readMessageWithLockScriptSha = redis.scriptLoad(READ_MESSAGE_WITH_LOCK_SRC)
}
log.info("Configured queue: $queueName")
}

Expand Down Expand Up @@ -317,7 +320,7 @@ class RedisQueue(
}

private fun ScriptingCommands.readMessageWithLock(): Triple<String, Instant, String?>? {
val response = eval(READ_MESSAGE_WITH_LOCK, listOf(
val response = evalsha(readMessageWithLockScriptSha, listOf(
queueKey,
unackedKey,
locksKey,
Expand Down Expand Up @@ -441,7 +444,7 @@ class RedisQueue(
)
}

private const val READ_MESSAGE = """
private const val READ_MESSAGE_SRC = """
local java_scientific = function(x)
return string.format("%.12E", x):gsub("\+", "")
end
Expand All @@ -465,7 +468,7 @@ private const val READ_MESSAGE = """
"""

/* ktlint-disable max-line-length */
private const val READ_MESSAGE_WITH_LOCK = """
private const val READ_MESSAGE_WITH_LOCK_SRC = """
local queueKey = KEYS[1]
local unackKey = KEYS[2]
local lockKey = KEYS[3]
Expand Down Expand Up @@ -507,7 +510,7 @@ private const val READ_MESSAGE_WITH_LOCK = """
return "AcquireLockFailed"
end
$READ_MESSAGE
$READ_MESSAGE_SRC
return {fingerprint, fingerprintScore, message}
"""

0 comments on commit 96da589

Please sign in to comment.