-
Notifications
You must be signed in to change notification settings - Fork 60
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
Implement batch keys in OSS #1344
Implement batch keys in OSS #1344
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
pkg/execution/batch/redis.go
Outdated
Str("expression", *fn.EventBatch.Key). | ||
Interface("event", evt.Map()). | ||
Msg("error evaluating batch key expression") | ||
return "<invalid>", nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's invalid, maybe just return it as the default fn.ID.String()
instead?
hardcoding "<invalid>"
here is no different from just using the function ID.
maybe also return an error
just in case. I suspect it's not going to be used anytime soon, but hopefully we can surface this kind of invalid expression errors to users better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated this to return an error and use the fn identifier as fallback, just to clarify, should we pass this up already or silently ignore the error? I guess it does make sense to warn early and condition users to use valid expressions returning strings, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a way to pass this error back up to the user atm.
It's the same for all expression related evaluation, and we should bubble the evaluation error up somehow so users are aware.
But regardless we don't want to fail
the execution because there's an invalid expression since there are no ways to recover from that and runs are essentially lost at that point.
4a935df
to
0083a3a
Compare
0083a3a
to
a3ea6b7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Nice tests as well.
Have a couple of comments and suggestions.
Only need you to add the CEL expr check in IsValid
, others are minor suggestions that are not blocking.
Approving the PR so you can merge it once you made the changes.
if str, ok := out.(string); ok { | ||
return str, nil | ||
} | ||
return fmt.Sprintf("%v", out), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ratelimit hashes the key before returning it. could be a something to copy if it makes sense, and can save you that type assertion above.
inngest/pkg/execution/ratelimit/ratelimit.go
Lines 27 to 51 in 16f8e28
// RateLimitKey returns the rate limiting key given a function ID, rate limit config, | |
// and incoming event data. | |
func RateLimitKey(ctx context.Context, id uuid.UUID, c inngest.RateLimit, evt map[string]any) (string, error) { | |
if c.Key == nil { | |
return id.String(), nil | |
} | |
eval, err := expressions.NewExpressionEvaluator(ctx, *c.Key) | |
if err != nil { | |
return "", ErrEvaluatingRateLimitExpression | |
} | |
res, _, err := eval.Evaluate(ctx, expressions.NewData(map[string]any{"event": evt})) | |
if err != nil { | |
return "", ErrEvaluatingRateLimitExpression | |
} | |
// Take a checksum of this data. It doesn't matter if this is a map or a string; | |
// as long as we're consistent here. | |
return hash(res, id), nil | |
} | |
func hash(res any, id uuid.UUID) string { | |
ui := xxhash.Sum64String(fmt.Sprintf("%v", res)) | |
sum := strconv.FormatUint(ui, 36) | |
return fmt.Sprintf("%s-%s", id, sum) | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you're already base64 encoding it, so it might not matter.
Description
This pull request introduces a new
key
option on the function batching configuration. When storing batches, the batch pointer now includes a hashed representation of the evaluated expression. Unlike the previous implementation where one function would have at most one associated active batch, batch keys create many batches per function.Motivation
Batch keys enable event aggregation or grouping by tenants: Events could be counted for user notifications, or processed together to enable other features.
Type of change (choose one)
Checklist
Check our Pull Request Guidelines