ProcessUniqueID.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. private import Synchronization
  17. /// An ID which is unique within this process.
  18. struct ProcessUniqueID: Hashable, Sendable, CustomStringConvertible {
  19. private static let source = Atomic(UInt64(0))
  20. private let rawValue: UInt64
  21. init() {
  22. let (_, newValue) = Self.source.add(1, ordering: .relaxed)
  23. self.rawValue = newValue
  24. }
  25. var description: String {
  26. String(describing: self.rawValue)
  27. }
  28. }
  29. /// A process-unique ID for a subchannel.
  30. package struct SubchannelID: Hashable, Sendable, CustomStringConvertible {
  31. private let id = ProcessUniqueID()
  32. package init() {}
  33. package var description: String {
  34. "subchan_\(self.id)"
  35. }
  36. }
  37. /// A process-unique ID for a load-balancer.
  38. struct LoadBalancerID: Hashable, Sendable, CustomStringConvertible {
  39. private let id = ProcessUniqueID()
  40. var description: String {
  41. "lb_\(self.id)"
  42. }
  43. }
  44. /// A process-unique ID for an entry in a queue.
  45. struct QueueEntryID: Hashable, Sendable, CustomStringConvertible {
  46. private let id = ProcessUniqueID()
  47. var description: String {
  48. "q_entry_\(self.id)"
  49. }
  50. }