GRPCCall+Interceptor.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import "GRPCCall+Interceptor.h"
  19. #import "GRPCInterceptor.h"
  20. static id<GRPCInterceptorFactory> globalInterceptorFactory = nil;
  21. static NSLock *globalInterceptorLock = nil;
  22. static dispatch_once_t onceToken;
  23. @implementation GRPCCall2 (Interceptor)
  24. + (void)registerGlobalInterceptor:(id<GRPCInterceptorFactory>)interceptorFactory {
  25. if (interceptorFactory == nil) {
  26. return;
  27. }
  28. dispatch_once(&onceToken, ^{
  29. globalInterceptorLock = [[NSLock alloc] init];
  30. });
  31. [globalInterceptorLock lock];
  32. if (globalInterceptorFactory != nil) {
  33. [globalInterceptorLock unlock];
  34. [NSException
  35. raise:NSInternalInconsistencyException
  36. format:@"Global interceptor is already registered. Only one global interceptor can be "
  37. @"registered in a process."];
  38. return;
  39. }
  40. globalInterceptorFactory = interceptorFactory;
  41. [globalInterceptorLock unlock];
  42. }
  43. + (id<GRPCInterceptorFactory>)globalInterceptorFactory {
  44. dispatch_once(&onceToken, ^{
  45. globalInterceptorLock = [[NSLock alloc] init];
  46. });
  47. id<GRPCInterceptorFactory> factory;
  48. [globalInterceptorLock lock];
  49. factory = globalInterceptorFactory;
  50. [globalInterceptorLock unlock];
  51. return factory;
  52. }
  53. @end