File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed
src/github.com/PuerkitoBio/throttled Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,11 @@ type VaryBy struct {
30
30
// Use this separator string to concatenate the various criteria of the VaryBy struct.
31
31
// Defaults to a newline character if empty (\n).
32
32
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
33
38
}
34
39
35
40
// 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 {
39
44
if vb == nil {
40
45
return "" // Special case for no vary-by option
41
46
}
47
+ if vb .Custom != nil {
48
+ // A custom key generator is specified
49
+ return vb .Custom (r )
50
+ }
42
51
sep := vb .Separator
43
52
if sep == "" {
44
53
sep = "\n " // Separator defaults to newline
Original file line number Diff line number Diff line change @@ -39,6 +39,13 @@ func TestVaryBy(t *testing.T) {
39
39
& http.Request {Header : http.Header {"Cookie" : []string {ck .String ()}}},
40
40
"test\n " ,
41
41
},
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
+ },
42
49
}
43
50
for i , c := range cases {
44
51
got := c .vb .Key (c .r )
You can’t perform that action at this time.
0 commit comments