ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - vivek-ng/concurrency-limiter
Contribute to vivek-ng/concurrency-limiter development by creating an account on GitHub.
Visit Site

GitHub - vivek-ng/concurrency-limiter

GitHub - vivek-ng/concurrency-limiter

concurrency-limiter

Mentioned in Awesome Go GoDoc reference example GoReportCard example codecov

About

concurrency-limiter allows you to limit the number of goroutines accessing a resource with support for timeouts , dynamic priority of goroutines and context cancellation of goroutines.

Installation

To install concurrency-limiter:

go get github.com/vivek-ng/concurrency-limiter

Then import concurrency-limiter to use it

    import(
        github.com/vivek-ng/concurrency-limiter/limiter
    )

    nl := limiter.New(3)
    ctx := context.Background()
    nl.Wait(ctx)
    // Perform actions .........
    nl.Finish()

Usage

Below are some examples of using this library. To run real examples , please check the examples folder.

Limiter

    import(
        github.com/vivek-ng/concurrency-limiter/limiter
    )

    func main() {
        nl := limiter.New(3)

        var wg sync.WaitGroup
        wg.Add(15)

        for i := 0; i < 15; i++ {
            go func(index int) {
                defer wg.Done()
                ctx := context.Background()
                nl.Wait(ctx)
                // in real life , this can be DB operations , message publish to queue ........
                fmt.Println("executing action...: ", "index: ", index, "current number of goroutines: ", nl.Count())
                nl.Finish()
            }(i)
        }
        wg.Wait()
    }

Priority Limiter

    import(
        github.com/vivek-ng/concurrency-limiter/priority
    )
    
    func main() {
        pr := priority.NewLimiter(1)
        var wg sync.WaitGroup
        wg.Add(15)
        for i := 0; i < 15; i++ {
            go func(index int) {
                defer wg.Done()
                ctx := context.Background()
                if index%2 == 1 {
                    pr.Wait(ctx, priority.High)
                } else {
                    pr.Wait(ctx, priority.Low)
                }
                // in real life , this can be DB operations , message publish to queue ........
                fmt.Println("executing action...: ", "index: ", index, "current number of goroutines: ", pr.Count())
                pr.Finish()
            }(i)
        }
        wg.Wait()
    }

Examples

Limiter

    nl := limiter.New(3)
    ctx := context.Background()
    nl.Wait(ctx)
    // Perform actions .........
    nl.Finish()

In the above example , there can be a maximum of 3 goroutines accessing a resource concurrently. The other goroutines are added to the waiting list and are removed and given a chance to access the resource in the FIFO order. If the context is cancelled , the goroutine is removed from the waitlist.

Limiter with Timeout

    nl := limiter.New(3,
    WithTimeout(10),
    )
    ctx := context.Background()
    nl.Wait(ctx)
    // Perform actions .........
    nl.Finish()

In the above example , the goroutines will wait for a maximum of 10 milliseconds. Goroutines will be removed from the waitlist after 10 ms even if the number of concurrent goroutines is greater than the limit specified.

Priority Limiter

    nl := priority.NewLimiter(3)
    ctx := context.Background()
    nl.Wait(ctx , priority.High)
    // Perform actions .........
    nl.Finish()

In Priority Limiter , goroutines with higher priority will be given preference to be removed from the waitlist. For instance in the above example , the goroutine will be given the maximum preference because it is of high priority. In the case of tie between the priorities , the goroutines will be removed from the waitlist in the FIFO order.

Priority Limiter with Dynamic priority

    nl := priority.NewLimiter(3,
    WithDynamicPriority(5),
    )
    ctx := context.Background()
    nl.Wait(ctx , priority.Low)
    // Perform actions .........
    nl.Finish()

In Dynamic Priority Limiter , the goroutines with lower priority will get their priority increased periodically by the time period specified. For instance in the above example , the goroutine will get it's priority increased every 5 ms. This will ensure that goroutines with lower priority do not suffer from starvation. It's highly recommended to use Dynamic Priority Limiter to avoid starving low priority goroutines.

Priority Limiter with Timeout

    nl := priority.NewLimiter(3,
    WithTimeout(30),
    WithDynamicPriority(5),
    )
    ctx := context.Background()
    nl.Wait(ctx , priority.Low)
    // Perform actions .........
    nl.Finish()

This is similar to the timeouts in the normal limiter. In the above example , goroutines will wait a maximum of 30 milliseconds. The low priority goroutines will get their priority increased every 5 ms.

Runnable Function

    nl := priority.NewLimiter(3)
    ctx := context.Background()
    nl.Run(ctx , priority.Low , func()error {
        return sendMetrics()
    })

Runnable function will allow you to wrap your function and execute them with concurrency limit. This function is a wrapper on top of the Wait() and Finish() functions.

Contribution

Please feel free to open up issues , create PRs for bugs/features. All contributions are welcome :)

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory