From 5fc8ba1f4f527218d7a824590abd3dc9acab5c71 Mon Sep 17 00:00:00 2001 From: George Barnett Date: Wed, 24 Jul 2019 16:00:11 +0100 Subject: [PATCH] Track the event observer and factory with state --- .../BidirectionalStreamingCallHandler.swift | 28 +++++++++------ .../ClientStreamingCallHandler.swift | 35 +++++++++++++------ 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Sources/GRPC/CallHandlers/BidirectionalStreamingCallHandler.swift b/Sources/GRPC/CallHandlers/BidirectionalStreamingCallHandler.swift index 80beb8a69..df9820bf0 100644 --- a/Sources/GRPC/CallHandlers/BidirectionalStreamingCallHandler.swift +++ b/Sources/GRPC/CallHandlers/BidirectionalStreamingCallHandler.swift @@ -25,11 +25,12 @@ import NIOHTTP1 /// they can fail the observer block future. /// - To close the call and send the status, complete `context.statusPromise`. public class BidirectionalStreamingCallHandler: BaseCallHandler { + public typealias Context = StreamingResponseCallContext public typealias EventObserver = (StreamEvent) -> Void - private var eventObserver: EventLoopFuture? - private let eventObserverFactory: (StreamingResponseCallContext) -> EventLoopFuture + public typealias EventObserverFactory = (Context) -> EventLoopFuture - private var callContext: StreamingResponseCallContext? + private var observerState: ClientStreamingHandlerObserverState + private var callContext: Context? // We ask for a future of type `EventObserver` to allow the framework user to e.g. asynchronously authenticate a call. // If authentication fails, they can simply fail the observer future, which causes the call to be terminated. @@ -37,7 +38,7 @@ public class BidirectionalStreamingCallHandler(channel: channel, request: request, errorDelegate: errorDelegate) self.callContext = context @@ -46,15 +47,20 @@ public class BidirectionalStreamingCallHandler` message is already present on the channel. @@ -64,13 +70,15 @@ public class BidirectionalStreamingCallHandler { + case pendingCreation(Factory) + case created(EventLoopFuture) + case notRequired +} + /// Handles client-streaming calls. Forwards incoming messages and end-of-stream events to the observer block. /// /// - The observer block is implemented by the framework user and fulfills `context.responsePromise` when done. @@ -25,19 +33,20 @@ import NIOHTTP1 /// they can fail the observer block future. /// - To close the call and send the response, complete `context.responsePromise`. public class ClientStreamingCallHandler: BaseCallHandler { + public typealias Context = UnaryResponseCallContext public typealias EventObserver = (StreamEvent) -> Void - private var eventObserver: EventLoopFuture? - private let eventObserverFactory: (UnaryResponseCallContext) -> EventLoopFuture + public typealias EventObserverFactory = (Context) -> EventLoopFuture + private var observerState: ClientStreamingHandlerObserverState private var callContext: UnaryResponseCallContext? // We ask for a future of type `EventObserver` to allow the framework user to e.g. asynchronously authenticate a call. // If authentication fails, they can simply fail the observer future, which causes the call to be terminated. - public init(channel: Channel, request: HTTPRequestHead, errorDelegate: ServerErrorDelegate?, eventObserverFactory: @escaping (UnaryResponseCallContext) -> EventLoopFuture) { + public init(channel: Channel, request: HTTPRequestHead, errorDelegate: ServerErrorDelegate?, eventObserverFactory: @escaping EventObserverFactory) { // Delay the creation of the event observer until `handlerAdded(context:)`, otherwise it is // possible for the service to write into the pipeline (by fulfilling the response promise // of the call context outside of the observer) before it has been configured. - self.eventObserverFactory = eventObserverFactory + self.observerState = .pendingCreation(eventObserverFactory) let callContext = UnaryResponseCallContextImpl(channel: channel, request: request, errorDelegate: errorDelegate) self.callContext = callContext @@ -46,15 +55,19 @@ public class ClientStreamingCallHandler