GithubHelp home page GithubHelp logo

upstash / lock Goto Github PK

View Code? Open in Web Editor NEW
18.0 6.0 2.0 136 KB

Home Page: https://lock-upstash.vercel.app

License: MIT License

TypeScript 98.47% JavaScript 1.53%
upstash upstash-redis upstash-sdk

lock's People

Contributors

buggyhunter avatar cahidarda avatar dev-ups avatar elibates avatar enesakar avatar mdogan avatar meshankhosla avatar ogzhanolguncu avatar williamoverton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lock's Issues

Remove crypto dependency

The import crypto makes this incompatible with worker environment. How about using uuid library instead and make it possible to pass a generateId() function as option?

How to set one hour in slidingWindow

Hey, I've been trying to setup my rate limiter like this:
const ratelimit = new Ratelimit({ redis: Redis.fromEnv(), limiter: Ratelimit.slidingWindow(2, '3600 s'), });
But it doesn't seem to work, there's a special way to reference an hour or theres a time limit?
Edit: sorry i posted this on the lock section without noticing

Facing typeError issue while using upstash lock

Problem:

I'm creating a chatapp where a user calls an api which returns a roomId ,but i was facing a problem where if two users call that api at same time which i dont want to happen as it leads to race condition,to solve i had used upstash lock but facing type error
Screenshot 2024-03-28 221904

component calling the api





 const Room = ({params}:roomProps) => {
    const {userId}= params
    const [roomId,setRoomId] = useState();
   

    async function getRoomId() {
      const API_BASE_URL =
   process.env.NEXT_PUBLIC_MODE === "production"? "https://blinkchat-nu.vercel.app": "http://localhost:3000";
  
      try {
        const { data } = await axios.get(
          `${API_BASE_URL}/api/roomIdExists/${userId}`
        );
        const value =await data.mssg;
        setRoomId(value);
      
      } catch (error) {
        console.error("Error fetching roomId:", error);
        setRoomId(undefined); // Reset roomId on error
      }
    }
    
    

 
//USING UPSTASH LOCK HERE
   useEffect(() => {
    async function fixRaceCondtion(){ 
      //since two browsers were hitting this api at same time it was causing race condition
  //to prevent that we are using lock system from redis

    const lock = new Lock({
      id:`userId`,
      lease: 2000,
      redis:client,
    });

console.log("console log",lock)

    if(await lock.acquire())
    {
       getRoomId();
       await lock.release()
      }
      else{ 
          console.log("lock not acquired")
      }
  }

  fixRaceCondtion()
    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [userId]);
    
 

    if (roomId === undefined) {
      return <div>Loading...</div>;
    }
  
 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.