More consistent handling of writable state changes, so it matches netty

This commit is contained in:
Norman Maurer committed 2017-06-19 15:05:29 +02:00
1 parent 255afbdeca
commit 57cd379950
6 files changed
+49 -30

No files matched your search

+28 -13
View File
@@ -14,6 +14,7 @@
import Foundation
import Sockets
import ConcurrencyHelpers
#if os(Linux)
import Glibc
@@ -104,12 +105,17 @@ final fileprivate class PendingWrites {
private var storageRefs: UnsafeMutableBufferPointer<Unmanaged<AnyObject>>
fileprivate var waterMark: WriteBufferWaterMark = WriteBufferWaterMark(32 * 1024..<64 * 1024)
private var writable = true
private var writable: Atomic<Bool> = Atomic(value: true)
fileprivate var writeSpinCount: UInt = 16
private(set) var outstanding: (chunks: Int, bytes: Int) = (0, 0)
private(set) var closed = false
var isWritable: Bool {
return writable.load()
}
var isEmpty: Bool {
return tail == nil
@@ -128,8 +134,7 @@ final fileprivate class PendingWrites {
}
outstanding = (chunks: outstanding.chunks + 1,
bytes: outstanding.bytes + buffer.readableBytes)
if writable && outstanding.bytes > Int(waterMark.upperBound) {
writable = false
if outstanding.bytes > Int(waterMark.upperBound) && writable.compareAndExchange(expected: true, desired: false) {
// Returns false to signal the Channel became non-writable and we need to notify the user
return false
}
@@ -148,12 +153,12 @@ final fileprivate class PendingWrites {
Function that takes two closures and based on if there are more then one ByteBuffer pending calls either one or the other.
*/
func consume(oneBody: (UnsafePointer<UInt8>, Int) throws -> IOResult<Int>, multipleBody: (UnsafeBufferPointer<IOVector>) throws -> IOResult<Int>) throws -> (writeResult: WriteResult, writable: Bool) {
let wasWritable = writable
let wasWritable = writable.load()
let result = try hasMultiple ? consumeMultiple(multipleBody) : consumeOne(oneBody)
if !wasWritable {
// Was not writable before so signal back to the caller the possible state change
return (result, writable)
return (result, writable.load())
}
return (result, false)
}
@@ -183,8 +188,8 @@ final fileprivate class PendingWrites {
case .processed(let written):
outstanding = (outstanding.chunks, outstanding.bytes - written)
if !writable && outstanding.bytes < Int(waterMark.lowerBound) {
writable = true
if outstanding.bytes < Int(waterMark.lowerBound) {
writable.store(true)
}
if pending.buffer.readableBytes == written {
@@ -234,8 +239,8 @@ final fileprivate class PendingWrites {
case .processed(let written):
outstanding = (outstanding.chunks, outstanding.bytes - written)
if !writable && outstanding.bytes < Int(waterMark.lowerBound) {
writable = true
if outstanding.bytes < Int(waterMark.lowerBound) {
writable.store(true)
}
var w = written
@@ -379,6 +384,8 @@ final class SocketChannel : BaseSocketChannel<Socket> {
if bytesRead > 0 {
recvAllocator.record(actualReadBytes: bytesRead)
readPending = false
pipeline.fireChannelRead0(data: .byteBuffer(buffer))
// Reset reader and writerIndex and so allow to have the buffer filled again
@@ -417,7 +424,7 @@ final class SocketChannel : BaseSocketChannel<Socket> {
})
if result.writable {
// writable again
self.pipeline.fireChannelWritabilityChanged0(writable: true)
self.pipeline.fireChannelWritabilityChanged0()
}
if result.writeResult == .writtenCompletely && pendingWrites.isFlushPending {
// there are more buffers to process, so continue
@@ -499,6 +506,8 @@ final class ServerSocketChannel : BaseSocketChannel<ServerSocket> {
return
}
if let accepted = try self.socket.accept() {
readPending = false
do {
pipeline.fireChannelRead0(data: .other(try SocketChannel(socket: accepted, eventLoop: group.next() as! SelectableEventLoop)))
} catch let err {
@@ -564,6 +573,8 @@ public protocol Channel : class, ChannelOutboundInvoker {
func setOption<T: ChannelOption>(option: T, value: T.OptionType) throws
func getOption<T: ChannelOption>(option: T) throws -> T.OptionType
var isWritable: Bool { get }
var _unsafe: ChannelCore { get }
}
@@ -584,6 +595,7 @@ extension Channel {
return _unsafe.eventLoop
}
@discardableResult public func bind(local: SocketAddress, promise: Promise<Void>) -> Future<Void> {
pipeline.bind(local: local, promise: promise)
return promise.futureResult
@@ -642,7 +654,7 @@ class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
}
private let pendingWrites: PendingWrites
private var readPending = false
fileprivate var readPending = false
private var neverRegistered = true
private var pendingConnect: Promise<Void>?
private let closePromise: Promise<Void>
@@ -657,6 +669,10 @@ class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
return selectableEventLoop
}
public final var isWritable: Bool {
return pendingWrites.isWritable
}
public final var allocator: ByteBufferAllocator {
if eventLoop.inEventLoop {
return bufferAllocator
@@ -830,7 +846,7 @@ class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
}
if let buffer = data.tryAsByteBuffer() {
if !pendingWrites.add(buffer: buffer, promise: promise) {
pipeline.fireChannelWritabilityChanged0(writable: false)
pipeline.fireChannelWritabilityChanged0()
}
} else {
// Only support ByteBuffer for now.
@@ -1008,7 +1024,6 @@ class BaseSocketChannel<T : BaseSocket> : SelectableChannel, ChannelCore {
assert(eventLoop.inEventLoop)
assert(!closed)
readPending = false
defer {
if !closed, !readPending {
unregisterForReadable()
+3 -3
View File
@@ -22,7 +22,7 @@ public protocol ChannelHandler : class {
func channelInactive(ctx: ChannelHandlerContext) throws
func channelRead(ctx: ChannelHandlerContext, data: IOData) throws
func channelReadComplete(ctx: ChannelHandlerContext) throws
func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) throws
func channelWritabilityChanged(ctx: ChannelHandlerContext) throws
func userEventTriggered(ctx: ChannelHandlerContext, event: Any) throws
func errorCaught(ctx: ChannelHandlerContext, error: Error) throws
func register(ctx: ChannelHandlerContext, promise: Promise<Void>)
@@ -64,8 +64,8 @@ public extension ChannelHandler {
ctx.fireChannelReadComplete()
}
public func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) {
ctx.fireChannelWritabilityChanged(writable: writable)
public func channelWritabilityChanged(ctx: ChannelHandlerContext) {
ctx.fireChannelWritabilityChanged()
}
public func userEventTriggered(ctx: ChannelHandlerContext, event: Any) {
+3 -3
View File
@@ -35,8 +35,8 @@ public class BackPressureHandler: ChannelHandler {
}
}
public func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) {
self.writable = writable
public func channelWritabilityChanged(ctx: ChannelHandlerContext) {
self.writable = ctx.channel!.isWritable
if writable {
if readPending {
readPending = false
@@ -47,7 +47,7 @@ public class BackPressureHandler: ChannelHandler {
}
// Propagate the event as the user may still want to do something based on it.
ctx.fireChannelWritabilityChanged(writable: writable)
ctx.fireChannelWritabilityChanged()
}
public func handlerRemoved(ctx: ChannelHandlerContext) {
+1 -1
View File
@@ -26,7 +26,7 @@ public protocol ChannelInboundInvoker {
func fireChannelReadComplete()
func fireChannelWritabilityChanged(writable: Bool)
func fireChannelWritabilityChanged()
func fireErrorCaught(error: Error)
+10 -10
View File
@@ -253,12 +253,12 @@ public final class ChannelPipeline : ChannelInboundInvoker {
}
}
public func fireChannelWritabilityChanged(writable: Bool) {
public func fireChannelWritabilityChanged() {
if eventLoop.inEventLoop {
fireChannelWritabilityChanged0(writable: writable)
fireChannelWritabilityChanged0()
} else {
eventLoop.execute {
self.fireChannelWritabilityChanged0(writable: writable)
self.fireChannelWritabilityChanged0()
}
}
}
@@ -392,8 +392,8 @@ public final class ChannelPipeline : ChannelInboundInvoker {
head!.invokeChannelReadComplete()
}
func fireChannelWritabilityChanged0(writable: Bool) {
head!.invokeChannelWritabilityChanged(writable: writable)
func fireChannelWritabilityChanged0() {
head!.invokeChannelWritabilityChanged()
}
func fireUserEventTriggered0(event: Any) {
@@ -503,7 +503,7 @@ private final class TailChannelHandler : ChannelHandler {
// Discard
}
func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) {
func channelWritabilityChanged(ctx: ChannelHandlerContext) {
// Discard
}
@@ -576,8 +576,8 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun
next!.invokeChannelReadComplete()
}
public func fireChannelWritabilityChanged(writable: Bool) {
next!.invokeChannelWritabilityChanged(writable: writable)
public func fireChannelWritabilityChanged() {
next!.invokeChannelWritabilityChanged()
}
public func fireErrorCaught(error: Error) {
@@ -686,11 +686,11 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun
}
}
func invokeChannelWritabilityChanged(writable: Bool) {
func invokeChannelWritabilityChanged() {
assert(inEventLoop)
do {
try handler.channelWritabilityChanged(ctx: self, writable: writable)
try handler.channelWritabilityChanged(ctx: self)
} catch let err {
invokeErrorCaught(error: err)
}
+4
View File
@@ -140,6 +140,10 @@ public class EmbeddedChannel : Channel {
return _pipeline
}
public var isWritable: Bool {
return true
}
private var _pipeline: ChannelPipeline!
public let allocator: ByteBufferAllocator = ByteBufferAllocator()
public var eventLoop: EventLoop = EmbeddedEventLoop()