Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized the exact filter performance #7311

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions pkg/eventfilter/subscriptionsapi/exact_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import (
)

type exactFilter struct {
filters map[string]string
attrsFilter eventfilter.Filter
filters map[string]string
}

// NewExactFilter returns an event filter which passes if value exactly matches the value of the context
Expand All @@ -44,23 +43,31 @@ func NewExactFilter(filters map[string]string) (eventfilter.Filter, error) {
}
return &exactFilter{
filters: filters,
// we're creating this filter to leverage the same filter logic of the existing attributes filter
attrsFilter: attributes.NewAttributesFilter(filters),
}, nil
}

func (filter *exactFilter) Filter(ctx context.Context, event cloudevents.Event) eventfilter.FilterResult {
if filter.filters == nil {
return eventfilter.NoFilter
}
for attribute, value := range filter.filters {
if attribute == "" || value == "" {
return eventfilter.NoFilter
}
}
logger := logging.FromContext(ctx)
logger.Debugw("Performing an exact match ", zap.Any("filters", filter.filters), zap.Any("event", event))
return filter.attrsFilter.Filter(ctx, event)
for k, v := range filter.filters {
value, ok := attributes.LookupAttribute(event, k)
if !ok {
logger.Debug("Attribute not found", zap.String("attribute", k))
return eventfilter.FailFilter
}
var s string
if s, ok = value.(string); !ok {
s = fmt.Sprintf("%v", value)
}
if s != v {
logger.Debug("Attribute had non-matching value", zap.String("attribute", k), zap.String("filter", v), zap.Any("received", value))
return eventfilter.FailFilter
}
}
return eventfilter.PassFilter
}

func (filter *exactFilter) Cleanup() {}