Skip to content

Commit 68ac98d

Browse files
committed
atomic counter when id is not passed
1 parent 0a15156 commit 68ac98d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Sources/Memcache/MemcacheConnection.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import _ConnectionPoolModule
16+
import Atomics
1617
import NIOCore
1718
import NIOPosix
1819
import ServiceLifecycle
@@ -23,6 +24,7 @@ import ServiceLifecycle
2324
public actor MemcacheConnection: Service, PooledConnection {
2425
public typealias ID = Int
2526
public let id: ID
27+
private static var nextID: ManagedAtomic<Int> = ManagedAtomic(0)
2628

2729
private let closePromise: EventLoopPromise<Void>
2830

@@ -73,10 +75,10 @@ public actor MemcacheConnection: Service, PooledConnection {
7375
/// - port: The port number of the Memcache server.
7476
/// - eventLoopGroup: The event loop group to use for this connection.
7577
/// - id: The unique identifier for the connection (optional).
76-
public init(host: String, port: Int, id: ID = 1, eventLoopGroup: EventLoopGroup) {
78+
public init(host: String, port: Int, id: ID? = nil, eventLoopGroup: EventLoopGroup) {
7779
self.host = host
7880
self.port = port
79-
self.id = id
81+
self.id = id ?? MemcacheConnection.nextID.wrappingIncrementThenLoad(ordering: .sequentiallyConsistent)
8082
let (stream, continuation) = AsyncStream<StreamElement>.makeStream()
8183
let bufferAllocator = ByteBufferAllocator()
8284
self.closePromise = eventLoopGroup.next().makePromise(of: Void.self)

0 commit comments

Comments
 (0)