Skip to content

Commit

Permalink
Issue-2743 - Clamp after Flush
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ahsan-ali committed Jul 4, 2024
1 parent 01e8cf3 commit 825a6b3
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Sources/NIOCore/Codec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -768,16 +768,16 @@ public final class MessageToByteHandler<Encoder: MessageToByteEncoder>: ChannelO
private var state: State = .notInChannelYet
private let encoder: Encoder
private var buffer: ByteBuffer? = nil
private let maxBufferCapacity: Int?
private let desiredBufferCapacity: Int?

public init(_ encoder: Encoder, maxBufferCapacity: Int) {
public init(_ encoder: Encoder, desiredBufferCapacity: Int) {
self.encoder = encoder
self.maxBufferCapacity = maxBufferCapacity.nextPowerOf2()
self.desiredBufferCapacity = desiredBufferCapacity.nextPowerOf2()
}

public init(_ encoder: Encoder) {
self.encoder = encoder
self.maxBufferCapacity = nil
self.desiredBufferCapacity = nil
}
}

Expand Down Expand Up @@ -818,13 +818,17 @@ extension MessageToByteHandler {
self.buffer!.clear()
try self.encoder.encode(data: data, out: &self.buffer!)
context.write(self.wrapOutboundOut(self.buffer!), promise: promise)
if let maxBufferCapacity {
self.buffer?.clampBufferCapacity(to: maxBufferCapacity)
}
} catch {
self.state = .error(error)
promise?.fail(error)
context.fireErrorCaught(error)
}
}

public func flush(context: ChannelHandlerContext) {
context.flush()
if let desiredBufferCapacity {
self.buffer?.clampBufferCapacity(to: desiredBufferCapacity)
}
}
}

0 comments on commit 825a6b3

Please sign in to comment.