EmbeddedGRPCChannel.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. errorDelegate: self.errorDelegate
  68. )
  69. )
  70. }
  71. internal func makeCall<Request: GRPCPayload, Response: GRPCPayload>(
  72. path: String,
  73. type: GRPCCallType,
  74. callOptions: CallOptions,
  75. interceptors: [ClientInterceptor<Request, Response>]
  76. ) -> Call<Request, Response> {
  77. return Call(
  78. path: path,
  79. type: type,
  80. eventLoop: self.eventLoop,
  81. options: callOptions,
  82. interceptors: interceptors,
  83. transportFactory: .http2(
  84. multiplexer: self.multiplexer,
  85. authority: self.authority,
  86. scheme: self.scheme,
  87. errorDelegate: self.errorDelegate
  88. )
  89. )
  90. }
  91. }