Skip to content

Commit

Permalink
responsewr: add negativettl option
Browse files Browse the repository at this point in the history
  • Loading branch information
luisguillenc committed Nov 27, 2020
1 parent 6a0f22d commit 298d74e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pkg/xlistd/wrappers/responsewr/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func parseOptions(src Config, opts map[string]interface{}, listID string) (Confi
dst.TTL = ttl
}

negativettl, ok, err := option.Int(opts, "negativettl")
if err != nil {
return dst, err
}
if ok && negativettl >= xlist.NeverCache {
dst.NegativeTTL = negativettl
}

reason, ok, err := option.String(opts, "reason")
if err != nil {
return dst, err
Expand Down
9 changes: 8 additions & 1 deletion pkg/xlistd/wrappers/responsewr/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
Reason string
Preffix string
TTL int
NegativeTTL int
UseThreshold bool
Score int
}
Expand All @@ -40,6 +41,9 @@ func New(list xlistd.List, cfg Config) *Wrapper {
if cfg.TTL < xlist.NeverCache {
cfg.TTL = 0
}
if cfg.NegativeTTL < xlist.NeverCache {
cfg.NegativeTTL = 0
}
return &Wrapper{cfg: cfg, list: list}
}

Expand Down Expand Up @@ -87,9 +91,12 @@ func (w *Wrapper) Check(ctx context.Context, name string, resource xlist.Resourc
if resp.Result && w.cfg.Clean {
resp.Reason = reason.Clean(resp.Reason)
}
if w.cfg.TTL != 0 {
if resp.Result && w.cfg.TTL != 0 {
resp.TTL = w.cfg.TTL
}
if !resp.Result && w.cfg.NegativeTTL != 0 {
resp.TTL = w.cfg.NegativeTTL
}
}
return resp, err
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/xlistd/wrappers/responsewr/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ func TestWrapper_CheckTTL(t *testing.T) {
mockup := &mockxl.List{ResourceList: ip4, Results: []bool{true, false}, TTL: 600}

var tests = []struct {
ttl int
want int
ttl int
negativettl int
want int
}{
{0, 600}, //0
{10, 10}, //1
{xlist.NeverCache, xlist.NeverCache}, //2
{-2, 600}, //3
{0, 0, 600}, //0 -> true
{50, 10, 10}, //1 -> false
{xlist.NeverCache, 10, xlist.NeverCache}, //2 -> true
{-2, -2, 600}, //3 -> false
}
for idx, test := range tests {
respwr := responsewr.New(mockup, responsewr.Config{TTL: test.ttl})
respwr := responsewr.New(mockup, responsewr.Config{TTL: test.ttl, NegativeTTL: test.negativettl})
resp, _ := respwr.Check(context.Background(), "10.10.10.10", xlist.IPv4)
if test.want != resp.TTL {
t.Errorf("idx[%v] respwr.Check(): want=%v got=%v", idx, test.want, resp.TTL)
Expand Down

0 comments on commit 298d74e

Please sign in to comment.