nicklaw5 / helix

A Twitch Helix API client written in Go.
MIT License
240 stars 91 forks source link

Proposal: Add default rate limit function #129

Open Cidan opened 2 years ago

Cidan commented 2 years ago

Currently in the documentation, a proposed rate limit function is provided as such:

func rateLimitCallback(lastResponse *helix.Response) error {
    if lastResponse.GetRateLimitRemaining() > 0 {
        return nil
    }

    var reset64 int64
    reset64 = int64(lastResponse.GetRateLimitReset())

    currentTime := time.Now().Unix()

    if currentTime < reset64 {
        timeDiff := time.Duration(reset64 - currentTime)
        if timeDiff > 0 {
            fmt.Printf("Waiting on rate limit to pass before sending next request (%d seconds)\n", timeDiff)
            time.Sleep(timeDiff * time.Second)
        }
    }

    return nil
}

I propose adding this function, sans the print statement, to the core helix library. The user would still have to specify the use of this function, but it would simplify the usage of this library and reduce boiler plate for the end user. As such, a client might instantiate the client like so:

client, err := helix.NewClient(&helix.Options{
    ClientID:      "your-client-id",
    RateLimitFunc: helix.DefaultRateLimit,
})

where helix.DefaultRateLimit is the example function above.

Thoughts?

nicklaw5 commented 2 years ago

Happy to accept a PR :slightly_smiling_face:

Cidan commented 2 years ago

Reasonable response, I'll go ahead and send one over soon.