suz

Suzaku

Hashed wheel timer in Swift

Showing:

Popularity

Downloads/wk

0

GitHub Stars

78

Maintenance

Last Commit

2mos ago

Contributors

0

Package

Dependencies

0

License

Apache 2.0

Categories

Readme

Suzaku

CI Status Version Platform

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, 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()))")
        })
    }
}

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, elijahdou@gmail.com

License

Suzaku is available under the Apache 2.0 license. See the LICENSE file for more info.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100