mirror of
https://git.mirrors.martin98.com/https://github.com/mendableai/firecrawl
synced 2025-08-13 17:39:00 +08:00
Nick: 10min cooldown on auto charge
This commit is contained in:
parent
4590577cba
commit
95c4652fd4
@ -7,12 +7,13 @@ import { createPaymentIntent } from "./stripe";
|
|||||||
import { issueCredits } from "./issue_credits";
|
import { issueCredits } from "./issue_credits";
|
||||||
import { sendNotification } from "../notification/email_notification";
|
import { sendNotification } from "../notification/email_notification";
|
||||||
import { NotificationType } from "../../types";
|
import { NotificationType } from "../../types";
|
||||||
import { deleteKey } from "../redis";
|
import { deleteKey, getValue, setValue } from "../redis";
|
||||||
import { sendSlackWebhook } from "../alerts/slack";
|
import { sendSlackWebhook } from "../alerts/slack";
|
||||||
import { Logger } from "../../lib/logger";
|
import { Logger } from "../../lib/logger";
|
||||||
|
|
||||||
// Define the number of credits to be added during auto-recharge
|
// Define the number of credits to be added during auto-recharge
|
||||||
const AUTO_RECHARGE_CREDITS = 1000;
|
const AUTO_RECHARGE_CREDITS = 1000;
|
||||||
|
const AUTO_RECHARGE_COOLDOWN = 600; // 10 minutes in seconds
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempt to automatically charge a user's account when their credit balance falls below a threshold
|
* Attempt to automatically charge a user's account when their credit balance falls below a threshold
|
||||||
@ -24,7 +25,22 @@ export async function autoCharge(
|
|||||||
autoRechargeThreshold: number
|
autoRechargeThreshold: number
|
||||||
): Promise<{ success: boolean; message: string; remainingCredits: number; chunk: AuthCreditUsageChunk }> {
|
): Promise<{ success: boolean; message: string; remainingCredits: number; chunk: AuthCreditUsageChunk }> {
|
||||||
const resource = `auto-recharge:${chunk.team_id}`;
|
const resource = `auto-recharge:${chunk.team_id}`;
|
||||||
|
const cooldownKey = `auto-recharge-cooldown:${chunk.team_id}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Check if the team is in the cooldown period
|
||||||
|
// Another check to prevent race conditions, double charging - cool down of 10 minutes
|
||||||
|
const cooldownValue = await getValue(cooldownKey);
|
||||||
|
if (cooldownValue) {
|
||||||
|
Logger.info(`Auto-recharge for team ${chunk.team_id} is in cooldown period`);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Auto-recharge is in cooldown period",
|
||||||
|
remainingCredits: chunk.remaining_credits,
|
||||||
|
chunk,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Use a distributed lock to prevent concurrent auto-charge attempts
|
// Use a distributed lock to prevent concurrent auto-charge attempts
|
||||||
return await redlock.using([resource], 5000, async (signal) : Promise<{ success: boolean; message: string; remainingCredits: number; chunk: AuthCreditUsageChunk }> => {
|
return await redlock.using([resource], 5000, async (signal) : Promise<{ success: boolean; message: string; remainingCredits: number; chunk: AuthCreditUsageChunk }> => {
|
||||||
// Recheck the condition inside the lock to prevent race conditions
|
// Recheck the condition inside the lock to prevent race conditions
|
||||||
@ -89,6 +105,9 @@ export async function autoCharge(
|
|||||||
chunk,
|
chunk,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Set cooldown period
|
||||||
|
await setValue(cooldownKey, 'true', AUTO_RECHARGE_COOLDOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset ACUC cache to reflect the new credit balance
|
// Reset ACUC cache to reflect the new credit balance
|
||||||
|
Loading…
x
Reference in New Issue
Block a user