2
0

ClientOptions.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2019, 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 struct Foundation.UUID
  17. import NIOHTTP1
  18. import NIOHTTP2
  19. import NIOHPACK
  20. /// Options to use for GRPC calls.
  21. public struct CallOptions {
  22. /// Additional metadata to send to the service.
  23. public var customMetadata: HPACKHeaders
  24. /// The time limit for the RPC.
  25. ///
  26. /// - Note: timeouts are treated as deadlines as soon as an RPC has been invoked.
  27. public var timeLimit: TimeLimit
  28. /// The compression used for requests, and the compression algorithms to advertise as acceptable
  29. /// for the remote peer to use for encoding responses.
  30. ///
  31. /// Compression may also be disabled at the message-level for streaming requests (i.e. client
  32. /// streaming and bidirectional streaming RPCs) by setting `compression` to `.disabled` in
  33. /// `sendMessage(_:compression)`, `sendMessage(_:compression:promise)`,
  34. /// `sendMessages(_:compression)` or `sendMessages(_:compression:promise)`.
  35. ///
  36. /// Note that enabling `compression` via the `sendMessage` or `sendMessages` methods only applies
  37. /// if encoding has been specified in these options.
  38. public var messageEncoding: ClientMessageEncoding
  39. /// Whether the call is cacheable.
  40. public var cacheable: Bool
  41. /// How IDs should be provided for requests. Defaults to `.autogenerated`.
  42. ///
  43. /// The request ID is used for logging and will be added to the headers of a call if
  44. /// `requestIDHeader` is specified.
  45. ///
  46. /// - Important: When setting `CallOptions` at the client level, `.userDefined` should __not__ be
  47. /// used otherwise each request will have the same ID.
  48. public var requestIDProvider: RequestIDProvider
  49. /// The name of the header to use when adding a request ID to a call, e.g. "x-request-id". If the
  50. /// value is `nil` (the default) then no additional header will be added.
  51. ///
  52. /// Setting this value will add a request ID to the headers of the call these options are used
  53. /// with. The request ID will be provided by `requestIDProvider` and will also be used in log
  54. /// messages associated with the call.
  55. public var requestIDHeader: String?
  56. public init(
  57. customMetadata: HPACKHeaders = HPACKHeaders(),
  58. timeLimit: TimeLimit = .none,
  59. messageEncoding: ClientMessageEncoding = .disabled,
  60. requestIDProvider: RequestIDProvider = .autogenerated,
  61. requestIDHeader: String? = nil,
  62. cacheable: Bool = false
  63. ) {
  64. self.customMetadata = customMetadata
  65. self.messageEncoding = messageEncoding
  66. self.requestIDProvider = requestIDProvider
  67. self.requestIDHeader = requestIDHeader
  68. self.cacheable = false
  69. self.timeLimit = timeLimit
  70. }
  71. public struct RequestIDProvider {
  72. private enum RequestIDSource {
  73. case `static`(String)
  74. case generated(() -> String)
  75. }
  76. private var source: RequestIDSource
  77. private init(_ source: RequestIDSource) {
  78. self.source = source
  79. }
  80. internal func requestID() -> String {
  81. switch self.source {
  82. case .static(let requestID):
  83. return requestID
  84. case .generated(let generator):
  85. return generator()
  86. }
  87. }
  88. /// Generate a new request ID for each RPC.
  89. public static let autogenerated = RequestIDProvider(.generated({ UUID().uuidString }))
  90. /// Specify an ID to be used.
  91. ///
  92. /// - Important: this should only be used when `CallOptions` are passed directly to the call.
  93. /// If it is used for the default options on a client then all calls with have the same ID.
  94. public static func userDefined(_ requestID: String) -> RequestIDProvider {
  95. return RequestIDProvider(.static(requestID))
  96. }
  97. /// Provide a factory to generate request IDs.
  98. public static func generated(_ requestIDFactory: @escaping () -> String) -> RequestIDProvider {
  99. return RequestIDProvider(.generated(requestIDFactory))
  100. }
  101. }
  102. }