Browse Source

Get rid of the `callbackQueue` argument for `CompletionQueue.runToCompletion`, as we only use it on one occasion, and on that occasion we already dispatch the completion block on that queue anyway.

Daniel Alm 7 years ago
parent
commit
cf323b474c

+ 4 - 10
Sources/SwiftGRPC/Core/CompletionQueue.swift

@@ -107,14 +107,12 @@ class CompletionQueue {
 
 
   /// Runs a completion queue and call a completion handler when finished
   /// Runs a completion queue and call a completion handler when finished
   ///
   ///
-  /// - Parameter callbackQueue: a DispatchQueue to use to call the completion handler
   /// - Parameter completion: a completion handler that is called when the queue stops running
   /// - Parameter completion: a completion handler that is called when the queue stops running
-  func runToCompletion(callbackQueue: DispatchQueue = DispatchQueue.main,
-                       completion: (() -> Void)?) {
+  func runToCompletion(completion: (() -> Void)?) {
     // run the completion queue on a new background thread
     // run the completion queue on a new background thread
     DispatchQueue.global().async {
     DispatchQueue.global().async {
       spinloop: while true {
       spinloop: while true {
-        let event = cgrpc_completion_queue_get_next_event(self.underlyingCompletionQueue, -1.0)
+        let event = cgrpc_completion_queue_get_next_event(self.underlyingCompletionQueue, 600)
         switch event.type {
         switch event.type {
         case GRPC_OP_COMPLETE:
         case GRPC_OP_COMPLETE:
           let tag = cgrpc_event_tag(event)
           let tag = cgrpc_event_tag(event)
@@ -150,12 +148,8 @@ class CompletionQueue {
           break spinloop
           break spinloop
         }
         }
       }
       }
-      if let completion = completion {
-        callbackQueue.async {
-          // when the queue stops running, call the queue completion handler
-          completion()
-        }
-      }
+      // when the queue stops running, call the queue completion handler
+      completion?()
     }
     }
   }
   }
 
 

+ 1 - 1
Sources/SwiftGRPC/Core/Server.swift

@@ -90,7 +90,7 @@ public class Server {
                   self.handlers.insert(handler)
                   self.handlers.insert(handler)
                 }
                 }
                 // this will start the completion queue on a new thread
                 // this will start the completion queue on a new thread
-                handler.completionQueue.runToCompletion(callbackQueue: dispatchQueue) {
+                handler.completionQueue.runToCompletion {
                   dispatchQueue.async {
                   dispatchQueue.async {
                     self.handlersMutex.synchronize {
                     self.handlersMutex.synchronize {
                       // release the handler when it finishes
                       // release the handler when it finishes