punchnet-macos/Tun/Punchnet/SDLQPSCounter.swift
2026-01-14 11:13:15 +08:00

38 lines
870 B
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SDLQPSCounter.swift
// Tun
//
// Created by on 2024/4/16.
//
import Foundation
// qps
class SDLQPSCounter: @unchecked Sendable {
private var count = 0
private let timer: DispatchSourceTimer
private let label: String
private let queue = DispatchQueue(label: "com.punchnet.qps")
init(label: String) {
self.label = label
timer = DispatchSource.makeTimerSource(queue: queue)
timer.schedule(deadline: .now(), repeating: .seconds(1), leeway: .milliseconds(100))
timer.setEventHandler { [weak self] in
guard let self = self else { return }
self.count = 0
}
timer.resume()
}
func increment(num: Int = 1) {
queue.async {
self.count += num
}
}
deinit {
timer.cancel()
}
}