This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
log "github.com/sirupsen/logrus" | |
"gopkg.in/olivere/elastic.v6" | |
"os" | |
"strings" | |
"time" |
The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.
In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.
This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package responseWriter | |
import ( | |
"bufio" | |
"bytes" | |
"net" | |
"net/http" | |
"net/http/httptest" | |
) |
I hereby claim:
- I am annismckenzie on github.
- I am annismckenzie (https://keybase.io/annismckenzie) on keybase.
- I have a public key ASB9U9Z8wcPQS7pNHi_jB2ssUidCZ-s5A891ReQuWTgjRAo
To claim this, I am signing this object: