From 4169e8760d4d6e9bb30f7e82dfd4f471dd54f24c Mon Sep 17 00:00:00 2001 From: Varun Chauhan Date: Fri, 17 Aug 2018 17:40:02 +0530 Subject: [PATCH] Added dwell count --- pkg/events/bucket.go | 6 +++++- pkg/rules/rule.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/events/bucket.go b/pkg/events/bucket.go index c0d1025..16cea71 100644 --- a/pkg/events/bucket.go +++ b/pkg/events/bucket.go @@ -91,7 +91,11 @@ func (rb *Bucket) getMaxDwell() time.Duration { // CanFlush returns if the bucket can be evicted from the db func (rb *Bucket) CanFlush() bool { - return time.Since(rb.CreatedAt) >= time.Millisecond*time.Duration(rb.flushWait) + if rb.Rule.DwellCount < 1 { + return time.Since(rb.CreatedAt) >= time.Millisecond*time.Duration(rb.flushWait) + } + return time.Since(rb.CreatedAt) >= time.Millisecond*time.Duration(rb.flushWait) || + uint64(len(rb.Events)) >= rb.Rule.DwellCount } // CanFlushIn returns time left for flush diff --git a/pkg/rules/rule.go b/pkg/rules/rule.go index dd54f62..9c70c13 100644 --- a/pkg/rules/rule.go +++ b/pkg/rules/rule.go @@ -18,6 +18,7 @@ type Rule struct { EventTypePatterns []string `json:"event_type_patterns"` // a list of event types to look for. wildcards are allowed. Dwell uint64 `json:"dwell"` // dwell duration in milliseconds for events to arrive DwellDeadline uint64 `json:"dwell_deadline"` // dwell duration threshold after which arriving events expand the dwell window + DwellCount uint64 `json:"dwell_count"` // dwell count is no of events it should wait before flushing events MaxDwell uint64 `json:"max_dwell"` // maximum dwell duration including expansion Regexes []string `json:"regexes,omitempty"` // generated regex string array from event types Disabled bool `json:"disabled,omitempty"` // if the rule is disabled @@ -61,6 +62,7 @@ type PublicRule struct { HookRetry int `json:"hook_retry"` // number of retries while attempting to post EventTypePatterns []string `json:"event_type_patterns"` // a list of event types to look for. wildcards are allowed. Dwell uint64 `json:"dwell"` // dwell duration in milliseconds for events to arrive + DwellCount uint64 `json:"dwell_count"` // dwell count is no of events it should wait before flushing events DwellDeadline uint64 `json:"dwell_deadline"` // dwell duration threshold after which arriving events expand the dwell window MaxDwell uint64 `json:"max_dwell"` // maximum dwell duration including expansion Disabled bool `json:"disabled,omitempty"` // if the rule is disabled @@ -76,6 +78,7 @@ func NewFromPublic(r *PublicRule) *Rule { HookRetry: r.HookRetry, EventTypePatterns: r.EventTypePatterns, Dwell: r.Dwell, + DwellCount: r.DwellCount, DwellDeadline: r.DwellDeadline, MaxDwell: r.MaxDwell, Disabled: r.Disabled,