punchnet-macos/Tun/Punchnet/SDLAddressResolver.swift
2026-02-03 23:45:03 +08:00

43 lines
943 B
Swift

//
// SDLAddressResolverPool.swift
// Tun
//
// Created by on 2026/2/3.
//
import Foundation
import NIOCore
import NIOPosix
actor SDLAddressResolver {
static let shared = SDLAddressResolver(threads: System.coreCount)
private let pool: NIOThreadPool
private var cache: [String: SocketAddress] = [:]
private init(threads: Int = 2) {
self.pool = NIOThreadPool(numberOfThreads: threads)
self.pool.start()
}
func resolve(host: String, port: Int) async throws -> SocketAddress {
let key = "\(host):\(port)"
if let cached = cache[key] {
return cached
}
let address = try await pool.runIfActive {
try SocketAddress.makeAddressResolvingHost(host, port: port)
}
cache[key] = address
return address
}
deinit {
pool.shutdownGracefully { _ in }
}
}