ProcessUniqueID.swift 1.8 KB

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