Swiftpack.co -  Co2333/SwiftThrottle as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Co2333/SwiftThrottle
A simple throttle written in Swift.
.package(url: "https://github.com/Co2333/SwiftThrottle.git", from: "1.0.1")

SwiftThrottle

A thread safe throttle written in Swift licensed under MIT.

Introduction

This throttle is intended to prevent the program from crashing with too many requests or is used for saving computer resources.

Swift Throttle is not designed for operations that require high time accuracy

Examples

I need a throttle that executes my code at most once per second, in the main thread.

let throttle = Throttle(minimumDelay: 1)
throttle.throttle { 
    print("it's default to main thread")
}

I need a throttle that executes my code at most once per hour, in some background thread.

let throttle = Throttle(minimumDelay: 3600, queue: .global())
throttle.throttle { 
    print("will async to global thread")
}

Explanation

  • The first execution is released immediately and the timestamp is recorded as 0.
  • Subsequent executions will enter the throttling control valve and will be cancelled if the execution interval is too short.
  • The last execution, if cancelled, will be reprogrammed to execute immediately after the time reaches the required interval.

Explain


2021-08-01

Copyright © 2021 Lakr Aream. All rights reserved.

GitHub

link
Stars: 3
Last commit: 20 hours ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Bug Fix
22 hours ago

🐛: now last job will run correctly

😭

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API