in Sources/StatsdClient/StatsdClient.swift [297:330]
func emit(_ metric: Metric) -> EventLoopFuture<Void> {
self.lock.lock()
switch self.state {
case .disconnected:
let promise = self.eventLoopGroup.next().makePromise(of: Void.self)
self.state = .connecting(promise.futureResult)
self.lock.unlock()
self.connect().flatMap { channel -> EventLoopFuture<Void> in
self.lock.withLock {
guard case .connecting = self.state else {
preconditionFailure("invalid state \(self.state)")
}
self.state = .connected(channel)
}
return self.emit(metric)
}.cascade(to: promise)
return promise.futureResult
case .connecting(let future):
let future = future.flatMap {
self.emit(metric)
}
self.state = .connecting(future)
self.lock.unlock()
return future
case .connected(let channel):
guard channel.isActive else {
self.state = .disconnected
self.lock.unlock()
return self.emit(metric)
}
self.lock.unlock()
return channel.writeAndFlush(metric)
}
}