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

UpstashError: NOPERM this user has no perm issions to run the 'eval' command or its subcommand #18

Closed YashKarthik closed 1 year ago

YashKarthik commented 1 year ago

I'm trying to rate-limit a simple netlify serverless function. The function manages to return a response but still throws an error.

Here's the code for the function:

// /netlify/handler.ts
import * as dotenv from 'dotenv';
import { Handler, HandlerEvent, HandlerContext } from "@netlify/functions";
import { Configuration, OpenAIApi } from "openai";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

dotenv.config();

const configuration = new Configuration({
    apiKey: process.env.OPENAI_API_KEY
});
const openai = new OpenAIApi(configuration);

const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.fixedWindow(10, "20 s"),
});

const handler: Handler = async (event: HandlerEvent, _context: HandlerContext) => {

  const question = JSON.parse(event.body).body;

  // check if the selected text is useless.
  if (question === undefined || question.length < 7) {
    return {
      statusCode: 205,
      body: JSON.stringify({ message: "no text" }),
    }
  }

  // Upstash rate-limiter
  const identifier = "api";
  const success = ratelimit.limit(identifier)
    .then(e => { return e})
    .catch(e => { console.log('Error in upstash fetch:', e) }) // <---- error comes from here

  if (!success) {
    return {
      statusCode: 429,
      body: JSON.stringify({ message: "Woah! Slow down." }),
    }
  }

  try {
    const res = await openai.createCompletion({
      model: "text-curie-001",
      prompt: `:Say this is a test: """${question}"""`,
      max_tokens: 100,
      temperature: 0,
    });

    return {
      statusCode: 200,
      body: JSON.stringify({ message: res.data.choices[0].text }),
    }
  } catch (e) {
    console.log("ERROR has occured", e.response);
    return {
      statusCode: 500,
      body: JSON.stringify({ message: "Error" }),
    };
  }
};

export { handler };

Here's the stack trace:

Request from ::1: POST /.netlify/functions/gpt-summarize
(node:87001) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Error in upstash fetch: UpstashError: NOPERM this user has no permissions to run the 'eval' command or its subcommand
    at HttpClient.request (/Users/Yash/Code/summarize/node_modules/@upstash/redis/esm/pkg/http.js:93:19)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at EvalCommand.exec (/Users/Yash/Code/summarize/node_modules/@upstash/redis/esm/pkg/commands/command.js:46:35)
    at RegionRatelimit.limiter (/Users/Yash/Code/summarize/node_modules/@upstash/ratelimit/esm/single.js:88:44)
    at RegionRatelimit.value (/Users/Yash/Code/summarize/node_modules/@upstash/ratelimit/esm/ratelimit.js:62:24)
Response with status 200 in 1763 ms.

After I catch the error, it manages to give me a response though.

Let me know if this a ratelimiter error or if I report it at upstash-redis.

Cheers!

chronark commented 1 year ago

Hey @YashKarthik sorry I didn't notice this issue, I just added an alert for myself in the future.

The error you pasted says NOPERM this user has no permissions to run the 'eval' command or its subcommand.

I don't think this is an issue with ratelimit nor the redis sdk.

Can you double check your ACL permissions?

chronark commented 1 year ago

Closing this as it looks like a configuration error, please reopen if necessary

jpwogaman commented 1 year ago

I solved this issue by making sure I do NOT use "read-only token" in the REST API section of the Upstash Details dashboard.