ClientOptions.swift 4.2 KB

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