/src/grpc-swift/Sources/GRPC/Interceptor/ServerInterceptors.swift
Line | Count | Source (jump to first uncovered line) |
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 | | |
18 | | /// A base class for server interceptors. |
19 | | /// |
20 | | /// Interceptors allow request and response and response parts to be observed, mutated or dropped |
21 | | /// as necessary. The default behaviour for this base class is to forward any events to the next |
22 | | /// interceptor. |
23 | | /// |
24 | | /// Interceptors may observe two different types of event: |
25 | | /// - receiving request parts with ``receive(_:context:)``, |
26 | | /// - sending response parts with ``send(_:promise:context:)``. |
27 | | /// |
28 | | /// These events flow through a pipeline of interceptors for each RPC. Request parts will enter |
29 | | /// the head of the interceptor pipeline once the request router has determined that there is a |
30 | | /// service provider which is able to handle the request stream. Response parts from the service |
31 | | /// provider enter the tail of the interceptor pipeline and will be sent to the client after |
32 | | /// traversing the pipeline through to the head. |
33 | | /// |
34 | | /// Each of the interceptor functions is provided with a `context` which exposes analogous functions |
35 | | /// (``receive(_:context:)`` and ``send(_:promise:context:)``) which may be called to forward events to the next |
36 | | /// interceptor. |
37 | | /// |
38 | | /// ### Thread Safety |
39 | | /// |
40 | | /// Functions on `context` are not thread safe and **must** be called on the `EventLoop` found on |
41 | | /// the `context`. Since each interceptor is invoked on the same `EventLoop` this does not usually |
42 | | /// require any extra attention. However, if work is done on a `DispatchQueue` or _other_ |
43 | | /// `EventLoop` then implementers should ensure that they use `context` from the correct |
44 | | /// `EventLoop`. |
45 | | open class ServerInterceptor<Request, Response>: @unchecked Sendable { |
46 | 0 | public init() {} |
47 | | |
48 | | /// Called when the interceptor has received a request part to handle. |
49 | | /// - Parameters: |
50 | | /// - part: The request part which has been received from the client. |
51 | | /// - context: An interceptor context which may be used to forward the response part. |
52 | | open func receive( |
53 | | _ part: GRPCServerRequestPart<Request>, |
54 | | context: ServerInterceptorContext<Request, Response> |
55 | 0 | ) { |
56 | 0 | context.receive(part) |
57 | 0 | } |
58 | | |
59 | | /// Called when the interceptor has received a response part to handle. |
60 | | /// - Parameters: |
61 | | /// - part: The request part which should be sent to the client. |
62 | | /// - promise: A promise which should be completed when the response part has been written. |
63 | | /// - context: An interceptor context which may be used to forward the request part. |
64 | | open func send( |
65 | | _ part: GRPCServerResponsePart<Response>, |
66 | | promise: EventLoopPromise<Void>?, |
67 | | context: ServerInterceptorContext<Request, Response> |
68 | 0 | ) { |
69 | 0 | context.send(part, promise: promise) |
70 | 0 | } |
71 | | } |