DelegatingOnCloseEchoProvider.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2021, 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 EchoModel
  17. import GRPC
  18. import NIOCore
  19. /// An `Echo_EchoProvider` which sets `onClose` for each RPC and then calls a delegate to provide
  20. /// the RPC implementation.
  21. class OnCloseEchoProvider: Echo_EchoProvider {
  22. let interceptors: Echo_EchoServerInterceptorFactoryProtocol?
  23. let onClose: (Result<Void, Error>) -> Void
  24. let delegate: Echo_EchoProvider
  25. init(
  26. delegate: Echo_EchoProvider,
  27. interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil,
  28. onClose: @escaping (Result<Void, Error>) -> Void
  29. ) {
  30. self.delegate = delegate
  31. self.onClose = onClose
  32. self.interceptors = interceptors
  33. }
  34. func get(
  35. request: Echo_EchoRequest,
  36. context: StatusOnlyCallContext
  37. ) -> EventLoopFuture<Echo_EchoResponse> {
  38. context.closeFuture.whenComplete(self.onClose)
  39. return self.delegate.get(request: request, context: context)
  40. }
  41. func expand(
  42. request: Echo_EchoRequest,
  43. context: StreamingResponseCallContext<Echo_EchoResponse>
  44. ) -> EventLoopFuture<GRPCStatus> {
  45. context.closeFuture.whenComplete(self.onClose)
  46. return self.delegate.expand(request: request, context: context)
  47. }
  48. func collect(
  49. context: UnaryResponseCallContext<Echo_EchoResponse>
  50. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  51. context.closeFuture.whenComplete(self.onClose)
  52. return self.delegate.collect(context: context)
  53. }
  54. func update(
  55. context: StreamingResponseCallContext<Echo_EchoResponse>
  56. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  57. context.closeFuture.whenComplete(self.onClose)
  58. return self.delegate.update(context: context)
  59. }
  60. }