Suzaku
Suzaku is a swift version of the hashed wheel timer.
Feature
- Thread safe
- GCD Timer
- Local variable timer
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Usage
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) { [weak self] in
guard let self = self else { return }
self.counter += 1
print("counter: \(self.counter), \(self.dateFormatter.string(from: Date()))")
}
}
}
/// local variable timer
class SomeClass {
let dateFormatter = DateFormatter()
func someFunction() {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
var localTimer: HashedWheelTimer? = try! HashedWheelTimer(tickDuration: .seconds(1), ticksPerWheel: 1, dispatchQueue: nil)
localTimer?.resume()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
_ = try? localTimer?.addTimeout(timeInterval: .seconds(5), reapting: true) { [weak self] in
guard let self = self else { return }
print("fired \(self.dateFormatter.string(from: Date()))")
localTimer?.stop()
localTimer = nil
}
}
}
}
Requirements
- iOS 10.0+
- Swift 5.0+
- Xcode 11+
- OSX 10.12+
Installation
CocoaPods
Suzaku is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Suzaku'
Swift Package Manager
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")
]
Author
elijahdou, [email protected]
License
Suzaku is available under the Apache 2.0 license. See the LICENSE file for more info.