mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-27 05:04:23 +00:00
Replace Future with a function call. (#116)
Motivation: Sometimes when you have a hammer everything looks like a nail. In this case, my hammer was Futures. But we don't need to use a Future here: these two classes are tightly coupled, and so we can safely just call from one to the other. A side note: while this appears to create a reference cycle, the act of calling the function in question will actually break that cycle. So it's all good! Modifications: - Remove a future callback. - Have the stream channel hold a reference to the multiplexer. Result: Fewer heap allocations.
This commit is contained in:
1 parent
016bf0be4d
commit
f1cc1f5069
2 files changed
+9
-8
No files matched your search
@@ -76,7 +76,7 @@ private enum StreamChannelState {
|
||||
|
||||
|
||||
final class HTTP2StreamChannel: Channel, ChannelCore {
|
||||
internal init(allocator: ByteBufferAllocator, parent: Channel, streamID: HTTP2StreamID, targetWindowSize: Int32, initiatedRemotely: Bool) {
|
||||
internal init(allocator: ByteBufferAllocator, parent: Channel, multiplexer: HTTP2StreamMultiplexer, streamID: HTTP2StreamID, targetWindowSize: Int32, initiatedRemotely: Bool) {
|
||||
self.allocator = allocator
|
||||
self.closePromise = parent.eventLoop.makePromise()
|
||||
self.localAddress = parent.localAddress
|
||||
@@ -84,6 +84,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
|
||||
self.parent = parent
|
||||
self.eventLoop = parent.eventLoop
|
||||
self.streamID = streamID
|
||||
self.multiplexer = multiplexer
|
||||
self.windowManager = InboundWindowManager(targetSize: Int32(targetWindowSize))
|
||||
// FIXME: that's just wrong
|
||||
self.isWritable = true
|
||||
@@ -148,6 +149,8 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
|
||||
|
||||
private let closePromise: EventLoopPromise<()>
|
||||
|
||||
private let multiplexer: HTTP2StreamMultiplexer
|
||||
|
||||
public var closeFuture: EventLoopFuture<Void> {
|
||||
return self.closePromise.futureResult
|
||||
}
|
||||
@@ -373,6 +376,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
|
||||
self.eventLoop.execute {
|
||||
self.removeHandlers(channel: self)
|
||||
self.closePromise.succeed(())
|
||||
self.multiplexer.childChannelClosed(streamID: self.streamID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,6 +397,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
|
||||
self.eventLoop.execute {
|
||||
self.removeHandlers(channel: self)
|
||||
self.closePromise.fail(error)
|
||||
self.multiplexer.childChannelClosed(streamID: self.streamID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,14 +60,12 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
|
||||
} else if case .headers = frame.payload {
|
||||
let channel = HTTP2StreamChannel(allocator: self.channel.allocator,
|
||||
parent: self.channel,
|
||||
multiplexer: self,
|
||||
streamID: streamID,
|
||||
targetWindowSize: 65535,
|
||||
initiatedRemotely: true)
|
||||
self.streams[streamID] = channel
|
||||
channel.configure(initializer: self.inboundStreamStateInitializer, userPromise: nil)
|
||||
channel.closeFuture.whenComplete { _ in
|
||||
self.childChannelClosed(streamID: streamID)
|
||||
}
|
||||
channel.receiveInboundFrame(frame)
|
||||
} else {
|
||||
// This frame is for a stream we know nothing about. We can't do much about it, so we
|
||||
@@ -121,7 +119,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
|
||||
context.fireUserInboundEventTriggered(event)
|
||||
}
|
||||
|
||||
private func childChannelClosed(streamID: HTTP2StreamID) {
|
||||
internal func childChannelClosed(streamID: HTTP2StreamID) {
|
||||
self.streams.removeValue(forKey: streamID)
|
||||
}
|
||||
|
||||
@@ -180,14 +178,12 @@ extension HTTP2StreamMultiplexer {
|
||||
self.nextOutboundStreamID = HTTP2StreamID(Int32(streamID) + 2)
|
||||
let channel = HTTP2StreamChannel(allocator: self.channel.allocator,
|
||||
parent: self.channel,
|
||||
multiplexer: self,
|
||||
streamID: streamID,
|
||||
targetWindowSize: 65535, // TODO: make configurable
|
||||
initiatedRemotely: false)
|
||||
self.streams[streamID] = channel
|
||||
channel.configure(initializer: streamStateInitializer, userPromise: promise)
|
||||
channel.closeFuture.whenComplete { _ in
|
||||
self.childChannelClosed(streamID: streamID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user