Clean up write logic (#385)

Motivation:

Unnecessary empty writes should be avoided, and we should avoid
completing promises too often.

Modifications:

- Avoid sending a split promise on every write when writes are too big.
- Avoid sending a zero-length write when writes are too big.

Result:

Slightly better write behaviour.

Note that this is fundamentally un-testable: there's no place to put a
test hook here, and we coalesce writes on the far side, so we can't do
much here.
This commit is contained in:
Cory Benfield authored and GitHub committed 2022-07-27 01:59:04 -07:00
1 parent 45fe612e84
commit ff0fccddad
3 files changed
+13 -6

No files matched your search

+5 -2
View File
@@ -698,9 +698,12 @@ extension NIOSSLHandler {
// Here we guard against the possibility that any of these writes are larger than CInt.max.
// This is very unusual but it can happen. To work around it, we just pretend that there were
// multiple writes.
//
// During the short writes we set the promise to `nil` to make sure they only arrive at the end.
// Note that we make sure that there's always a single write, at the end, that holds the promise.
let maxWriteSize = Int(CInt.max)
while let slice = data.readSlice(length: maxWriteSize) {
bufferedWrites.append((data: slice, promise: promise))
while data.readableBytes > maxWriteSize, let slice = data.readSlice(length: maxWriteSize) {
bufferedWrites.append((data: slice, promise: nil))
}
assert(data.readableBytes <= maxWriteSize)
@@ -2405,8 +2405,7 @@ class NIOSSLIntegrationTest: XCTestCase {
// for an existing bug.
// We only run this test on 64-bit systems where we can safely allocate enough memory.
try XCTSkipIf(MemoryLayout<Int>.size <= 4)
let targetSize = (1 << 31) + 1
let targetSize = Int(CInt.max) + 1
let write = ByteBuffer(repeating: 0, count: targetSize)
let b2b = BackToBackEmbeddedChannel()
@@ -2434,7 +2433,11 @@ class NIOSSLIntegrationTest: XCTestCase {
)
XCTAssertNoThrow(try b2b.connectInMemory())
b2b.client.writeAndFlush(write, promise: nil)
var completed = false
let promise = b2b.loop.makePromise(of: Void.self)
promise.futureResult.whenComplete { _ in completed = true }
b2b.client.writeAndFlush(write, promise: promise)
try b2b.interactInMemory()
var reads: [ByteBuffer] = []
@@ -2443,6 +2446,7 @@ class NIOSSLIntegrationTest: XCTestCase {
}
let totalReadBytes = reads.reduce(into: 0, { $0 += $1.readableBytes })
XCTAssertEqual(totalReadBytes, targetSize)
XCTAssertTrue(completed)
b2b.client.close(promise: nil)
try b2b.interactInMemory()
+1 -1
View File
@@ -669,7 +669,7 @@ func generateSelfSignedCert(keygenFunction: () -> UnsafeMutablePointer<EVP_PKEY>
final class BackToBackEmbeddedChannel {
private(set) var client: EmbeddedChannel
private(set) var server: EmbeddedChannel
private var loop: EmbeddedEventLoop
private(set) var loop: EmbeddedEventLoop
init() {