EmbeddedGRPCChannel.swift 3.0 KB

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