ConnectionPoolIDs.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2024, 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 Atomics
  17. enum RawID {
  18. private static let source = ManagedAtomic(0)
  19. static func next() -> Int {
  20. self.source.loadThenWrappingIncrement(ordering: .relaxed)
  21. }
  22. }
  23. /// The ID of a connection pool.
  24. public struct GRPCConnectionPoolID: Hashable, Sendable, CustomStringConvertible {
  25. private var rawValue: Int
  26. private init(rawValue: Int) {
  27. self.rawValue = rawValue
  28. }
  29. public static func next() -> Self {
  30. return Self(rawValue: RawID.next())
  31. }
  32. public var description: String {
  33. "ConnectionPool(\(self.rawValue))"
  34. }
  35. }
  36. /// The ID of a sub-pool in a connection pool.
  37. public struct GRPCSubPoolID: Hashable, Sendable, CustomStringConvertible {
  38. private var rawValue: Int
  39. private init(rawValue: Int) {
  40. self.rawValue = rawValue
  41. }
  42. public static func next() -> Self {
  43. return Self(rawValue: RawID.next())
  44. }
  45. public var description: String {
  46. "SubPool(\(self.rawValue))"
  47. }
  48. }