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

Hardcode script hash values and deprecate cacheScripts #120

Merged
merged 2 commits into from
Sep 10, 2024
Merged

Conversation

CahidArda
Copy link
Contributor

Previously, we had the cacheScripts parameter which allowed us to define the script running behavior of the sdk. The two options were:

  1. cacheScripts: true (defaul): load the script to redis on cold start. In the other runs, use the hash with EVALSHA command. Resulted in two round trips in cold start.
    2.cacheScripts: false: simply send the script with EVAL command. More bandwidth.

Now, we deprecate the cacheScripts parameter. It no longer has any affect. We also change the two behaviors:

  • We hardcode the hash values to the sdk.
  • We call redis with the hash values in cold/hot starts.
  • If the EVALSHA fails because the script is missing at redis, we load the script and continue.

Copy link

vercel bot commented Sep 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ratelimit-with-vercel-kv ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 10, 2024 10:39am

Copy link

linear bot commented Sep 9, 2024

@CahidArda CahidArda changed the title Hardcode script hashes and use them in all requests Hardcode script hash values and use them in all requests Sep 9, 2024
@CahidArda CahidArda changed the title Hardcode script hash values and use them in all requests Hardcode script hash values and deprecate cacheScripts Sep 9, 2024
@CahidArda CahidArda marked this pull request as ready for review September 10, 2024 03:31
src/hash.ts Outdated
await setHash(ctx, script, kind)
console.log(" New script successfully loaded.")
return await ctx.redis.evalsha(ctx.scriptHashes[kind]!, keys, args)
console.log(
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really when these to be printed by default? Maybe we should have a debug config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the logs, but kept the warning below. Warning should never be logged, so if it does I think we can print by default #120 (comment)


console.log("Upstash Ratelimit: Script loaded successfully.");

if (hash !== script.hash) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the case where script itself it updated, but we haven't updated the constant script hash on sdk, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, because even if I update in the sdk, it will happen again in the next cold start.

This if condition should never be true though. It essentially means that the hashing algorithm of redis changed, which should never happen.

This is just a failsafe.

@CahidArda CahidArda merged commit 377f72c into main Sep 10, 2024
3 checks passed
@CahidArda CahidArda deleted the DX-1116 branch September 10, 2024 11:48
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.

2 participants