ConnectionManagerChannelProvider.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 sync = channel.pipeline.syncOperations
  66. do {
  67. try sync.configureGRPCClient(
  68. channel: channel,
  69. httpTargetWindowSize: self.configuration.httpTargetWindowSize,
  70. tlsConfiguration: self.configuration.tls?.configuration,
  71. tlsServerHostname: serverHostname,
  72. connectionManager: connectionManager,
  73. connectionKeepalive: self.configuration.connectionKeepalive,
  74. connectionIdleTimeout: self.configuration.connectionIdleTimeout,
  75. errorDelegate: self.configuration.errorDelegate,
  76. requiresZeroLengthWriteWorkaround: PlatformSupport.requiresZeroLengthWriteWorkaround(
  77. group: eventLoop,
  78. hasTLS: self.configuration.tls != nil
  79. ),
  80. logger: logger,
  81. customVerificationCallback: self.configuration.tls?.customVerificationCallback
  82. )
  83. } catch {
  84. return channel.eventLoop.makeFailedFuture(error)
  85. }
  86. // Run the debug initializer, if there is one.
  87. if let debugInitializer = self.configuration.debugChannelInitializer {
  88. return debugInitializer(channel)
  89. } else {
  90. return channel.eventLoop.makeSucceededVoidFuture()
  91. }
  92. }
  93. if let connectTimeout = connectTimeout {
  94. _ = bootstrap.connectTimeout(connectTimeout)
  95. }
  96. return bootstrap.connect(to: self.configuration.target)
  97. }
  98. }