Suzaku is a swift version of the hashed wheel timer.
- Thread safe
- GCD Timer
- Local variable timer
To run the example project, clone the repo, and run pod install
from the Example directory first.
Hashed Wheel Timer were used as a base for Kernels and Network stacks. It is suitable for scenarios where a large number of timed tasks are concurrent. Suzaku is a swift implementation of hashed wheel timer designed for iOS clients, suitable for scenarios such as live rooms and persistent connections.
/// normal
class SomeClass {
let timer = try! HashedWheelTimer(tickDuration: .seconds(1), ticksPerWheel: 8, dispatchQueue: nil)
var counter = 0
let dateFormatter = DateFormatter()
func someFunction() {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
timer.resume()
_ = try? timer.addTimeout(timeInterval: .seconds(3), reapting: true, block: { [weak self](timer) in
guard let self = self else { return }
self.counter += 1
print("\(Thread.current) counter: \(self.counter) at \(self.dateFormatter.string(from: Date()))")
if self.counter == 18 {
timer.stop()
}
})
}
}
/// local variable timer
class SomeClass {
let dateFormatter = DateFormatter()
func someFunction() {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var localTimer = try? HashedWheelTimer(tickDuration: .seconds(1), ticksPerWheel: 1, dispatchQueue: DispatchQueue.global())
localTimer?.resume()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
_ = try? localTimer?.addTimeout(timeInterval: .seconds(5), reapting: true, block: { [weak self](timer) in
guard let self = self else { return }
print("\(Thread.current) fired \(self.dateFormatter.string(from: Date()))")
})
}
}
- iOS 10.0+
- Swift 5.0+
- Xcode 11+
- OSX 10.12+
Suzaku is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Suzaku'
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. It is in early development, but Alamofire does support its use on supported platforms.
Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/elijahdou/Suzaku.git")
]
elijahdou, elijahdou@gmail.com
Suzaku is available under the Apache 2.0 license. See the LICENSE file for more info.