EmbeddedGRPCChannel.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2020, 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 NIOCore
  18. import NIOEmbedded
  19. import NIOHTTP2
  20. import SwiftProtobuf
  21. // This is currently intended for internal testing only.
  22. class EmbeddedGRPCChannel: GRPCChannel {
  23. let embeddedChannel: EmbeddedChannel
  24. let multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>
  25. let logger: Logger
  26. let scheme: String
  27. let authority: String
  28. let errorDelegate: ClientErrorDelegate?
  29. func close() -> EventLoopFuture<Void> {
  30. return self.embeddedChannel.close()
  31. }
  32. var eventLoop: EventLoop {
  33. return self.embeddedChannel.eventLoop
  34. }
  35. init(
  36. logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() }),
  37. errorDelegate: ClientErrorDelegate? = nil
  38. ) {
  39. let embeddedChannel = EmbeddedChannel()
  40. self.embeddedChannel = embeddedChannel
  41. self.logger = logger
  42. self.multiplexer = embeddedChannel.configureGRPCClient(
  43. errorDelegate: errorDelegate,
  44. logger: logger
  45. ).flatMap {
  46. embeddedChannel.pipeline.handler(type: HTTP2StreamMultiplexer.self)
  47. }
  48. self.scheme = "http"
  49. self.authority = "localhost"
  50. self.errorDelegate = errorDelegate
  51. }
  52. internal func makeCall<Request: Message, Response: Message>(
  53. path: String,
  54. type: GRPCCallType,
  55. callOptions: CallOptions,
  56. interceptors: [ClientInterceptor<Request, Response>]
  57. ) -> Call<Request, Response> {
  58. return Call(
  59. path: path,
  60. type: type,
  61. eventLoop: self.eventLoop,
  62. options: callOptions,
  63. interceptors: interceptors,
  64. transportFactory: .http2(
  65. channel: self.makeStreamChannel(),
  66. authority: self.authority,
  67. scheme: self.scheme,
  68. // This is internal and only for testing, so max is fine here.
  69. maximumReceiveMessageLength: .max,
  70. errorDelegate: self.errorDelegate
  71. )
  72. )
  73. }
  74. internal func makeCall<Request: GRPCPayload, Response: GRPCPayload>(
  75. path: String,
  76. type: GRPCCallType,
  77. callOptions: CallOptions,
  78. interceptors: [ClientInterceptor<Request, Response>]
  79. ) -> Call<Request, Response> {
  80. return Call(
  81. path: path,
  82. type: type,
  83. eventLoop: self.eventLoop,
  84. options: callOptions,
  85. interceptors: interceptors,
  86. transportFactory: .http2(
  87. channel: self.makeStreamChannel(),
  88. authority: self.authority,
  89. scheme: self.scheme,
  90. // This is internal and only for testing, so max is fine here.
  91. maximumReceiveMessageLength: .max,
  92. errorDelegate: self.errorDelegate
  93. )
  94. )
  95. }
  96. private func makeStreamChannel() -> EventLoopFuture<Channel> {
  97. let promise = self.eventLoop.makePromise(of: Channel.self)
  98. self.multiplexer.whenSuccess {
  99. $0.createStreamChannel(promise: promise) {
  100. $0.eventLoop.makeSucceededVoidFuture()
  101. }
  102. }
  103. return promise.futureResult
  104. }
  105. }