/
tarpit.go
59 lines (45 loc) · 1.06 KB
/
tarpit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package diag
import (
"net/http"
"time"
"github.com/zalando/skipper/filters"
)
type tarpitSpec struct{}
type tarpit struct {
d time.Duration
}
func NewTarpit() filters.Spec {
return &tarpitSpec{}
}
func (t *tarpitSpec) Name() string {
return filters.TarpitName
}
func (t *tarpitSpec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 1 {
return nil, filters.ErrInvalidFilterParameters
}
s, ok := args[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
d, err := time.ParseDuration(s)
if err != nil {
return nil, filters.ErrInvalidFilterParameters
}
return &tarpit{d: d}, nil
}
func (t *tarpit) Request(ctx filters.FilterContext) {
ctx.Serve(&http.Response{StatusCode: http.StatusOK, Body: &slowBlockingReader{d: t.d}})
}
func (*tarpit) Response(filters.FilterContext) {}
type slowBlockingReader struct {
d time.Duration
}
func (r *slowBlockingReader) Read(p []byte) (int, error) {
time.Sleep(r.d)
n := copy(p, []byte(" "))
return n, nil
}
func (r *slowBlockingReader) Close() error {
return nil
}