GRPCClient.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import NIO
  18. import NIOHTTP2
  19. /// Underlying channel and HTTP/2 stream multiplexer.
  20. ///
  21. /// Different service clients implementing `GRPCServiceClient` may share an instance of this class.
  22. open class GRPCClient {
  23. public static func start(
  24. host: String,
  25. port: Int,
  26. eventLoopGroup: EventLoopGroup
  27. ) -> EventLoopFuture<GRPCClient> {
  28. let bootstrap = ClientBootstrap(group: eventLoopGroup)
  29. // Enable SO_REUSEADDR.
  30. .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
  31. .channelInitializer { channel in
  32. channel.pipeline.add(handler: HTTP2Parser(mode: .client))
  33. }
  34. return bootstrap.connect(host: host, port: port).then { (channel: Channel) -> EventLoopFuture<GRPCClient> in
  35. let multiplexer = HTTP2StreamMultiplexer(inboundStreamStateInitializer: nil)
  36. return channel.pipeline.add(handler: multiplexer)
  37. .map { GRPCClient(channel: channel, multiplexer: multiplexer, host: host) }
  38. }
  39. }
  40. public let channel: Channel
  41. public let multiplexer: HTTP2StreamMultiplexer
  42. public let host: String
  43. public var defaultCallOptions: CallOptions
  44. init(channel: Channel, multiplexer: HTTP2StreamMultiplexer, host: String, defaultCallOptions: CallOptions = CallOptions()) {
  45. self.channel = channel
  46. self.multiplexer = multiplexer
  47. self.host = host
  48. self.defaultCallOptions = defaultCallOptions
  49. }
  50. /// Fired when the client shuts down.
  51. public var onClose: EventLoopFuture<Void> {
  52. return channel.closeFuture
  53. }
  54. public func close() -> EventLoopFuture<Void> {
  55. return channel.close(mode: .all)
  56. }
  57. }
  58. /// A GRPC client for a given service.
  59. public protocol GRPCServiceClient {
  60. /// The client providing the underlying HTTP/2 channel for this client.
  61. var client: GRPCClient { get }
  62. /// Name of the service this client is for (e.g. "echo.Echo").
  63. var service: String { get }
  64. /// The call options to use should the user not provide per-call options.
  65. var defaultCallOptions: CallOptions { get set }
  66. /// Return the path for the given method in the format "/Service-Name/Method-Name".
  67. ///
  68. /// This may be overriden if consumers require a different path format.
  69. ///
  70. /// - Parameter forMethod: name of method to return a path for.
  71. /// - Returns: path for the given method used in gRPC request headers.
  72. func path(forMethod method: String) -> String
  73. }
  74. extension GRPCServiceClient {
  75. public func path(forMethod method: String) -> String {
  76. return "/\(service)/\(method)"
  77. }
  78. }