ClientOptions.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /// Options to use for GRPC calls.
  19. public struct CallOptions {
  20. /// Additional metadata to send to the service.
  21. public var customMetadata: HTTPHeaders
  22. /// The call timeout.
  23. public var timeout: GRPCTimeout
  24. /// Whether the call is cacheable.
  25. public var cacheable: Bool
  26. /// How IDs should be provided for requests. Defaults to `.autogenerated`.
  27. ///
  28. /// The request ID is used for logging and will be added to the headers of a call if
  29. /// `requestIDHeader` is specified.
  30. ///
  31. /// - Important: When setting `CallOptions` at the client level, `.userDefined` should __not__ be
  32. /// used otherwise each request will have the same ID.
  33. public var requestIDProvider: RequestIDProvider
  34. /// The name of the header to use when adding a request ID to a call, e.g. "x-request-id". If the
  35. /// value is `nil` (the default) then no additional header will be addded.
  36. ///
  37. /// Setting this value will add a request ID to the headers of the call these options are used
  38. /// with. The request ID will be provided by `requestIDProvider` and will also be used in log
  39. /// messages associated with the call.
  40. public var requestIDHeader: String?
  41. public init(
  42. customMetadata: HTTPHeaders = HTTPHeaders(),
  43. timeout: GRPCTimeout = GRPCTimeout.infinite,
  44. cacheable: Bool = false,
  45. requestIDProvider: RequestIDProvider = .autogenerated,
  46. requestIDHeader: String? = nil
  47. ) {
  48. self.customMetadata = customMetadata
  49. self.timeout = timeout
  50. self.cacheable = false
  51. self.requestIDProvider = requestIDProvider
  52. self.requestIDHeader = requestIDHeader
  53. }
  54. /// How Request IDs should be provided.
  55. public enum RequestIDProvider {
  56. /// Generate a new ID automatically.
  57. case autogenerated
  58. /// Specify an ID to be used.
  59. ///
  60. /// - Important: this should only be used when `CallOptions` are passed directly to the call.
  61. /// If it is used for the default options on a client then all calls with have the same ID.
  62. case userDefined(String)
  63. func requestID() -> String {
  64. switch self {
  65. case .autogenerated:
  66. return UUID().uuidString
  67. case .userDefined(let id):
  68. return id
  69. }
  70. }
  71. }
  72. }