Browse Source

Make `Server` retain its active handlers without having to resort to a `handlers` set.

Daniel Alm 7 years ago
parent
commit
f6d4341682
2 changed files with 10 additions and 23 deletions
  1. 0 8
      Sources/SwiftGRPC/Core/Handler.swift
  2. 10 15
      Sources/SwiftGRPC/Core/Server.swift

+ 0 - 8
Sources/SwiftGRPC/Core/Handler.swift

@@ -172,11 +172,3 @@ public class Handler {
                    completion: completion)
   }
 }
-
-extension Handler: Hashable {
-  public var hashValue: Int { return underlyingHandler.hashValue }
-  
-  public static func ==(A: Handler, B: Handler) -> Bool {
-    return A === B
-  }
-}

+ 10 - 15
Sources/SwiftGRPC/Core/Server.swift

@@ -33,12 +33,6 @@ public class Server {
   /// Completion queue used for server operations
   let completionQueue: CompletionQueue
 
-  /// Active handlers
-  private var handlers = Set<Handler>()
-
-  /// Mutex for synchronizing access to handlers
-  private let handlersMutex: Mutex = Mutex()
-
   /// Optional callback when server stops serving
   public var onCompletion: (() -> Void)?
 
@@ -86,20 +80,21 @@ public class Server {
               // run the handler and remove it when it finishes
               if event.success != 0 {
                 // hold onto the handler while it runs
-                self.handlersMutex.synchronize {
-                  self.handlers.insert(handler)
-                }
+                var strongHandlerReference: Handler?
+                strongHandlerReference = handler
+                // To prevent the "Variable 'strongHandlerReference' was written to, but never read" warning.
+                _ = strongHandlerReference
                 // this will start the completion queue on a new thread
                 handler.completionQueue.runToCompletion {
                   dispatchQueue.async {
-                    self.handlersMutex.synchronize {
-                      // release the handler when it finishes
-                      self.handlers.remove(handler)
-                    }
+                    // release the handler when it finishes
+                    strongHandlerReference = nil
                   }
                 }
-                // call the handler function on the server thread
-                handlerFunction(handler)
+                dispatchQueue.async {
+                  // dispatch the handler function on a separate thread
+                  handlerFunction(handler)
+                }
               }
             } else if event.tag == Server.stopTag || event.tag == Server.destroyTag {
               running = false // exit the loop