Просмотр исходного кода

Drop support for the old way of handling RPCs on the server (#1113)

Motivation:

We said in the last release that the old method of handling RPCs would
be dropped in a future release.

Modifications:

- Drop support for 'legacyHandler' in 'HTTP2ToRawGRPCServerCodec'
- Make 'HTTP2ToRawGRPCServerCodec' an 'InboundChannelHandler'

Result:

We no longer support the old way of doing RPCs.
George Barnett 5 лет назад
Родитель
Сommit
ed647a39a9

+ 3 - 54
Sources/GRPC/HTTP2ToRawGRPCServerCodec.swift

@@ -18,12 +18,9 @@ import NIO
 import NIOHPACK
 import NIOHTTP2
 
-internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServerResponseWriter {
+internal final class HTTP2ToRawGRPCServerCodec: ChannelInboundHandler, GRPCServerResponseWriter {
   typealias InboundIn = HTTP2Frame.FramePayload
-  typealias InboundOut = GRPCServerRequestPart<ByteBuffer>
-
   typealias OutboundOut = HTTP2Frame.FramePayload
-  typealias OutboundIn = GRPCServerResponsePart<ByteBuffer>
 
   private var logger: Logger
   private var state: HTTP2ToRawGRPCStateMachine
@@ -43,7 +40,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
 
   private enum Mode {
     case notConfigured
-    case legacy
     case handler(GRPCServerHandlerProtocol)
   }
 
@@ -69,14 +65,13 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
 
   internal func handlerRemoved(context: ChannelHandlerContext) {
     self.context = nil
+    self.mode = .notConfigured
   }
 
   internal func errorCaught(context: ChannelHandlerContext, error: Error) {
     switch self.mode {
     case .notConfigured:
       context.close(mode: .all, promise: nil)
-    case .legacy:
-      context.fireErrorCaught(error)
     case let .handler(hander):
       hander.receiveError(error)
     }
@@ -84,7 +79,7 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
 
   internal func channelInactive(context: ChannelHandlerContext) {
     switch self.mode {
-    case .notConfigured, .legacy:
+    case .notConfigured:
       context.fireChannelInactive()
     case let .handler(handler):
       handler.finish()
@@ -108,12 +103,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       )
 
       switch receiveHeaders {
-      case let .configureLegacy(handler):
-        self.mode = .legacy
-        context.channel.pipeline.addHandler(handler).whenSuccess {
-          self.configured()
-        }
-
       case let .configure(handler):
         self.mode = .handler(handler)
         self.configured()
@@ -161,31 +150,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
     context.fireChannelReadComplete()
   }
 
-  internal func write(
-    context: ChannelHandlerContext,
-    data: NIOAny,
-    promise: EventLoopPromise<Void>?
-  ) {
-    let responsePart = self.unwrapOutboundIn(data)
-
-    switch responsePart {
-    case let .metadata(headers):
-      // We're in 'write' so we're using the old type of RPC handler which emits its own flushes,
-      // no need to emit an extra one.
-      self.sendMetadata(headers, flush: false, promise: promise)
-
-    case let .message(buffer, metadata):
-      self.sendMessage(buffer, metadata: metadata, promise: promise)
-
-    case let .end(status, trailers):
-      self.sendEnd(status: status, trailers: trailers, promise: promise)
-    }
-  }
-
-  internal func flush(context: ChannelHandlerContext) {
-    self.markFlushPoint()
-  }
-
   /// Called when the pipeline has finished configuring.
   private func configured() {
     switch self.state.pipelineConfigured() {
@@ -193,8 +157,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireChannelRead(self.wrapInboundOut(.metadata(headers)))
       case let .handler(handler):
         handler.receiveMetadata(headers)
       }
@@ -203,8 +165,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireChannelRead(self.wrapInboundOut(.metadata(headers)))
       case let .handler(handler):
         handler.receiveMetadata(headers)
       }
@@ -223,8 +183,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireChannelRead(self.wrapInboundOut(.message(buffer)))
       case let .handler(handler):
         handler.receiveMessage(buffer)
       }
@@ -233,9 +191,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireChannelRead(self.wrapInboundOut(.message(buffer)))
-        self.context.fireChannelRead(self.wrapInboundOut(.end))
       case let .handler(handler):
         handler.receiveMessage(buffer)
         handler.receiveEnd()
@@ -245,8 +200,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireChannelRead(self.wrapInboundOut(.message(buffer)))
       case let .handler(handler):
         handler.receiveMessage(buffer)
       }
@@ -256,8 +209,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireChannelRead(self.wrapInboundOut(.end))
       case let .handler(handler):
         handler.receiveEnd()
       }
@@ -266,8 +217,6 @@ internal final class HTTP2ToRawGRPCServerCodec: ChannelDuplexHandler, GRPCServer
       switch self.mode {
       case .notConfigured:
         preconditionFailure()
-      case .legacy:
-        self.context.fireErrorCaught(error)
       case let .handler(handler):
         handler.receiveError(error)
       }

+ 3 - 12
Sources/GRPC/HTTP2ToRawGRPCStateMachine.swift

@@ -231,8 +231,6 @@ extension HTTP2ToRawGRPCStateMachine {
   }
 
   enum ReceiveHeadersAction {
-    /// Configure the pipeline with the given call handler.
-    case configureLegacy(GRPCCallHandler)
     /// Configure the RPC to use the given server handler.
     case configure(GRPCServerHandlerProtocol)
     /// Reject the RPC by writing out the given headers and setting end-stream.
@@ -334,8 +332,8 @@ extension HTTP2ToRawGRPCStateMachine.RequestIdleResponseIdleState {
     // We have a matching service, hopefully we have a provider for the method too.
     let method = Substring(callPath.method)
 
-    func nextState() -> HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState {
-      return HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState(
+    if let handler = service.handle(method: method, context: context) {
+      let nextState = HTTP2ToRawGRPCStateMachine.RequestOpenResponseIdleState(
         reader: reader,
         writer: writer,
         contentType: contentType,
@@ -344,18 +342,11 @@ extension HTTP2ToRawGRPCStateMachine.RequestIdleResponseIdleState {
         normalizeHeaders: self.normalizeHeaders,
         configurationState: .configuring(headers)
       )
-    }
 
-    if let handler = service.handle(method: method, context: context) {
       return .init(
-        state: .requestOpenResponseIdle(nextState()),
+        state: .requestOpenResponseIdle(nextState),
         action: .configure(handler)
       )
-    } else if let handler = service.handleMethod(method, callHandlerContext: context) {
-      return .init(
-        state: .requestOpenResponseIdle(nextState()),
-        action: .configureLegacy(handler)
-      )
     } else {
       return self.methodNotImplemented(path, contentType: contentType)
     }

+ 1 - 1
Tests/GRPCTests/XCTestHelpers.swift

@@ -481,7 +481,7 @@ struct Matcher<Value> {
   static func configure() -> Matcher<HTTP2ToRawGRPCStateMachine.ReceiveHeadersAction> {
     return .init { actual in
       switch actual {
-      case .configureLegacy, .configure:
+      case .configure:
         return .match
       default:
         return .noMatch(actual: "\(actual)", expected: "configurePipeline")