Skip to content

feat(quotas): Optionally exceed quota once [INGEST-1654] - #1521

Merged
olksdr merged 8 commits into
masterfrom
feat/quota-go-over
Oct 13, 2022
Merged

olksdr merged 8 commits into
masterfrom
feat/quota-go-over

Conversation

@jjbayer

@jjbayer jjbayer commented Oct 10, 2022 •

Copy link
Copy Markdown
Member

For checks with quantity=0, we need the quota stored in redis to exceed the rate limit to be executed. For this, it's necessary to accept the first call of the rate limiter that exceeds the limit.

  • Test that describes desired behavior
  • Add an optional argument to lua script to over-accept.
 if quantity == 0 or over_accept_once then
      rejected = consumed >= limit
  else
      rejected = consumed + quantity > limit
  end

#skip-changelog

@olksdr
olksdr marked this pull request as ready for review October 11, 2022 06:24
@olksdr
olksdr requested a review from a team October 11, 2022 06:24

@jjbayer jjbayer left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions to improve documentation, other than that, this looks good!

Comment thread relay-quotas/src/is_rate_limited.lua Resolved
Comment thread relay-quotas/src/is_rate_limited.lua Resolved
Comment thread relay-quotas/src/is_rate_limited.lua Resolved
Comment thread relay-quotas/src/is_rate_limited.lua Resolved
Comment thread relay-quotas/src/is_rate_limited.lua Outdated
Comment on lines +35 to +36
-- parse the incoming string into boolean, returns `nil` if could not parse which is also considered `false`
local function parse_boolean(v)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we agree to a protocol and not do these operations? They run quite often and it's expensive. For example, we could use t as truthy value and consider it falsy otherwise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, we could check for the string "true" instead (assuming that is what the rust redis library converts the argument to).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reduced this only to "true" and "false" but it does not really matter, we still have to have comparison to the string to get the boolean from that, either we assign this to the variable or do it in the if, there still will be that additional operation. IMHO having function and variable is much cleaner.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go lisp-style and use t for faster strcmp results :)

i'm not sure what the overheads are of a function in lua, but it is used only once to assign to a variable (which i agree is clearer to read) so could as well write the if statement right there.

Comment thread relay-quotas/src/redis.rs
quotas: &[Quota],
item_scoping: ItemScoping<'_>,
quantity: usize,
over_accept_once: bool,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: using a boolean makes reading the code calling this function harder to read, could we use an enum instead? An example is available in the processor.rs file in this diff. Example:

enum Overaccept {
    Once,
    No,
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point, but you could apply this reasoning to any boolean function arg. I'd rather keep it a bool and rely on IDEs to annotate the arguments you're passing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is totally my doing. and yes, i'd do this to any boolean arg :).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @jjbayer , this is overkill in here. If we had 3 and more values we want to accept that we could use enum for that, right now it's only 1 function with one marker flag.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one day you too will despise boolean args. but i'm not going to die on this hill today :)

Comment thread relay-quotas/src/redis.rs Resolved
@olksdr
olksdr requested a review from a team October 12, 2022 06:59
Comment thread relay-quotas/src/is_rate_limited.lua Outdated
Comment on lines +41 to +42
else
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this nil branch exist? it's used directly in a boolean expression which i assume will be treated like false. Might as well simplify to if ... true else false

Comment thread relay-quotas/src/is_rate_limited.lua Outdated
Comment on lines +35 to +36
-- parse the incoming string into boolean, returns `nil` if could not parse which is also considered `false`
local function parse_boolean(v)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go lisp-style and use t for faster strcmp results :)

i'm not sure what the overheads are of a function in lua, but it is used only once to assign to a variable (which i agree is clearer to read) so could as well write the if statement right there.

Comment thread relay-quotas/src/redis.rs
quotas: &[Quota],
item_scoping: ItemScoping<'_>,
quantity: usize,
over_accept_once: bool,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one day you too will despise boolean args. but i'm not going to die on this hill today :)

@olksdr
olksdr merged commit 4bc30c0 into master Oct 13, 2022
@olksdr
olksdr deleted the feat/quota-go-over branch October 13, 2022 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants