| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- /*
- * Copyright 2019, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import Foundation
- import NIOHTTP1
- /// Options to use for GRPC calls.
- public struct CallOptions {
- /// Additional metadata to send to the service.
- public var customMetadata: HTTPHeaders
- /// The call timeout.
- public var timeout: GRPCTimeout
- /// Whether the call is cacheable.
- public var cacheable: Bool
- /// How IDs should be provided for requests. Defaults to `.autogenerated`.
- ///
- /// The request ID is used for logging and will be added to the headers of a call if
- /// `requestIDHeader` is specified.
- ///
- /// - Important: When setting `CallOptions` at the client level, `.userDefined` should __not__ be
- /// used otherwise each request will have the same ID.
- public var requestIDProvider: RequestIDProvider
- /// The name of the header to use when adding a request ID to a call, e.g. "x-request-id". If the
- /// value is `nil` (the default) then no additional header will be addded.
- ///
- /// Setting this value will add a request ID to the headers of the call these options are used
- /// with. The request ID will be provided by `requestIDProvider` and will also be used in log
- /// messages associated with the call.
- public var requestIDHeader: String?
- public init(
- customMetadata: HTTPHeaders = HTTPHeaders(),
- timeout: GRPCTimeout = GRPCTimeout.infinite,
- cacheable: Bool = false,
- requestIDProvider: RequestIDProvider = .autogenerated,
- requestIDHeader: String? = nil
- ) {
- self.customMetadata = customMetadata
- self.timeout = timeout
- self.cacheable = false
- self.requestIDProvider = requestIDProvider
- self.requestIDHeader = requestIDHeader
- }
- /// How Request IDs should be provided.
- public enum RequestIDProvider {
- /// Generate a new ID automatically.
- case autogenerated
- /// Specify an ID to be used.
- ///
- /// - Important: this should only be used when `CallOptions` are passed directly to the call.
- /// If it is used for the default options on a client then all calls with have the same ID.
- case userDefined(String)
- func requestID() -> String {
- switch self {
- case .autogenerated:
- return UUID().uuidString
- case .userDefined(let id):
- return id
- }
- }
- }
- }
|