ProcessUniqueID.swift 1.6 KB

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