2
0

ServerChannelErrorHandler.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 NIOCore
  17. /// A handler that passes errors thrown into the server channel to the server error delegate.
  18. ///
  19. /// A NIO server bootstrap produces two kinds of channels. The first and most common is the "child" channel:
  20. /// each of these corresponds to one connection, and has the connection state stored on it. The other kind is
  21. /// the "server" channel. Each bootstrap produces only one of these, and it is the channel that owns the listening
  22. /// socket.
  23. ///
  24. /// This channel handler is inserted into the server channel, and is responsible for passing any errors in that pipeline
  25. /// to the server error delegate. If there is no error delegate, this handler is not inserted into the pipeline.
  26. final class ServerChannelErrorHandler {
  27. private let errorDelegate: ServerErrorDelegate
  28. init(errorDelegate: ServerErrorDelegate) {
  29. self.errorDelegate = errorDelegate
  30. }
  31. }
  32. extension ServerChannelErrorHandler: ChannelInboundHandler {
  33. typealias InboundIn = Any
  34. typealias InboundOut = Any
  35. func errorCaught(context: ChannelHandlerContext, error: Error) {
  36. // This handler does not treat errors as fatal to the listening socket, as it's possible they were transiently
  37. // occurring in a single connection setup attempt.
  38. self.errorDelegate.observeLibraryError(error)
  39. context.fireErrorCaught(error)
  40. }
  41. }