Procházet zdrojové kódy

Avoid using `NSMutableSet` in `Server`.

And while at it, make a few `var`s `let`.
Daniel Alm před 7 roky
rodič
revize
9a34c32937
2 změnil soubory, kde provedl 16 přidání a 10 odebrání
  1. 11 3
      Sources/gRPC/Handler.swift
  2. 5 7
      Sources/gRPC/Server.swift

+ 11 - 3
Sources/gRPC/Handler.swift

@@ -21,13 +21,13 @@ import Foundation // for String.Encoding
 /// A gRPC request handler
 public class Handler {
   /// Pointer to underlying C representation
-  private var underlyingHandler: UnsafeMutableRawPointer
+  fileprivate let underlyingHandler: UnsafeMutableRawPointer
 
   /// Completion queue for handler response operations
-  var completionQueue: CompletionQueue
+  let completionQueue: CompletionQueue
 
   /// Metadata received with the request
-  public var requestMetadata: Metadata
+  public let requestMetadata: Metadata
 
   /// A Call object that can be used to respond to the request
   lazy var call: Call = {
@@ -245,3 +245,11 @@ public class Handler {
     try call.perform(operations)
   }
 }
+
+extension Handler: Hashable {
+  public var hashValue: Int { return underlyingHandler.hashValue }
+  
+  public static func ==(A: Handler, B: Handler) -> Bool {
+    return A === B
+  }
+}

+ 5 - 7
Sources/gRPC/Server.swift

@@ -22,16 +22,16 @@ import Foundation
 /// gRPC Server
 public class Server {
   /// Pointer to underlying C representation
-  private var underlyingServer: UnsafeMutableRawPointer
+  private let underlyingServer: UnsafeMutableRawPointer
 
   /// Completion queue used for server operations
-  var completionQueue: CompletionQueue
+  let completionQueue: CompletionQueue
 
   /// Active handlers
-  private var handlers: NSMutableSet
+  private var handlers = Set<Handler>()
 
   /// Mutex for synchronizing access to handlers
-  private var handlersMutex: Mutex = Mutex()
+  private let handlersMutex: Mutex = Mutex()
 
   /// Optional callback when server stops serving
   private var onCompletion: (() -> Void)?
@@ -43,7 +43,6 @@ public class Server {
     underlyingServer = cgrpc_server_create(address)
     completionQueue = CompletionQueue(underlyingCompletionQueue: cgrpc_server_get_completion_queue(underlyingServer))
     completionQueue.name = "Server " + address
-    handlers = NSMutableSet()
   }
 
   /// Initializes a secure Server
@@ -55,7 +54,6 @@ public class Server {
     underlyingServer = cgrpc_server_create_secure(address, key, certs)
     completionQueue = CompletionQueue(underlyingCompletionQueue: cgrpc_server_get_completion_queue(underlyingServer))
     completionQueue.name = "Server " + address
-    handlers = NSMutableSet()
   }
 
   deinit {
@@ -81,7 +79,7 @@ public class Server {
               if event.success != 0 {
                 // hold onto the handler while it runs
                 self.handlersMutex.synchronize {
-                  self.handlers.add(handler)
+                  self.handlers.insert(handler)
                 }
                 // this will start the completion queue on a new thread
                 handler.completionQueue.runToCompletion(callbackQueue: dispatchQueue) {