ConnectionManagerChannelProvider.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2021, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Logging
  17. import NIO
  18. internal protocol ConnectionManagerChannelProvider {
  19. /// Make an `EventLoopFuture<Channel>`.
  20. ///
  21. /// - Parameters:
  22. /// - connectionManager: The `ConnectionManager` requesting the `Channel`.
  23. /// - eventLoop: The `EventLoop` to use for the`Channel`.
  24. /// - connectTimeout: Optional connection timeout when starting the connection.
  25. /// - logger: A logger.
  26. func makeChannel(
  27. managedBy connectionManager: ConnectionManager,
  28. onEventLoop eventLoop: EventLoop,
  29. connectTimeout: TimeAmount?,
  30. logger: Logger
  31. ) -> EventLoopFuture<Channel>
  32. }
  33. extension ClientConnection {
  34. internal struct ChannelProvider {
  35. private var configuration: Configuration
  36. internal init(configuration: Configuration) {
  37. self.configuration = configuration
  38. }
  39. }
  40. }
  41. extension ClientConnection.ChannelProvider: ConnectionManagerChannelProvider {
  42. internal func makeChannel(
  43. managedBy connectionManager: ConnectionManager,
  44. onEventLoop eventLoop: EventLoop,
  45. connectTimeout: TimeAmount?,
  46. logger: Logger
  47. ) -> EventLoopFuture<Channel> {
  48. let serverHostname: String? = self.configuration.tls.flatMap { tls -> String? in
  49. if let hostnameOverride = tls.hostnameOverride {
  50. return hostnameOverride
  51. } else {
  52. return self.configuration.target.host
  53. }
  54. }.flatMap { hostname in
  55. if hostname.isIPAddress {
  56. return nil
  57. } else {
  58. return hostname
  59. }
  60. }
  61. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop, logger: logger)
  62. .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
  63. .channelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
  64. .channelInitializer { channel in
  65. let initialized = channel.configureGRPCClient(
  66. httpTargetWindowSize: self.configuration.httpTargetWindowSize,
  67. tlsConfiguration: self.configuration.tls?.configuration,
  68. tlsServerHostname: serverHostname,
  69. connectionManager: connectionManager,
  70. connectionKeepalive: self.configuration.connectionKeepalive,
  71. connectionIdleTimeout: self.configuration.connectionIdleTimeout,
  72. errorDelegate: self.configuration.errorDelegate,
  73. requiresZeroLengthWriteWorkaround: PlatformSupport.requiresZeroLengthWriteWorkaround(
  74. group: eventLoop,
  75. hasTLS: self.configuration.tls != nil
  76. ),
  77. logger: logger,
  78. customVerificationCallback: self.configuration.tls?.customVerificationCallback
  79. )
  80. // Run the debug initializer, if there is one.
  81. if let debugInitializer = self.configuration.debugChannelInitializer {
  82. return initialized.flatMap {
  83. debugInitializer(channel)
  84. }
  85. } else {
  86. return initialized
  87. }
  88. }
  89. if let connectTimeout = connectTimeout {
  90. _ = bootstrap.connectTimeout(connectTimeout)
  91. }
  92. return bootstrap.connect(to: self.configuration.target)
  93. }
  94. }