Skip to content

proof of concept for a rate limiter #155

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,25 @@ var au aurora.Aurora
var currentConfig *APIConfig

func generic(method string, addr string, args []string) {
var body io.Reader

d, err := GetBody("application/json", args)
if err != nil {
panic(err)
}
if len(d) > 0 {
body = strings.NewReader(d)
rateLimiter := Throttle{
rate: viper.GetInt("rsh-load-rate"),
requests: viper.GetInt("rsh-requests"),
}

req, _ := http.NewRequest(method, fixAddress(addr), body)
MakeRequestAndFormat(req)
rateLimiter.RateLimit(func() {
var body io.Reader

d, err := GetBody("application/json", args)
if err != nil {
panic(err)
}
if len(d) > 0 {
body = strings.NewReader(d)
}

req, _ := http.NewRequest(method, fixAddress(addr), body)
MakeRequestAndFormat(req)
})
}

// templateVarRegex used to find/replace variables `/{foo}/bar/{baz}` in a
Expand Down Expand Up @@ -544,6 +551,8 @@ Not after (expires): %s (%s)
AddGlobalFlag("rsh-raw", "r", "Output result of query as raw rather than an escaped JSON string or list", false, false)
AddGlobalFlag("rsh-server", "s", "Override scheme://server:port for an API", "", false)
AddGlobalFlag("rsh-header", "H", "Add custom header", []string{}, true)
AddGlobalFlag("rsh-requests", "R", "Do a number of requests", 1, false)
AddGlobalFlag("rsh-load-rate", "l", "Set a load rate in requests per second. Overrides the HTTP cache to false", 1, false)
Comment on lines +554 to +555
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these default to zero? I have some concerns:

  1. If non-zero by default they disable caching
  2. Limiting to 1 request a second is problematic for automatic pagination (which may do many sequential requests) and also see Bulk Resource Management #156 which does many requests.
  3. Total number of requests vs. some time period (e.g. 30 seconds) - which is better for this?

AddGlobalFlag("rsh-query", "q", "Add custom query param", []string{}, true)
AddGlobalFlag("rsh-no-paginate", "", "Disable auto-pagination", false, false)
AddGlobalFlag("rsh-profile", "p", "API auth profile", "default", false)
Expand Down Expand Up @@ -735,6 +744,14 @@ func Run() (returnErr error) {
if headers, _ := GlobalFlags.GetStringArray("rsh-header"); len(headers) > 0 {
viper.Set("rsh-header", headers)
}
if requests, _ := GlobalFlags.GetInt("rsh-requests"); requests > 0 {
viper.Set("rsh-requests", requests)
}
if rps, _ := GlobalFlags.GetInt("rsh-load-rate"); rps > 0 {
viper.Set("rsh-load-rate", rps)
// doesn't make sense to flood with requests and obtain a cached response
viper.Set("rsh-no-cache", true)
}
profile, _ := GlobalFlags.GetString("rsh-profile")
viper.Set("rsh-profile", profile)

Expand Down
8 changes: 7 additions & 1 deletion cli/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ func (o Operation) command() *cobra.Command {

req, _ := http.NewRequest(o.Method, uri, body)
req.Header = headers
MakeRequestAndFormat(req)
rateLimiter := Throttle{
rate: viper.GetInt("rsh-load-rate"),
requests: viper.GetInt("rsh-requests"),
}
rateLimiter.RateLimit(func() {
MakeRequestAndFormat(req)
})
},
}

Expand Down
33 changes: 33 additions & 0 deletions cli/throttle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cli

import (
"sync"
"time"
)

type Throttle struct {
rate int // requests per second
requests int // total requests
}

// simple rate limitter
func (l Throttle) RateLimit(f func()) {
executed := 0

ticker := time.Tick(time.Second)
var wg = &sync.WaitGroup{}
wg.Add(l.requests)
for ; true; <-ticker {
if executed >= l.requests {
break
}
for i := 0; i < l.rate; i++ {
go func() {
f()
executed++
wg.Done()
}()
}
}
wg.Wait()
Comment on lines +17 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions/concerns:

  1. What happens if a request takes more than a second? Seems like we'd get many in flight at the same time.
  2. If a request finishes really fast there is no opportunity to create new ones until the full second has passed, even if a sliding time window would allow a new request.
  3. This assumes all the requests are the same, but add multiple requests option flag #134 suggests they might get generated for each API operation. How will that work?

}