first commit with streaming support (buggy and unreliable)
This commit is contained in:
@@ -34,6 +34,7 @@ dependencies {
|
||||
implementation catalog.jwo
|
||||
implementation catalog.slf4j.api
|
||||
implementation catalog.netty.common
|
||||
implementation catalog.netty.handler
|
||||
implementation catalog.netty.codec.memcache
|
||||
|
||||
bundle catalog.netty.codec.memcache
|
||||
|
@@ -11,6 +11,7 @@ module net.woggioni.rbcs.server.memcache {
|
||||
requires io.netty.codec.memcache;
|
||||
requires io.netty.common;
|
||||
requires io.netty.buffer;
|
||||
requires io.netty.handler;
|
||||
requires org.slf4j;
|
||||
|
||||
provides CacheProvider with net.woggioni.rbcs.server.memcache.MemcacheCacheProvider;
|
||||
|
@@ -1,20 +1,232 @@
|
||||
package net.woggioni.rbcs.server.memcache
|
||||
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.ByteBufAllocator
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheResponseStatus
|
||||
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest
|
||||
import net.woggioni.rbcs.api.Cache
|
||||
import net.woggioni.rbcs.api.RequestHandle
|
||||
import net.woggioni.rbcs.api.ResponseHandle
|
||||
import net.woggioni.rbcs.api.event.RequestStreamingEvent
|
||||
import net.woggioni.rbcs.api.event.ResponseStreamingEvent
|
||||
import net.woggioni.rbcs.api.exception.ContentTooLargeException
|
||||
import net.woggioni.rbcs.common.ByteBufOutputStream
|
||||
import net.woggioni.rbcs.common.RBCS.digest
|
||||
import net.woggioni.rbcs.common.contextLogger
|
||||
import net.woggioni.rbcs.common.debug
|
||||
import net.woggioni.rbcs.common.extractChunk
|
||||
import net.woggioni.rbcs.server.memcache.client.MemcacheClient
|
||||
import java.nio.channels.ReadableByteChannel
|
||||
import net.woggioni.rbcs.server.memcache.client.MemcacheResponseHandle
|
||||
import net.woggioni.rbcs.server.memcache.client.StreamingRequestEvent
|
||||
import net.woggioni.rbcs.server.memcache.client.StreamingResponseEvent
|
||||
import java.security.MessageDigest
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.CompletableFuture
|
||||
import java.util.zip.Deflater
|
||||
import java.util.zip.DeflaterOutputStream
|
||||
import java.util.zip.Inflater
|
||||
import java.util.zip.InflaterOutputStream
|
||||
|
||||
class MemcacheCache(private val cfg : MemcacheCacheConfiguration) : Cache {
|
||||
private val memcacheClient = MemcacheClient(cfg)
|
||||
class MemcacheCache(private val cfg: MemcacheCacheConfiguration) : Cache {
|
||||
|
||||
override fun get(key: String): CompletableFuture<ReadableByteChannel?> {
|
||||
return memcacheClient.get(key)
|
||||
companion object {
|
||||
@JvmStatic
|
||||
private val log = contextLogger()
|
||||
}
|
||||
|
||||
override fun put(key: String, content: ByteBuf): CompletableFuture<Void> {
|
||||
return memcacheClient.put(key, content, cfg.maxAge)
|
||||
private val memcacheClient = MemcacheClient(cfg)
|
||||
|
||||
override fun get(key: String, responseHandle: ResponseHandle, alloc: ByteBufAllocator) {
|
||||
val compressionMode = cfg.compressionMode
|
||||
val buf = alloc.compositeBuffer()
|
||||
val stream = ByteBufOutputStream(buf).let { outputStream ->
|
||||
if (compressionMode != null) {
|
||||
when (compressionMode) {
|
||||
MemcacheCacheConfiguration.CompressionMode.DEFLATE -> {
|
||||
InflaterOutputStream(
|
||||
outputStream,
|
||||
Inflater()
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
outputStream
|
||||
}
|
||||
}
|
||||
val memcacheResponseHandle = object : MemcacheResponseHandle {
|
||||
override fun handleEvent(evt: StreamingResponseEvent) {
|
||||
when (evt) {
|
||||
is StreamingResponseEvent.ResponseReceived -> {
|
||||
if (evt.response.status() == BinaryMemcacheResponseStatus.SUCCESS) {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.RESPONSE_RECEIVED)
|
||||
} else if (evt.response.status() == BinaryMemcacheResponseStatus.KEY_ENOENT) {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.NOT_FOUND)
|
||||
} else {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ExceptionCaught(MemcacheException(evt.response.status())))
|
||||
}
|
||||
}
|
||||
|
||||
is StreamingResponseEvent.LastContentReceived -> {
|
||||
evt.content.content().let { content ->
|
||||
content.readBytes(stream, content.readableBytes())
|
||||
}
|
||||
buf.retain()
|
||||
stream.close()
|
||||
val chunk = extractChunk(buf, alloc)
|
||||
buf.release()
|
||||
responseHandle.handleEvent(
|
||||
ResponseStreamingEvent.LastChunkReceived(
|
||||
chunk
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
is StreamingResponseEvent.ContentReceived -> {
|
||||
evt.content.content().let { content ->
|
||||
content.readBytes(stream, content.readableBytes())
|
||||
}
|
||||
if (buf.readableBytes() >= cfg.chunkSize) {
|
||||
val chunk = extractChunk(buf, alloc)
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ChunkReceived(chunk))
|
||||
}
|
||||
}
|
||||
|
||||
is StreamingResponseEvent.ExceptionCaught -> {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ExceptionCaught(evt.exception))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
memcacheClient.sendRequest(Unpooled.wrappedBuffer(key.toByteArray()), memcacheResponseHandle)
|
||||
.thenApply { memcacheRequestHandle ->
|
||||
val request = (cfg.digestAlgorithm
|
||||
?.let(MessageDigest::getInstance)
|
||||
?.let { md ->
|
||||
digest(key.toByteArray(), md)
|
||||
} ?: key.toByteArray(Charsets.UTF_8)
|
||||
).let { digest ->
|
||||
DefaultBinaryMemcacheRequest(Unpooled.wrappedBuffer(digest)).apply {
|
||||
setOpcode(BinaryMemcacheOpcodes.GET)
|
||||
}
|
||||
}
|
||||
memcacheRequestHandle.handleEvent(StreamingRequestEvent.SendRequest(request))
|
||||
}.exceptionally { ex ->
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ExceptionCaught(ex))
|
||||
}
|
||||
}
|
||||
|
||||
private fun encodeExpiry(expiry: Duration): Int {
|
||||
val expirySeconds = expiry.toSeconds()
|
||||
return expirySeconds.toInt().takeIf { it.toLong() == expirySeconds }
|
||||
?: Instant.ofEpochSecond(expirySeconds).epochSecond.toInt()
|
||||
}
|
||||
|
||||
override fun put(
|
||||
key: String,
|
||||
responseHandle: ResponseHandle,
|
||||
alloc: ByteBufAllocator
|
||||
): CompletableFuture<RequestHandle> {
|
||||
val memcacheResponseHandle = object : MemcacheResponseHandle {
|
||||
override fun handleEvent(evt: StreamingResponseEvent) {
|
||||
when (evt) {
|
||||
is StreamingResponseEvent.ResponseReceived -> {
|
||||
when (evt.response.status()) {
|
||||
BinaryMemcacheResponseStatus.SUCCESS -> {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.RESPONSE_RECEIVED)
|
||||
}
|
||||
|
||||
BinaryMemcacheResponseStatus.KEY_ENOENT -> {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.NOT_FOUND)
|
||||
}
|
||||
|
||||
BinaryMemcacheResponseStatus.E2BIG -> {
|
||||
responseHandle.handleEvent(
|
||||
ResponseStreamingEvent.ExceptionCaught(
|
||||
ContentTooLargeException("Request payload is too big", null)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ExceptionCaught(MemcacheException(evt.response.status())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is StreamingResponseEvent.LastContentReceived -> {
|
||||
responseHandle.handleEvent(
|
||||
ResponseStreamingEvent.LastChunkReceived(
|
||||
evt.content.content().retain()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
is StreamingResponseEvent.ContentReceived -> {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ChunkReceived(evt.content.content().retain()))
|
||||
}
|
||||
|
||||
is StreamingResponseEvent.ExceptionCaught -> {
|
||||
responseHandle.handleEvent(ResponseStreamingEvent.ExceptionCaught(evt.exception))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val result: CompletableFuture<RequestHandle> =
|
||||
memcacheClient.sendRequest(Unpooled.wrappedBuffer(key.toByteArray()), memcacheResponseHandle)
|
||||
.thenApply { memcacheRequestHandle ->
|
||||
val request = (cfg.digestAlgorithm
|
||||
?.let(MessageDigest::getInstance)
|
||||
?.let { md ->
|
||||
digest(key.toByteArray(), md)
|
||||
} ?: key.toByteArray(Charsets.UTF_8)).let { digest ->
|
||||
val extras = Unpooled.buffer(8, 8)
|
||||
extras.writeInt(0)
|
||||
extras.writeInt(encodeExpiry(cfg.maxAge))
|
||||
DefaultBinaryMemcacheRequest(Unpooled.wrappedBuffer(digest), extras).apply {
|
||||
setOpcode(BinaryMemcacheOpcodes.SET)
|
||||
}
|
||||
}
|
||||
// memcacheRequestHandle.handleEvent(StreamingRequestEvent.SendRequest(request))
|
||||
val compressionMode = cfg.compressionMode
|
||||
val buf = alloc.heapBuffer()
|
||||
val stream = ByteBufOutputStream(buf).let { outputStream ->
|
||||
if (compressionMode != null) {
|
||||
when (compressionMode) {
|
||||
MemcacheCacheConfiguration.CompressionMode.DEFLATE -> {
|
||||
DeflaterOutputStream(
|
||||
outputStream,
|
||||
Deflater(Deflater.DEFAULT_COMPRESSION, false)
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
outputStream
|
||||
}
|
||||
}
|
||||
RequestHandle { evt ->
|
||||
when (evt) {
|
||||
is RequestStreamingEvent.LastChunkReceived -> {
|
||||
evt.chunk.readBytes(stream, evt.chunk.readableBytes())
|
||||
buf.retain()
|
||||
stream.close()
|
||||
request.setTotalBodyLength(buf.readableBytes() + request.keyLength() + request.extrasLength())
|
||||
memcacheRequestHandle.handleEvent(StreamingRequestEvent.SendRequest(request))
|
||||
memcacheRequestHandle.handleEvent(StreamingRequestEvent.SendLastChunk(buf))
|
||||
}
|
||||
|
||||
is RequestStreamingEvent.ChunkReceived -> {
|
||||
evt.chunk.readBytes(stream, evt.chunk.readableBytes())
|
||||
}
|
||||
|
||||
is RequestStreamingEvent.ExceptionCaught -> {
|
||||
stream.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
|
@@ -10,14 +10,10 @@ data class MemcacheCacheConfiguration(
|
||||
val maxSize: Int = 0x100000,
|
||||
val digestAlgorithm: String? = null,
|
||||
val compressionMode: CompressionMode? = null,
|
||||
val chunkSize : Int
|
||||
) : Configuration.Cache {
|
||||
|
||||
enum class CompressionMode {
|
||||
/**
|
||||
* Gzip mode
|
||||
*/
|
||||
GZIP,
|
||||
|
||||
/**
|
||||
* Deflate mode
|
||||
*/
|
||||
|
@@ -29,12 +29,14 @@ class MemcacheCacheProvider : CacheProvider<MemcacheCacheConfiguration> {
|
||||
?.let(Duration::parse)
|
||||
?: Duration.ofDays(1)
|
||||
val maxSize = el.renderAttribute("max-size")
|
||||
?.let(String::toInt)
|
||||
?.let(Integer::decode)
|
||||
?: 0x100000
|
||||
val chunkSize = el.renderAttribute("chunk-size")
|
||||
?.let(Integer::decode)
|
||||
?: 0x4000
|
||||
val compressionMode = el.renderAttribute("compression-mode")
|
||||
?.let {
|
||||
when (it) {
|
||||
"gzip" -> MemcacheCacheConfiguration.CompressionMode.GZIP
|
||||
"deflate" -> MemcacheCacheConfiguration.CompressionMode.DEFLATE
|
||||
else -> MemcacheCacheConfiguration.CompressionMode.DEFLATE
|
||||
}
|
||||
@@ -63,6 +65,7 @@ class MemcacheCacheProvider : CacheProvider<MemcacheCacheConfiguration> {
|
||||
maxSize,
|
||||
digestAlgorithm,
|
||||
compressionMode,
|
||||
chunkSize
|
||||
)
|
||||
}
|
||||
|
||||
@@ -70,7 +73,6 @@ class MemcacheCacheProvider : CacheProvider<MemcacheCacheConfiguration> {
|
||||
val result = doc.createElement("cache")
|
||||
Xml.of(doc, result) {
|
||||
attr("xmlns:${xmlNamespacePrefix}", xmlNamespace, namespaceURI = "http://www.w3.org/2000/xmlns/")
|
||||
|
||||
attr("xs:type", "${xmlNamespacePrefix}:$xmlType", RBCS.XML_SCHEMA_NAMESPACE_URI)
|
||||
for (server in servers) {
|
||||
node("server") {
|
||||
@@ -84,13 +86,13 @@ class MemcacheCacheProvider : CacheProvider<MemcacheCacheConfiguration> {
|
||||
}
|
||||
attr("max-age", maxAge.toString())
|
||||
attr("max-size", maxSize.toString())
|
||||
attr("chunk-size", chunkSize.toString())
|
||||
digestAlgorithm?.let { digestAlgorithm ->
|
||||
attr("digest", digestAlgorithm)
|
||||
}
|
||||
compressionMode?.let { compressionMode ->
|
||||
attr(
|
||||
"compression-mode", when (compressionMode) {
|
||||
MemcacheCacheConfiguration.CompressionMode.GZIP -> "gzip"
|
||||
MemcacheCacheConfiguration.CompressionMode.DEFLATE -> "deflate"
|
||||
}
|
||||
)
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package net.woggioni.rbcs.server.memcache.client
|
||||
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.handler.codec.memcache.LastMemcacheContent
|
||||
import io.netty.handler.codec.memcache.MemcacheContent
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheRequest
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheResponse
|
||||
|
||||
sealed interface StreamingRequestEvent {
|
||||
class SendRequest(val request : BinaryMemcacheRequest) : StreamingRequestEvent
|
||||
open class SendChunk(val chunk : ByteBuf) : StreamingRequestEvent
|
||||
class SendLastChunk(chunk : ByteBuf) : SendChunk(chunk)
|
||||
class ExceptionCaught(val exception : Throwable) : StreamingRequestEvent
|
||||
}
|
||||
|
||||
sealed interface StreamingResponseEvent {
|
||||
class ResponseReceived(val response : BinaryMemcacheResponse) : StreamingResponseEvent
|
||||
open class ContentReceived(val content : MemcacheContent) : StreamingResponseEvent
|
||||
class LastContentReceived(val lastContent : LastMemcacheContent) : ContentReceived(lastContent)
|
||||
class ExceptionCaught(val exception : Throwable) : StreamingResponseEvent
|
||||
}
|
||||
|
||||
interface MemcacheRequestHandle {
|
||||
fun handleEvent(evt : StreamingRequestEvent)
|
||||
}
|
||||
|
||||
interface MemcacheResponseHandle {
|
||||
fun handleEvent(evt : StreamingResponseEvent)
|
||||
}
|
||||
|
@@ -3,7 +3,6 @@ package net.woggioni.rbcs.server.memcache.client
|
||||
|
||||
import io.netty.bootstrap.Bootstrap
|
||||
import io.netty.buffer.ByteBuf
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.Channel
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.channel.ChannelOption
|
||||
@@ -14,36 +13,23 @@ import io.netty.channel.pool.AbstractChannelPoolHandler
|
||||
import io.netty.channel.pool.ChannelPool
|
||||
import io.netty.channel.pool.FixedChannelPool
|
||||
import io.netty.channel.socket.nio.NioSocketChannel
|
||||
import io.netty.handler.codec.DecoderException
|
||||
import io.netty.handler.codec.memcache.DefaultLastMemcacheContent
|
||||
import io.netty.handler.codec.memcache.DefaultMemcacheContent
|
||||
import io.netty.handler.codec.memcache.LastMemcacheContent
|
||||
import io.netty.handler.codec.memcache.MemcacheContent
|
||||
import io.netty.handler.codec.memcache.MemcacheObject
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheClientCodec
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheObjectAggregator
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheResponseStatus
|
||||
import io.netty.handler.codec.memcache.binary.DefaultFullBinaryMemcacheRequest
|
||||
import io.netty.handler.codec.memcache.binary.FullBinaryMemcacheRequest
|
||||
import io.netty.handler.codec.memcache.binary.FullBinaryMemcacheResponse
|
||||
import io.netty.handler.codec.memcache.binary.BinaryMemcacheResponse
|
||||
import io.netty.handler.logging.LoggingHandler
|
||||
import io.netty.util.concurrent.GenericFutureListener
|
||||
import net.woggioni.rbcs.common.ByteBufInputStream
|
||||
import net.woggioni.rbcs.common.ByteBufOutputStream
|
||||
import net.woggioni.rbcs.common.RBCS.digest
|
||||
import net.woggioni.rbcs.common.HostAndPort
|
||||
import net.woggioni.rbcs.common.contextLogger
|
||||
import net.woggioni.rbcs.common.debug
|
||||
import net.woggioni.rbcs.server.memcache.MemcacheCacheConfiguration
|
||||
import net.woggioni.rbcs.server.memcache.MemcacheException
|
||||
import net.woggioni.jwo.JWO
|
||||
import java.net.InetSocketAddress
|
||||
import java.nio.channels.Channels
|
||||
import java.nio.channels.ReadableByteChannel
|
||||
import java.security.MessageDigest
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.CompletableFuture
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.zip.Deflater
|
||||
import java.util.zip.DeflaterOutputStream
|
||||
import java.util.zip.GZIPInputStream
|
||||
import java.util.zip.GZIPOutputStream
|
||||
import java.util.zip.InflaterInputStream
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import io.netty.util.concurrent.Future as NettyFuture
|
||||
|
||||
|
||||
@@ -61,6 +47,8 @@ class MemcacheClient(private val cfg: MemcacheCacheConfiguration) : AutoCloseabl
|
||||
group = NioEventLoopGroup()
|
||||
}
|
||||
|
||||
private val counter = AtomicLong(0)
|
||||
|
||||
private fun newConnectionPool(server: MemcacheCacheConfiguration.Server): FixedChannelPool {
|
||||
val bootstrap = Bootstrap().apply {
|
||||
group(group)
|
||||
@@ -76,18 +64,15 @@ class MemcacheClient(private val cfg: MemcacheCacheConfiguration) : AutoCloseabl
|
||||
override fun channelCreated(ch: Channel) {
|
||||
val pipeline: ChannelPipeline = ch.pipeline()
|
||||
pipeline.addLast(BinaryMemcacheClientCodec())
|
||||
pipeline.addLast(BinaryMemcacheObjectAggregator(cfg.maxSize))
|
||||
pipeline.addLast(LoggingHandler())
|
||||
}
|
||||
}
|
||||
return FixedChannelPool(bootstrap, channelPoolHandler, server.maxConnections)
|
||||
}
|
||||
|
||||
|
||||
private fun sendRequest(request: FullBinaryMemcacheRequest): CompletableFuture<FullBinaryMemcacheResponse> {
|
||||
|
||||
fun sendRequest(key: ByteBuf, responseHandle: MemcacheResponseHandle): CompletableFuture<MemcacheRequestHandle> {
|
||||
val server = cfg.servers.let { servers ->
|
||||
if (servers.size > 1) {
|
||||
val key = request.key().duplicate()
|
||||
var checksum = 0
|
||||
while (key.readableBytes() > 4) {
|
||||
val byte = key.readInt()
|
||||
@@ -103,7 +88,7 @@ class MemcacheClient(private val cfg: MemcacheCacheConfiguration) : AutoCloseabl
|
||||
}
|
||||
}
|
||||
|
||||
val response = CompletableFuture<FullBinaryMemcacheResponse>()
|
||||
val response = CompletableFuture<MemcacheRequestHandle>()
|
||||
// Custom handler for processing responses
|
||||
val pool = connectionPool.computeIfAbsent(server.endpoint) {
|
||||
newConnectionPool(server)
|
||||
@@ -113,31 +98,73 @@ class MemcacheClient(private val cfg: MemcacheCacheConfiguration) : AutoCloseabl
|
||||
if (channelFuture.isSuccess) {
|
||||
val channel = channelFuture.now
|
||||
val pipeline = channel.pipeline()
|
||||
channel.pipeline()
|
||||
.addLast("client-handler", object : SimpleChannelInboundHandler<FullBinaryMemcacheResponse>() {
|
||||
override fun channelRead0(
|
||||
ctx: ChannelHandlerContext,
|
||||
msg: FullBinaryMemcacheResponse
|
||||
) {
|
||||
pipeline.removeLast()
|
||||
pool.release(channel)
|
||||
msg.touch("The method's caller must remember to release this")
|
||||
response.complete(msg.retain())
|
||||
}
|
||||
val handler = object : SimpleChannelInboundHandler<MemcacheObject>() {
|
||||
override fun channelRead0(
|
||||
ctx: ChannelHandlerContext,
|
||||
msg: MemcacheObject
|
||||
) {
|
||||
when (msg) {
|
||||
is BinaryMemcacheResponse -> responseHandle.handleEvent(
|
||||
StreamingResponseEvent.ResponseReceived(
|
||||
msg
|
||||
)
|
||||
)
|
||||
|
||||
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
|
||||
val ex = when (cause) {
|
||||
is DecoderException -> cause.cause!!
|
||||
else -> cause
|
||||
is LastMemcacheContent -> {
|
||||
responseHandle.handleEvent(
|
||||
StreamingResponseEvent.LastContentReceived(
|
||||
msg
|
||||
)
|
||||
)
|
||||
pipeline.removeLast()
|
||||
pool.release(channel)
|
||||
}
|
||||
ctx.close()
|
||||
pipeline.removeLast()
|
||||
pool.release(channel)
|
||||
response.completeExceptionally(ex)
|
||||
|
||||
is MemcacheContent -> responseHandle.handleEvent(
|
||||
StreamingResponseEvent.ContentReceived(
|
||||
msg
|
||||
)
|
||||
)
|
||||
}
|
||||
})
|
||||
request.touch()
|
||||
channel.writeAndFlush(request)
|
||||
}
|
||||
|
||||
override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
|
||||
responseHandle.handleEvent(StreamingResponseEvent.ExceptionCaught(cause))
|
||||
ctx.close()
|
||||
pipeline.removeLast()
|
||||
pool.release(channel)
|
||||
}
|
||||
}
|
||||
channel.pipeline()
|
||||
.addLast("client-handler", handler)
|
||||
response.complete(object : MemcacheRequestHandle {
|
||||
override fun handleEvent(evt: StreamingRequestEvent) {
|
||||
when (evt) {
|
||||
is StreamingRequestEvent.SendRequest -> {
|
||||
channel.writeAndFlush(evt.request)
|
||||
}
|
||||
|
||||
is StreamingRequestEvent.SendLastChunk -> {
|
||||
channel.writeAndFlush(DefaultLastMemcacheContent(evt.chunk))
|
||||
val value = counter.incrementAndGet()
|
||||
log.debug {
|
||||
"Finished request counter: $value"
|
||||
}
|
||||
}
|
||||
|
||||
is StreamingRequestEvent.SendChunk -> {
|
||||
channel.writeAndFlush(DefaultMemcacheContent(evt.chunk))
|
||||
}
|
||||
|
||||
is StreamingRequestEvent.ExceptionCaught -> {
|
||||
responseHandle.handleEvent(StreamingResponseEvent.ExceptionCaught(evt.exception))
|
||||
channel.close()
|
||||
pipeline.removeLast()
|
||||
pool.release(channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
response.completeExceptionally(channelFuture.cause())
|
||||
}
|
||||
@@ -146,107 +173,6 @@ class MemcacheClient(private val cfg: MemcacheCacheConfiguration) : AutoCloseabl
|
||||
return response
|
||||
}
|
||||
|
||||
private fun encodeExpiry(expiry: Duration): Int {
|
||||
val expirySeconds = expiry.toSeconds()
|
||||
return expirySeconds.toInt().takeIf { it.toLong() == expirySeconds }
|
||||
?: Instant.ofEpochSecond(expirySeconds).epochSecond.toInt()
|
||||
}
|
||||
|
||||
fun get(key: String): CompletableFuture<ReadableByteChannel?> {
|
||||
val request = (cfg.digestAlgorithm
|
||||
?.let(MessageDigest::getInstance)
|
||||
?.let { md ->
|
||||
digest(key.toByteArray(), md)
|
||||
} ?: key.toByteArray(Charsets.UTF_8)).let { digest ->
|
||||
DefaultFullBinaryMemcacheRequest(Unpooled.wrappedBuffer(digest), null).apply {
|
||||
setOpcode(BinaryMemcacheOpcodes.GET)
|
||||
}
|
||||
}
|
||||
return sendRequest(request).thenApply { response ->
|
||||
try {
|
||||
when (val status = response.status()) {
|
||||
BinaryMemcacheResponseStatus.SUCCESS -> {
|
||||
val compressionMode = cfg.compressionMode
|
||||
val content = response.content().retain()
|
||||
content.touch()
|
||||
if (compressionMode != null) {
|
||||
when (compressionMode) {
|
||||
MemcacheCacheConfiguration.CompressionMode.GZIP -> {
|
||||
GZIPInputStream(ByteBufInputStream(content))
|
||||
}
|
||||
|
||||
MemcacheCacheConfiguration.CompressionMode.DEFLATE -> {
|
||||
InflaterInputStream(ByteBufInputStream(content))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ByteBufInputStream(content)
|
||||
}.let(Channels::newChannel)
|
||||
}
|
||||
|
||||
BinaryMemcacheResponseStatus.KEY_ENOENT -> {
|
||||
null
|
||||
}
|
||||
|
||||
else -> throw MemcacheException(status)
|
||||
}
|
||||
} finally {
|
||||
response.release()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun put(key: String, content: ByteBuf, expiry: Duration, cas: Long? = null): CompletableFuture<Void> {
|
||||
val request = (cfg.digestAlgorithm
|
||||
?.let(MessageDigest::getInstance)
|
||||
?.let { md ->
|
||||
digest(key.toByteArray(), md)
|
||||
} ?: key.toByteArray(Charsets.UTF_8)).let { digest ->
|
||||
val extras = Unpooled.buffer(8, 8)
|
||||
extras.writeInt(0)
|
||||
extras.writeInt(encodeExpiry(expiry))
|
||||
val compressionMode = cfg.compressionMode
|
||||
content.retain()
|
||||
val payload = if (compressionMode != null) {
|
||||
val inputStream = ByteBufInputStream(content)
|
||||
val buf = content.alloc().buffer()
|
||||
buf.retain()
|
||||
val outputStream = when (compressionMode) {
|
||||
MemcacheCacheConfiguration.CompressionMode.GZIP -> {
|
||||
GZIPOutputStream(ByteBufOutputStream(buf))
|
||||
}
|
||||
|
||||
MemcacheCacheConfiguration.CompressionMode.DEFLATE -> {
|
||||
DeflaterOutputStream(ByteBufOutputStream(buf), Deflater(Deflater.DEFAULT_COMPRESSION, false))
|
||||
}
|
||||
}
|
||||
inputStream.use { i ->
|
||||
outputStream.use { o ->
|
||||
JWO.copy(i, o)
|
||||
}
|
||||
}
|
||||
buf
|
||||
} else {
|
||||
content
|
||||
}
|
||||
DefaultFullBinaryMemcacheRequest(Unpooled.wrappedBuffer(digest), extras, payload).apply {
|
||||
setOpcode(BinaryMemcacheOpcodes.SET)
|
||||
cas?.let(this::setCas)
|
||||
}
|
||||
}
|
||||
return sendRequest(request).thenApply { response ->
|
||||
try {
|
||||
when (val status = response.status()) {
|
||||
BinaryMemcacheResponseStatus.SUCCESS -> null
|
||||
else -> throw MemcacheException(status)
|
||||
}
|
||||
} finally {
|
||||
response.release()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun shutDown(): NettyFuture<*> {
|
||||
return group.shutdownGracefully()
|
||||
}
|
||||
|
@@ -20,7 +20,8 @@
|
||||
<xs:element name="server" type="rbcs-memcache:memcacheServerType"/>
|
||||
</xs:sequence>
|
||||
<xs:attribute name="max-age" type="xs:duration" default="P1D"/>
|
||||
<xs:attribute name="max-size" type="xs:unsignedInt" default="1048576"/>
|
||||
<xs:attribute name="max-size" type="rbcs:byteSize" default="1048576"/>
|
||||
<xs:attribute name="chunk-size" type="rbcs:byteSize" default="0x4000"/>
|
||||
<xs:attribute name="digest" type="xs:token" />
|
||||
<xs:attribute name="compression-mode" type="rbcs-memcache:compressionType"/>
|
||||
</xs:extension>
|
||||
@@ -30,7 +31,6 @@
|
||||
<xs:simpleType name="compressionType">
|
||||
<xs:restriction base="xs:token">
|
||||
<xs:enumeration value="deflate"/>
|
||||
<xs:enumeration value="gzip"/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
|
||||
|
Reference in New Issue
Block a user