-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Able to rate limit by custom HTTP request headers. Also, write a lot …
…more tests.
- Loading branch information
Didip Kerabat
committed
May 17, 2015
1 parent
93960d6
commit fc751b9
Showing
8 changed files
with
177 additions
and
59 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Package config provides data structure to configure rate limiter. | ||
package config | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// NewLimiter is a constructor for Limiter. | ||
func NewLimiter(max int64, ttl time.Duration) *Limiter { | ||
limiter := &Limiter{Max: max, TTL: ttl} | ||
limiter.Message = "You have reached maximum request limit." | ||
limiter.StatusCode = 429 | ||
|
||
return limiter | ||
} | ||
|
||
// Limiter is a config struct to limit a particular request handler. | ||
type Limiter struct { | ||
// HTTP message when limit is reached. | ||
Message string | ||
|
||
// HTTP status code when limit is reached. | ||
StatusCode int | ||
|
||
// Maximum number of requests to limit per duration. | ||
Max int64 | ||
|
||
// Duration of rate limiter. | ||
TTL time.Duration | ||
|
||
// List of HTTP Methods to limit (GET, POST, PUT, etc.). | ||
// Empty means limit all methods. | ||
Methods []string | ||
|
||
// List of HTTP headers to limit. | ||
// Empty means skip headers checking. | ||
Headers map[string][]string | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestConstructor(t *testing.T) { | ||
limiter := NewLimiter(1, time.Second) | ||
if limiter.Max != 1 { | ||
t.Errorf("Max field is incorrect. Value: %v", limiter.Max) | ||
} | ||
if limiter.TTL != time.Second { | ||
t.Errorf("TTL field is incorrect. Value: %v", limiter.TTL) | ||
} | ||
if limiter.Message != "You have reached maximum request limit." { | ||
t.Errorf("Message field is incorrect. Value: %v", limiter.Message) | ||
} | ||
if limiter.StatusCode != 429 { | ||
t.Errorf("StatusCode field is incorrect. Value: %v", limiter.StatusCode) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Package errors provide data structure for errors. | ||
package errors | ||
|
||
import "fmt" | ||
|
||
// HTTPError is an error struct that returns both message and status code. | ||
type HTTPError struct { | ||
Message string | ||
StatusCode int | ||
} | ||
|
||
// Error returns error message. | ||
func (httperror *HTTPError) Error() string { | ||
return fmt.Sprintf("%v: %v", httperror.StatusCode, httperror.Message) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package errors | ||
|
||
import "testing" | ||
|
||
func TestError(t *testing.T) { | ||
errs := HTTPError{"blah", 429} | ||
if errs.Error() == "" { | ||
t.Errorf("Unable to print Error(). Value: %v", errs.Error()) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package libstring | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFlattenMapSliceString(t *testing.T) { | ||
headersToCheck := make(map[string][]string) | ||
headersToCheck["X-Auth-Token"] = []string{"abc123", "brotato!23"} | ||
|
||
for i, flatten := range FlattenMapSliceString(headersToCheck, "headers", "") { | ||
if flatten != "headers:X-Auth-Token:"+headersToCheck["X-Auth-Token"][i] { | ||
t.Errorf("Failed to flatten map correctly. Result: %v", flatten) | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tollbooth | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/didip/tollbooth/storages" | ||
) | ||
|
||
func TestLimitByKeyParts(t *testing.T) { | ||
storage := storages.NewInMemory() | ||
limiter := NewLimiter(1, time.Second) // Only 1 request per second is allowed. | ||
|
||
httperror := LimitByKeyParts(storage, limiter, []string{"127.0.0.1", "/"}) | ||
if httperror != nil { | ||
t.Errorf("First time count should not return error. Error: %v", httperror.Error()) | ||
} | ||
|
||
httperror = LimitByKeyParts(storage, limiter, []string{"127.0.0.1", "/"}) | ||
if httperror == nil { | ||
t.Errorf("Second time count should return error because it exceeds 1 request per second.") | ||
} | ||
|
||
<-time.After(1 * time.Second) | ||
httperror = LimitByKeyParts(storage, limiter, []string{"127.0.0.1", "/"}) | ||
if httperror != nil { | ||
t.Errorf("Third time count should not return error because the 1 second window has passed.") | ||
} | ||
} |