ClientOptions.swift 2.8 KB

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