upstash / lock Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://lock-upstash.vercel.app
License: MIT License
Home Page: https://lock-upstash.vercel.app
License: MIT License
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?
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
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
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>;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.