Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rate limiting #1

Closed
ChimeraCoder opened this issue Mar 5, 2013 · 3 comments
Closed

Implement rate limiting #1

ChimeraCoder opened this issue Mar 5, 2013 · 3 comments

Comments

@ChimeraCoder
Copy link
Owner

It should be straightforward to implement rate-limiting as https://github.com/ChimeraCoder/goangel does without changing the interface of the library.

@ChimeraCoder
Copy link
Owner Author

Technically, this involves rate-limiting based on "resource families": https://dev.twitter.com/docs/api/1.1/get/application/rate_limit_status

@bobrik
Copy link
Contributor

bobrik commented Dec 3, 2013

I do graceful rate-limiting like this:

func TryUntilSucceeded(method func() (interface{}, error)) (result interface{}, err error) {
    for {
        result, err = method()

        if apiErr, ok := err.(*anaconda.ApiError); ok {
            limited, freed := apiErr.RateLimitCheck()
            if !limited {
                break
            } else {
                freed = freed.Add(5 * time.Second) // extra margin
                now := time.Now()
                fmt.Println("[rate limited] will wake up at", freed, "in", freed.Sub(now).Seconds(), "seconds")
                time.Sleep(freed.Sub(now))
                continue
            }
        } else {
            break
        }
    }

    return result, err
}

and usage looks like this:

users, err := TryUntilSucceeded(func() (interface {}, error) {
    return lookuper.Api.GetUsersLookupByIds(chunk, nil)
})

// users.([]anaconda.TwitterUser) to get profiles

I need #12 and ApiError to make this happen, but it works great. It also doesn't need time.Sleep() and can handle weird stuff at twitter side, unlike ChimeraCoder/goangel.

@ChimeraCoder
Copy link
Owner Author

Looks like we forgot to close this issue out before. Thanks @bobrik - the (optional) rate-limiting thats' now built-in is very similar to what you outline here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants