upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-vercel-kv.vercel.app
MIT License
1.65k stars 33 forks source link

Self-hosted Redis use? #81

Closed straygar closed 6 months ago

straygar commented 6 months ago

Can this library be used with a self-hosted Redis instance? Or is it only meant to integrate with the Upstash Redis service?

ogzhanolguncu commented 6 months ago

It should work with other providers as well. But, being serverless means it expects an endpoint and a token. I'm not sure how you'll provide those in a self-hosting setup, but it should be doable.

ytkimirti commented 6 months ago

You can look here for more info https://github.com/upstash/upstash-redis/issues/802

straygar commented 6 months ago

In my case I'd host it on AWS ElastiCache, so the endpoint is fine. However, looks like it expects an HTTP proxy, as described in this repo's README.

I've decided to go for the limits python package, since it seems that this lib is a no-go for my use-case.

Thanks all! :)

TimNZ commented 2 months ago

For anyone else looking for help:

I got this working with node-redis and wrapping with an adapter for a couple of functions. Initially I tried ioredis but I couldn't see the needed .scriptLoad function call by ratelimit and moved on.

import {createClient}  from '@redis/client' 
import { Ratelimit } from "@upstash/ratelimit"

const redisClient = createClient({url: 'redis://127.0.0.1:6379'})
redisClient.evalsha = (sha1,keys,args) => redisClient.evalSha(sha1,{keys,arguments: args.map(arg => arg.toString())})
redisClient.hset = redisClient.hSet.bind(redisClient)
await redisClient.connect()
const ratelimit = new Ratelimit({
            redis: redisClient,
            limiter: Ratelimit.slidingWindow(20,'100s'),
            prefix: 'api-ratelimit',
          })
redisClient.disconnect()