Skip to content

Commit 15432cf

Browse files
committed
Merge commit '0dbb8856e873c22f9a8ec7fcd12d8e189cd8ce7e'
2 parents 75b19e2 + 0dbb885 commit 15432cf

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/github.com/PuerkitoBio/throttled/varyby.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type VaryBy struct {
3030
// Use this separator string to concatenate the various criteria of the VaryBy struct.
3131
// Defaults to a newline character if empty (\n).
3232
Separator string
33+
34+
// Custom specifies the custom-generated key to use for this request.
35+
// If not nil, the value returned by this function is used instead of any
36+
// VaryBy criteria.
37+
Custom func(r *http.Request) string
3338
}
3439

3540
// Key returns the key for this request based on the criteria defined by the VaryBy struct.
@@ -39,6 +44,10 @@ func (vb *VaryBy) Key(r *http.Request) string {
3944
if vb == nil {
4045
return "" // Special case for no vary-by option
4146
}
47+
if vb.Custom != nil {
48+
// A custom key generator is specified
49+
return vb.Custom(r)
50+
}
4251
sep := vb.Separator
4352
if sep == "" {
4453
sep = "\n" // Separator defaults to newline

src/github.com/PuerkitoBio/throttled/varyby_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ func TestVaryBy(t *testing.T) {
3939
&http.Request{Header: http.Header{"Cookie": []string{ck.String()}}},
4040
"test\n",
4141
},
42+
6: {
43+
&VaryBy{Cookies: []string{"ssn"}, RemoteAddr: true, Custom: func(r *http.Request) string {
44+
return "blah"
45+
}},
46+
&http.Request{Header: http.Header{"Cookie": []string{ck.String()}}},
47+
"blah",
48+
},
4249
}
4350
for i, c := range cases {
4451
got := c.vb.Key(c.r)

0 commit comments

Comments
 (0)