From 6f5cfedfd56caddc1f1e99b9341c771d03f15b11 Mon Sep 17 00:00:00 2001 From: Muxi Yan <mxyan@google.com> Date: Tue, 25 Oct 2016 15:04:41 -0700 Subject: [PATCH] Add comments for GRPCHost --- src/objective-c/GRPCClient/private/GRPCHost.m | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 4737e45168..c41cb81ee1 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -54,6 +54,10 @@ NS_ASSUME_NONNULL_BEGIN #define GRPC_OBJC_VERSION_STRING @"1.0.0" static NSMutableDictionary *kHostCache; + +// This connectivity monitor flushes the host cache when connectivity status +// changes. Otherwise, a new call might still use the cached channel which is +// no longer available and will call gRPC to hang static GRPCConnectivityMonitor *connectivityMonitor = nil; @implementation GRPCHost { @@ -94,6 +98,7 @@ static GRPCConnectivityMonitor *connectivityMonitor = nil; dispatch_once(&cacheInitialization, ^{ kHostCache = [NSMutableDictionary dictionary]; }); + @synchronized(kHostCache) { GRPCHost *cachedHost = kHostCache[address]; if (cachedHost) { @@ -106,6 +111,7 @@ static GRPCConnectivityMonitor *connectivityMonitor = nil; kHostCache[address] = self; } // Keep a single monitor to flush the cache if the connectivity status changed + // Thread safety guarded by @synchronized(kHostCache) if (!connectivityMonitor) { connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:hostURL.host]; -- GitLab