42 lines
934 B
Swift
42 lines
934 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 }
|
|
}
|
|
|
|
}
|