Coverage Report

Created: 2026-07-30 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/grpc-swift/Sources/GRPC/Interceptor/ServerInterceptorContext.swift
Line
Count
Source
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 Logging
17
import NIOCore
18
19
public struct ServerInterceptorContext<Request, Response> {
20
  /// The interceptor this context is for.
21
  @usableFromInline
22
  internal let interceptor: ServerInterceptor<Request, Response>
23
24
  /// The pipeline this context is associated with.
25
  @usableFromInline
26
  internal let _pipeline: ServerInterceptorPipeline<Request, Response>
27
28
  /// The index of this context's interceptor within the pipeline.
29
  @usableFromInline
30
  internal let _index: Int
31
32
  /// The `EventLoop` this interceptor pipeline is being executed on.
33
0
  public var eventLoop: EventLoop {
34
0
    return self._pipeline.eventLoop
35
0
  }
36
37
  /// A logger.
38
0
  public var logger: Logger {
39
0
    return self._pipeline.logger
40
0
  }
41
42
  /// The type of the RPC, e.g. "unary".
43
0
  public var type: GRPCCallType {
44
0
    return self._pipeline.type
45
0
  }
46
47
  /// The path of the RPC in the format "/Service/Method", e.g. "/echo.Echo/Get".
48
0
  public var path: String {
49
0
    return self._pipeline.path
50
0
  }
51
52
  /// The address of the remote peer.
53
0
  public var remoteAddress: SocketAddress? {
54
0
    return self._pipeline.remoteAddress
55
0
  }
56
57
  /// A future which completes when the call closes. This may be used to register callbacks which
58
  /// free up resources used by the interceptor.
59
0
  public var closeFuture: EventLoopFuture<Void> {
60
0
    return self._pipeline.closeFuture
61
0
  }
62
63
  /// A 'UserInfo' dictionary.
64
  ///
65
  /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
66
  ///   reference wrapped `UserInfo`. The contexts passed to the service provider share the same
67
  ///   reference. As such this may be used as a mechanism to pass information between interceptors
68
  ///   and service providers.
69
  /// - Important: `userInfo` *must* be accessed from the context's `eventLoop` in order to ensure
70
  ///   thread-safety.
71
  public var userInfo: UserInfo {
72
0
    get {
73
0
      return self._pipeline.userInfoRef.value
74
0
    }
75
0
    nonmutating set {
76
0
      self._pipeline.userInfoRef.value = newValue
77
0
    }
78
  }
79
80
  /// Construct a `ServerInterceptorContext` for the interceptor at the given index within the
81
  /// interceptor pipeline.
82
  @inlinable
83
  internal init(
84
    for interceptor: ServerInterceptor<Request, Response>,
85
    atIndex index: Int,
86
    in pipeline: ServerInterceptorPipeline<Request, Response>
87
0
  ) {
88
0
    self.interceptor = interceptor
89
0
    self._pipeline = pipeline
90
0
    self._index = index
91
0
  }
92
93
  /// Forwards the request part to the next inbound interceptor in the pipeline, if there is one.
94
  ///
95
  /// - Parameter part: The request part to forward.
96
  /// - Important: This *must* to be called from the `eventLoop`.
97
  @inlinable
98
0
  public func receive(_ part: GRPCServerRequestPart<Request>) {
99
0
    self._pipeline.invokeReceive(part, fromContextAtIndex: self._index)
100
0
  }
101
102
  /// Forwards the response part to the next outbound interceptor in the pipeline, if there is one.
103
  ///
104
  /// - Parameters:
105
  ///   - part: The response part to forward.
106
  ///   - promise: The promise the complete when the part has been written.
107
  /// - Important: This *must* to be called from the `eventLoop`.
108
  @inlinable
109
  public func send(
110
    _ part: GRPCServerResponsePart<Response>,
111
    promise: EventLoopPromise<Void>?
112
0
  ) {
113
0
    self._pipeline.invokeSend(part, promise: promise, fromContextAtIndex: self._index)
114
0
  }
115
}