GRPCChannelHandler.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Foundation
  2. import SwiftProtobuf
  3. import NIO
  4. import NIOHTTP1
  5. /// Processes individual gRPC messages and stream-close events on a HTTP2 channel.
  6. public protocol GRPCCallHandler: ChannelHandler {
  7. func makeGRPCServerCodec() -> ChannelHandler
  8. }
  9. /// Provides `GRPCCallHandler` objects for the methods on a particular service name.
  10. ///
  11. /// Implemented by the generated code.
  12. public protocol CallHandlerProvider: class {
  13. /// The name of the service this object is providing methods for, including the package path.
  14. ///
  15. /// - Example: "io.grpc.Echo.EchoService"
  16. var serviceName: String { get }
  17. /// Determines, calls and returns the appropriate request handler (`GRPCCallHandler`), depending on the request's
  18. /// method. Returns nil for methods not handled by this service.
  19. func handleMethod(_ methodName: String, request: HTTPRequestHead, serverHandler: GRPCChannelHandler, channel: Channel) -> GRPCCallHandler?
  20. }
  21. /// Listens on a newly-opened HTTP2 subchannel and yields to the sub-handler matching a call, if available.
  22. ///
  23. /// Once the request headers are available, asks the `CallHandlerProvider` corresponding to the request's service name
  24. /// for an `GRPCCallHandler` object. That object is then forwarded the individual gRPC messages.
  25. public final class GRPCChannelHandler {
  26. private let servicesByName: [String: CallHandlerProvider]
  27. public init(servicesByName: [String: CallHandlerProvider]) {
  28. self.servicesByName = servicesByName
  29. }
  30. }
  31. extension GRPCChannelHandler: ChannelInboundHandler {
  32. public typealias InboundIn = RawGRPCServerRequestPart
  33. public typealias OutboundOut = RawGRPCServerResponsePart
  34. public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
  35. let requestPart = self.unwrapInboundIn(data)
  36. switch requestPart {
  37. case .head(let requestHead):
  38. // URI format: "/package.Servicename/MethodName", resulting in the following components separated by a slash:
  39. // - uriComponents[0]: empty
  40. // - uriComponents[1]: service name (including the package name);
  41. // `CallHandlerProvider`s should provide the service name including the package name.
  42. // - uriComponents[2]: method name.
  43. let uriComponents = requestHead.uri.components(separatedBy: "/")
  44. guard uriComponents.count >= 3 && uriComponents[0].isEmpty,
  45. let providerForServiceName = servicesByName[uriComponents[1]],
  46. let callHandler = providerForServiceName.handleMethod(uriComponents[2], request: requestHead, serverHandler: self, channel: ctx.channel) else {
  47. ctx.writeAndFlush(self.wrapOutboundOut(.status(.unimplemented(method: requestHead.uri))), promise: nil)
  48. return
  49. }
  50. let codec = callHandler.makeGRPCServerCodec()
  51. let handlerRemoved: EventLoopPromise<Bool> = ctx.eventLoop.newPromise()
  52. handlerRemoved.futureResult.whenSuccess { handlerWasRemoved in
  53. assert(handlerWasRemoved)
  54. ctx.pipeline.add(handler: callHandler, after: codec).whenComplete {
  55. // Send the .headers event back to begin the headers flushing for the response.
  56. // At this point, which headers should be returned is not known, as the content type is
  57. // processed in HTTP1ToRawGRPCServerCodec. At the same time the HTTP1ToRawGRPCServerCodec
  58. // handler doesn't have the data to determine whether headers should be returned, as it is
  59. // this handler that checks whether the stub for the requested Service/Method is implemented.
  60. // This likely signals that the architecture for these handlers could be improved.
  61. ctx.write(self.wrapOutboundOut(.headers(HTTPHeaders())), promise: nil)
  62. }
  63. }
  64. ctx.pipeline.add(handler: codec, after: self)
  65. .whenComplete { ctx.pipeline.remove(handler: self, promise: handlerRemoved) }
  66. case .message, .end:
  67. preconditionFailure("received \(requestPart), should have been removed as a handler at this point")
  68. }
  69. }
  70. }