From a637d638263750446086281823adf95458548ca2 Mon Sep 17 00:00:00 2001 From: ppom <> Date: Sat, 25 Mar 2023 19:12:11 +0100 Subject: [PATCH] close #6 --- app/conf.go | 3 ++- app/reaction.go | 21 ++++++++++++++++++++- reaction.yml | 6 +++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/conf.go b/app/conf.go index 399a4cf..65ae4c8 100644 --- a/app/conf.go +++ b/app/conf.go @@ -31,7 +31,7 @@ type Filter struct { compiledRegex []regexp.Regexp patternName, patternWithBraces string - Retry uint + Retry int RetryPeriod string `yaml:"retry-period"` retryDuration time.Duration @@ -64,6 +64,7 @@ func (c *Conf) setup() { filter := stream.Filters[filterName] filter.stream = stream filter.name = filterName + filter.matches = make(map[string][]time.Time) // Parse Duration retryDuration, err := time.ParseDuration(filter.RetryPeriod) diff --git a/app/reaction.go b/app/reaction.go index e0337b5..1fe65b8 100644 --- a/app/reaction.go +++ b/app/reaction.go @@ -3,6 +3,7 @@ package app import ( "bufio" "flag" + // "fmt" "log" "os" @@ -80,13 +81,31 @@ func (a *Action) exec(match string) { } } +func (f *Filter) cleanOldMatches(match string) { + now := time.Now() + newMatches := make([]time.Time, 0, len(f.matches[match])) + for _, old := range f.matches[match] { + if old.Add(f.retryDuration).After(now) { + newMatches = append(newMatches, old) + } + } + f.matches[match] = newMatches +} + func (f *Filter) handle() chan *string { lines := make(chan *string) go func() { for line := range lines { if match := f.match(line); match != "" { - f.execActions(match) + + f.cleanOldMatches(match) + + f.matches[match] = append(f.matches[match], time.Now()) + + if len(f.matches[match]) >= f.Retry { + f.execActions(match) + } } } }() diff --git a/reaction.yml b/reaction.yml index b076840..b1e9b0f 100644 --- a/reaction.yml +++ b/reaction.yml @@ -8,13 +8,13 @@ patterns: streams: tailDown: - cmd: [ "tail", "/home/ao/DOWN" ] + cmd: [ "sh", "-c", "echo 'found 1.1.1.1' && sleep 2s && echo 'found 1.1.1.2' && sleep 2s && echo 'found 1.1.1.1' && sleep 1s" ] filters: findIP: regex: - found - # retry: 1 - retry-period: 1s + retry: 2 + retry-period: 5s actions: damn: cmd: [ "echo", "" ]