diff --git a/Sources/NIOHTTP2/HTTP2StreamChannel.swift b/Sources/NIOHTTP2/HTTP2StreamChannel.swift index 889fac0..dbeaf79 100644 --- a/Sources/NIOHTTP2/HTTP2StreamChannel.swift +++ b/Sources/NIOHTTP2/HTTP2StreamChannel.swift @@ -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 { 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) } } diff --git a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift index a9d1a23..6ceca82 100644 --- a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift +++ b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift @@ -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) - } } } }