unkeyed / unkey

Open source API management platform
https://go.unkey.com
Other
3.97k stars 464 forks source link

Allow dynamic error handling and dynamic timeout fallback via the ratelimit SDK #1912

Closed AlessandroGasparri closed 2 months ago

AlessandroGasparri commented 3 months ago

Preliminary Checks

Is your feature request related to a problem? Please describe.

Currently, when using the ratelimit SDK, the behavior upon hitting an error is to either:

I would like to dynamically handle this scenarios to implement another mechanism of rate limiting in case something is wrong with the network or with the request.

Describe the solution

The solution I've come up with is pretty simple:

Describe alternatives you have considered (if any)

No response

Additional context

No response

linear[bot] commented 3 months ago

ENG-1243 Allow dynamic error handling and dynamic timeout fallback via the SDK of ratelimit requests

chronark commented 3 months ago

Hey, could you add a code snippet of an example how you'd like to use this?

AlessandroGasparri commented 3 months ago

Hi, thank you for your response and sorry for the delay, i missed the email. I was thinking something like:

new Ratelimit({
  limit: 5,
  duration: '1d',
  namespace: 'namespace',
  rootKey: process.env.UNKEY_ROOT_KEY!,
  timeout: {
    ms: 3000,
    fallback: (resourceIdentifier) => {
      // Additional logic here with resourceIdentifier
      return { success: true, limit: 0, remaining: 0, reset: 0 }
    }
  },
  onError: (resourceIdentifier, error) => {
    // Additional logic here with resourceIdentifier and error
    return { success: true, limit: 0, remaining: 0, reset: 0 }
  }
})
chronark commented 3 months ago

Thank you, that makes it clearer.

Sounds like a great idea, would you want to tackle this one?

AlessandroGasparri commented 3 months ago

Sure! I'll provide a PR and link it to this issue once I have it ready

AlessandroGasparri commented 2 months ago

Hi! Sorry for the delay, I have been busy at work. I've opened the PR #1998.

AlessandroGasparri commented 2 months ago

Hi, thank you for approving the pull request! We need these changes in our codebase. Could you please let me know the expected timeframe for the release to be published on npm?

chronark commented 2 months ago

@AlessandroGasparri today, I'll ping you

chronark commented 2 months ago

@AlessandroGasparri v0.4.0 is released