Browse Source

Convert sendMessage to throw errors when the channel is full.

Tim Burks 9 years ago
parent
commit
fa8af466ec

+ 2 - 2
Examples/Echo/Swift/Generated/echo.client.pb.swift

@@ -156,7 +156,7 @@ public class Echo_EchoCollectCall {
   // Call this to send each message in the request stream.
   public func Send(_ message: Echo_EchoRequest) throws {
     let messageData = try message.serializeProtobuf()
-    _ = call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData)
   }
 
   // Call this to close the connection and wait for a response. Blocks.
@@ -238,7 +238,7 @@ public class Echo_EchoUpdateCall {
 
   public func Send(_ message:Echo_EchoRequest) throws {
     let messageData = try message.serializeProtobuf()
-    _ = call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData)
   }
 
   public func CloseSend() throws {

+ 1 - 1
Plugin/swiftgrpc.templates/client-call-bidistreaming.swift

@@ -44,7 +44,7 @@ public class {{ .|call:protoFile,service,method }} {
 
   public func Send(_ message:{{ method|input }}) throws {
     let messageData = try message.serializeProtobuf()
-    _ = call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData)
   }
 
   public func CloseSend() throws {

+ 1 - 1
Plugin/swiftgrpc.templates/client-call-clientstreaming.swift

@@ -18,7 +18,7 @@ public class {{ .|call:protoFile,service,method }} {
   // Call this to send each message in the request stream.
   public func Send(_ message: {{ method|input }}) throws {
     let messageData = try message.serializeProtobuf()
-    _ = call.sendMessage(data:messageData)
+    try call.sendMessage(data:messageData)
   }
 
   // Call this to close the connection and wait for a response. Blocks.

+ 9 - 10
Sources/gRPC/Call.swift

@@ -43,6 +43,10 @@ public enum CallStyle {
   case bidiStreaming
 }
 
+public enum CallWarning : Error {
+  case blocked
+}
+
 public enum CallError : Error {
   case ok
   case unknown
@@ -230,24 +234,18 @@ public class Call {
   /// Sends a message over a streaming connection.
   ///
   /// Parameter data: the message data to send
-  /// Returns: true if the message could be queued or sent, false if the queue is full
-  public func sendMessage(data: Data) -> Bool {
+  public func sendMessage(data: Data) throws {
     self.sendMutex.lock()
     defer {self.sendMutex.unlock()}
     if self.writing {
       if self.pendingMessages.count == Call.maximumQueuedMessages {
-        return false
+        throw CallWarning.blocked
       }
-      self.pendingMessages.append(data) // TODO: return something if we can't accept another message
+      self.pendingMessages.append(data) 
     } else {
       self.writing = true
-      do {
-        try self.sendWithoutBlocking(data: data)
-      } catch (let callError) {
-        print("Call sendMessage: grpc error \(callError)")
-      }
+      try self.sendWithoutBlocking(data: data)
     }
-    return true
   }
 
   /// helper for sending queued messages
@@ -275,6 +273,7 @@ public class Call {
         }
       } else {
         // TODO: if the event failed, shut down
+        self.writing = false
       }
     })
   }