ClientOptions.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /// How Request IDs should be provided.
  70. public enum RequestIDProvider {
  71. /// Generate a new ID automatically.
  72. case autogenerated
  73. /// Specify an ID to be used.
  74. ///
  75. /// - Important: this should only be used when `CallOptions` are passed directly to the call.
  76. /// If it is used for the default options on a client then all calls with have the same ID.
  77. case userDefined(String)
  78. func requestID() -> String {
  79. switch self {
  80. case .autogenerated:
  81. return UUID().uuidString
  82. case .userDefined(let id):
  83. return id
  84. }
  85. }
  86. }
  87. }