逻辑集中管理

This commit is contained in:
anlicheng 2026-03-06 15:06:25 +08:00
parent 1fb7364c66
commit 4e781e881c
3 changed files with 38 additions and 57 deletions

View File

@ -76,7 +76,6 @@ actor SDLContextActor {
//
private let identifyStore: IdentityStore
private let snapshotPublisher: SnapshotPublisher<IdentitySnapshot>
private let policyRequesterActor: PolicyRequesterActor
//
private var registerTask: Task<Void, Never>?
@ -97,7 +96,6 @@ actor SDLContextActor {
let snapshotPublisher = SnapshotPublisher(initial: IdentitySnapshot.empty())
self.identifyStore = IdentityStore(publisher: snapshotPublisher)
self.snapshotPublisher = snapshotPublisher
self.policyRequesterActor = PolicyRequesterActor()
}
public func start() {
@ -578,7 +576,7 @@ actor SDLContextActor {
policyRequest.srcIdentityID = data.identityID
policyRequest.dstIdentityID = self.config.identityId
await self.policyRequesterActor.submitPolicyRequest(using: self.quicClient, request: &policyRequest)
await self.identifyStore.policyRequest(using: self.quicClient, request: &policyRequest)
}
default:
SDLLogger.shared.log("[SDLContext] get invalid packet", level: .debug)

View File

@ -10,6 +10,13 @@ import NIO
actor IdentityStore {
typealias IdentityID = UInt32
//
nonisolated private let cooldown: Duration = .seconds(5)
// identityId
private var coolingDown: Set<UInt32> = []
// , map[identityId] = version
private var versions: [UInt32: UInt32] = [:]
nonisolated private let alloctor = ByteBufferAllocator()
private let publisher: SnapshotPublisher<IdentitySnapshot>
@ -19,6 +26,32 @@ actor IdentityStore {
self.publisher = publisher
}
//
func policyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) {
let identityId = request.srcIdentityID
guard let quicClient, !coolingDown.contains(identityId) else {
return
}
//
coolingDown.insert(identityId)
let version = self.versions[identityId, default: 1]
request.version = version
//
self.versions[identityId] = version + 1
//
if let queryData = try? request.serializedData() {
quicClient.send(type: .policyRequest, data: queryData)
}
Task {
//
try? await Task.sleep(for: .seconds(5))
self.endCooldown(for: identityId)
}
}
func apply(policyResponse: SDLPolicyResponse) {
let id = policyResponse.srcIdentityID
let version = policyResponse.version
@ -50,4 +83,8 @@ actor IdentityStore {
return IdentitySnapshot(identityMap: identityMap)
}
private func endCooldown(for key: UInt32) {
self.coolingDown.remove(key)
}
}

View File

@ -1,54 +0,0 @@
//
// SDLPuncherActor.swift
// Tun
//
// Created by on 2026/1/7.
//
import Foundation
import NIOCore
actor PolicyRequesterActor {
nonisolated private let cooldown: Duration = .seconds(5)
// identityId
private var coolingDown: Set<UInt32> = []
// , map[identityId] = version
private var versions: [UInt32: UInt32] = [:]
init() {
}
//
func submitPolicyRequest(using quicClient: SDLQUICClient?, request: inout SDLPolicyRequest) {
let identityId = request.srcIdentityID
guard let quicClient, !coolingDown.contains(identityId) else {
return
}
//
coolingDown.insert(identityId)
let version = self.versions[identityId, default: 1]
request.version = version
//
self.versions[identityId] = version + 1
//
if let queryData = try? request.serializedData() {
quicClient.send(type: .policyRequest, data: queryData)
}
Task {
//
try? await Task.sleep(for: .seconds(5))
self.endCooldown(for: identityId)
}
}
private func endCooldown(for key: UInt32) {
self.coolingDown.remove(key)
}
}