Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Yamux] Implement basic backpressure #324

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun interface StreamMuxerProtocol {
}

/**
* @param maxBufferedConnectionWrites the maximum amount of bytes in the write buffer per connection
* @param maxBufferedConnectionWrites the maximum amount of bytes in the internal write buffer per connection
*/
@JvmStatic
@JvmOverloads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ abstract class AbstractMuxHandler<TData>() :
*/
abstract fun releaseMessage(msg: TData)

abstract fun isChildWritable(child: MuxChannel<TData>): Boolean

abstract fun onChildWrite(child: MuxChannel<TData>, data: TData)

protected fun onRemoteOpen(id: MuxId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class MuxChannel<TData>(
initializer(this)
}

override fun isWritable(): Boolean {
return super.isWritable() && parent.isChildWritable(this)
}

override fun doWrite(buf: ChannelOutboundBuffer) {
while (true) {
val msg = buf.current() ?: break
Expand Down
4 changes: 4 additions & 0 deletions libp2p/src/main/kotlin/io/libp2p/mux/mplex/MplexHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ open class MplexHandler(
}
}

override fun isChildWritable(child: MuxChannel<ByteBuf>): Boolean {
return true
}

override fun onChildWrite(child: MuxChannel<ByteBuf>, data: ByteBuf) {
val ctx = getChannelHandlerContext()
data.sliceMaxSize(maxFrameDataLength)
Expand Down
12 changes: 11 additions & 1 deletion libp2p/src/main/kotlin/io/libp2p/mux/yamux/YamuxHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ open class YamuxHandler(
goAwayPromise.complete(msg.length)
}

override fun isChildWritable(child: MuxChannel<ByteBuf>): Boolean {
val windowSize = windowSizes[child.id]?.send
return if (windowSize == null) {
false
} else {
windowSize.get() > 0
}
}

override fun onChildWrite(child: MuxChannel<ByteBuf>, data: ByteBuf) {
val windowSize = windowSizes[child.id]?.send
if (windowSize == null) {
Expand Down Expand Up @@ -192,12 +201,13 @@ open class YamuxHandler(
val frame = YamuxFrame(child.id, YamuxType.DATA, 0, length.toLong(), slicedData)
getChannelHandlerContext().writeAndFlush(frame)
} else {
// wait until the window is increased to send
// add to internal outbound buffer until the window is increased
addToSendBuffer(child, data)
}
}
}

// Can't rely only on the Netty outbound buffer to handle window updates, so specifying an internal outbound buffer
private fun addToSendBuffer(child: MuxChannel<ByteBuf>, data: ByteBuf) {
val buffer = sendBuffers.getOrPut(child.id) { SendBuffer(child.id) }
buffer.add(data)
Expand Down