Skip to content

Commit

Permalink
fix(vertx): reads should not wait for requested bytes
Browse files Browse the repository at this point in the history
when receiving a chunk of bytes socket should check requested as an upper bound rather than wait for it to be available
  • Loading branch information
Malinskiy committed May 19, 2021
1 parent aacea43 commit 8795891
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.vertx.core.Handler
import io.vertx.core.buffer.Buffer
import io.vertx.core.impl.Arguments
import io.vertx.core.streams.ReadStream
import kotlin.math.min

class VariableSizeRecordParser(
private val stream: ReadStream<Buffer>? = null
Expand All @@ -31,7 +32,7 @@ class VariableSizeRecordParser(
private val bufferLock = Object()
private var pos = 0 // Current position in buffer
private var start = 0 // Position of beginning of current record
private var requested = 0
private var requestedAtMost = 0
private var maxRecordSize = 0
private var demand = Long.MAX_VALUE
private var eventHandler: Handler<Buffer>? = null
Expand Down Expand Up @@ -63,7 +64,7 @@ class VariableSizeRecordParser(

fun request(size: Int) {
Arguments.require(size > 0, "Size must be > 0")
requested = size
requestedAtMost = size
handleParsing()
}

Expand All @@ -83,7 +84,7 @@ class VariableSizeRecordParser(
break
}
}
requested = 0
requestedAtMost = 0
if (demand != Long.MAX_VALUE) {
demand--
}
Expand Down Expand Up @@ -117,11 +118,12 @@ class VariableSizeRecordParser(
private fun parseFixed(): Int {
val length = buff.length()
val available = length - start
if (available >= requested && requested > 0) {
val end = start + requested
if (available > 0 && requestedAtMost > 0) {
val toSend = min(requestedAtMost, available)
val end = start + toSend
pos = end
return end
} else if (streamEnded && available > 0 && available < requested) {
} else if (streamEnded && available > 0 && available < requestedAtMost) {
val end = start + length
pos = end
return end
Expand Down

0 comments on commit 8795891

Please sign in to comment.