Sfoglia il codice sorgente

Add a method to `ServerSessionClientStreaming` to return an error status without a result.

Daniel Alm 7 anni fa
parent
commit
6af3914244

+ 5 - 1
Sources/Examples/Echo/Generated/echo.grpc.swift

@@ -219,9 +219,13 @@ internal protocol Echo_EchoCollectSession: ServerSessionClientStreaming {
   /// Call this to wait for a result. Nonblocking.
   func receive(completion: @escaping (ResultOrRPCError<Echo_EchoRequest?>) -> Void) throws
 
+  /// You MUST call one of these two methods once you are done processing the request.
   /// Close the connection and send a single result. Non-blocking.
-  /// You MUST call this method once you are done processing the request.
   func sendAndClose(response: Echo_EchoResponse, status: ServerStatus, completion: ((CallResult) -> Void)?) throws
+  /// Close the connection and send an error. Non-blocking.
+  /// Use this method if you encountered an error that makes it impossible to send a response.
+  /// Accordingly, it does not make sense to call this method with a status of `.ok`.
+  func sendErrorAndClose(status: ServerStatus, completion: ((CallResult) -> Void)?) throws
 }
 
 fileprivate final class Echo_EchoCollectSessionBase: ServerSessionClientStreamingBase<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectSession {}

+ 9 - 0
Sources/SwiftGRPC/Runtime/ServerSessionClientStreaming.swift

@@ -36,6 +36,10 @@ open class ServerSessionClientStreamingBase<InputType: Message, OutputType: Mess
     try handler.sendResponse(message: response.serializedData(), status: status, completion: completion)
   }
 
+  public func sendErrorAndClose(status: ServerStatus, completion: ((CallResult) -> Void)? = nil) throws {
+    try handler.sendStatus(status, completion: completion)
+  }
+  
   public func run(queue: DispatchQueue) throws {
     try handler.sendMetadata(initialMetadata: initialMetadata) { success in
       queue.async {
@@ -86,5 +90,10 @@ open class ServerSessionClientStreamingTestStub<InputType: Message, OutputType:
     completion?(.fakeOK)
   }
 
+  open func sendErrorAndClose(status: ServerStatus, completion: ((CallResult) -> Void)? = nil) throws {
+    self.status = status
+    completion?(.fakeOK)
+  }
+  
   open func close() throws {}
 }

+ 5 - 1
Sources/protoc-gen-swiftgrpc/Generator-Server.swift

@@ -143,9 +143,13 @@ extension Generator {
   }
   
   private func printServerMethodSendAndClose(sentType: String) {
+    println("/// You MUST call one of these two methods once you are done processing the request.")
     println("/// Close the connection and send a single result. Non-blocking.")
-    println("/// You MUST call this method once you are done processing the request.")
     println("func sendAndClose(response: \(sentType), status: ServerStatus, completion: ((CallResult) -> Void)?) throws")
+    println("/// Close the connection and send an error. Non-blocking.")
+    println("/// Use this method if you encountered an error that makes it impossible to send a response.")
+    println("/// Accordingly, it does not make sense to call this method with a status of `.ok`.")
+    println("func sendErrorAndClose(status: ServerStatus, completion: ((CallResult) -> Void)?) throws")
   }
 
   private func printServerMethodClientStreaming() {