Skip to content

Commit fcd1d32

Browse files
authored
update swift-nio atomic use to new API (#9)
motivation: swift-nio is deprecating its AtomicBox and replaced the recommnded Atomic implementation changes: * use NIOAtomic instead of Atomic which is deprecated * do not attemp to cache the connection
1 parent 1f21012 commit fcd1d32

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

Sources/StatsdClient/StatsdClient.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public final class StatsdClient: MetricsFactory {
124124
private final class StatsdCounter: CounterHandler, Equatable {
125125
let id: String
126126
let client: Client
127-
var value = Atomic<Int64>(value: 0)
127+
var value = NIOAtomic<Int64>.makeAtomic(value: 0)
128128

129129
init(label: String, dimensions: [(String, String)], client: Client) {
130130
self.id = StatsdUtils.id(label: label, dimensions: dimensions)
@@ -240,9 +240,10 @@ private final class StatsdTimer: TimerHandler, Equatable {
240240
private final class Client {
241241
private let eventLoopGroupProvider: StatsdClient.EventLoopGroupProvider
242242
private let eventLoopGroup: EventLoopGroup
243-
private let isShutdown = Atomic<Bool>(value: false)
243+
244244
private let address: SocketAddress
245-
private var channel: AtomicBox<Box<Channel?>> = AtomicBox(value: Box(nil))
245+
246+
private let isShutdown = NIOAtomic<Bool>.makeAtomic(value: false)
246247

247248
init(eventLoopGroupProvider: StatsdClient.EventLoopGroupProvider, address: SocketAddress) {
248249
self.eventLoopGroupProvider = eventLoopGroupProvider
@@ -256,7 +257,7 @@ private final class Client {
256257
}
257258

258259
deinit {
259-
assert(self.isShutdown.load(), "client not stopped before the deinit.")
260+
precondition(self.isShutdown.load(), "client not stopped before the deinit.")
260261
}
261262

262263
func shutdown(_ callback: @escaping (Error?) -> Void) {
@@ -265,7 +266,7 @@ private final class Client {
265266
if self.isShutdown.compareAndExchange(expected: false, desired: true) {
266267
self.eventLoopGroup.shutdownGracefully(callback)
267268
}
268-
default:
269+
case .shared:
269270
self.isShutdown.store(true)
270271
callback(nil)
271272
}
@@ -278,20 +279,11 @@ private final class Client {
278279
}
279280

280281
private func connect() -> EventLoopFuture<Channel> {
281-
if let channel = self.channel.load().value {
282-
return self.eventLoopGroup.next().makeSucceededFuture(channel)
283-
}
284-
285282
let bootstrap = DatagramBootstrap(group: self.eventLoopGroup)
286283
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
287284
.channelInitializer { channel in channel.pipeline.addHandler(Encoder(address: self.address)) }
288-
289285
// the bind address is local and does not really matter, the remote address is addressed by AddressedEnvelope below
290-
let future = bootstrap.bind(host: "0.0.0.0", port: 0)
291-
future.whenSuccess { channel in
292-
self.channel.store(Box(channel))
293-
}
294-
return future
286+
return bootstrap.bind(host: "0.0.0.0", port: 0)
295287
}
296288

297289
private final class Encoder: ChannelOutboundHandler {

0 commit comments

Comments
 (0)