|
1 | 1 | package retry
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + |
4 | 7 | "github.com/zalando/skipper/filters"
|
| 8 | + "gopkg.in/yaml.v2" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + single = "single" |
5 | 13 | )
|
6 | 14 |
|
7 |
| -type retry struct{} |
| 15 | +type ( |
| 16 | + retrySpec struct{} |
| 17 | + RetryFilter struct { |
| 18 | + Type string `json:"type,omitempty"` |
| 19 | + StatusCodes []int `json:"status-codes,omitempty"` |
| 20 | + MaxTimes int `json:"max-times,omitempty"` |
| 21 | + |
| 22 | + Check func(*http.Response) bool |
| 23 | + } |
| 24 | +) |
8 | 25 |
|
9 | 26 | // NewRetry creates a filter specification for the retry() filter
|
10 |
| -func NewRetry() filters.Spec { return retry{} } |
| 27 | +func NewRetry() filters.Spec { return &retrySpec{} } |
| 28 | + |
| 29 | +func (*retrySpec) Name() string { return filters.RetryName } |
| 30 | + |
| 31 | +func (s *retrySpec) CreateFilter(args []interface{}) (filters.Filter, error) { |
| 32 | + rf := &RetryFilter{} |
| 33 | + |
| 34 | + if config, ok := args[0].(string); !ok { |
| 35 | + return nil, fmt.Errorf("filter %q requires single string argument", s.Name()) |
| 36 | + } else if err := yaml.Unmarshal([]byte(config), rf); err != nil { |
| 37 | + return nil, fmt.Errorf("failed to parse configuration: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + switch rf.Type { |
| 41 | + case single: |
| 42 | + i := 0 |
| 43 | + rf.Check = func(rsp *http.Response) bool { |
| 44 | + i++ |
| 45 | + if i > rf.MaxTimes { |
| 46 | + return false |
| 47 | + } |
| 48 | + return shouldRetry(rsp.StatusCode, rf.StatusCodes) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return rf, nil |
| 53 | +} |
| 54 | + |
| 55 | +// copy from proxy.shouldLog |
| 56 | +func shouldRetry(statusCode int, prefixes []int) bool { |
| 57 | + if len(prefixes) == 0 { |
| 58 | + return false |
| 59 | + } |
| 60 | + |
| 61 | + match := false |
| 62 | + for _, prefix := range prefixes { |
| 63 | + switch { |
| 64 | + case prefix < 10: |
| 65 | + match = (statusCode >= prefix*100 && statusCode < (prefix+1)*100) |
| 66 | + case prefix < 100: |
| 67 | + match = (statusCode >= prefix*10 && statusCode < (prefix+1)*10) |
| 68 | + default: |
| 69 | + match = statusCode == prefix |
| 70 | + } |
| 71 | + if match { |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + return match |
| 76 | +} |
11 | 77 |
|
12 |
| -func (retry) Name() string { return filters.RetryName } |
13 |
| -func (retry) CreateFilter([]interface{}) (filters.Filter, error) { return retry{}, nil } |
14 |
| -func (retry) Response(filters.FilterContext) {} |
| 78 | +func (rf *RetryFilter) Response(filters.FilterContext) {} |
15 | 79 |
|
16 |
| -func (retry) Request(ctx filters.FilterContext) { |
17 |
| - ctx.StateBag()[filters.RetryName] = struct{}{} |
| 80 | +func (rf *RetryFilter) Request(ctx filters.FilterContext) { |
| 81 | + ctx.StateBag()[filters.RetryName] = rf.Check |
18 | 82 | }
|
0 commit comments