ProcessUniqueID.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. internal 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. package struct SubchannelID: Hashable, Sendable, CustomStringConvertible {
  30. private let id = ProcessUniqueID()
  31. package init() {}
  32. package var description: String {
  33. "subchan_\(self.id)"
  34. }
  35. }
  36. /// A process-unique ID for a load-balancer.
  37. struct LoadBalancerID: Hashable, Sendable, CustomStringConvertible {
  38. private let id = ProcessUniqueID()
  39. var description: String {
  40. "lb_\(self.id)"
  41. }
  42. }
  43. /// A process-unique ID for an entry in a queue.
  44. struct QueueEntryID: Hashable, Sendable, CustomStringConvertible {
  45. private let id = ProcessUniqueID()
  46. var description: String {
  47. "q_entry_\(self.id)"
  48. }
  49. }