Files
firecrawl/apps/api/src/controllers/v1/concurrency-check.ts
T

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-10-01 16:04:39 -03:00
import { authenticateUser } from "../auth";
import {
ConcurrencyCheckParams,
ConcurrencyCheckResponse,
2024-12-11 19:51:08 -03:00
RequestWithAuth,
2024-10-01 16:04:39 -03:00
} from "./types";
import { RateLimiterMode, PlanType } from "../../types";
2024-10-01 16:04:39 -03:00
import { Response } from "express";
import { redisConnection } from "../../services/queue-service";
import { getConcurrencyLimitMax } from "../../services/rate-limiter";
2024-10-01 16:04:39 -03:00
// Basically just middleware and error wrapping
export async function concurrencyCheckController(
req: RequestWithAuth<ConcurrencyCheckParams, undefined, undefined>,
2024-12-11 19:51:08 -03:00
res: Response<ConcurrencyCheckResponse>,
2024-10-01 16:04:39 -03:00
) {
const concurrencyLimiterKey = "concurrency-limiter:" + req.auth.team_id;
2024-10-01 16:04:39 -03:00
const now = Date.now();
const activeJobsOfTeam = await redisConnection.zrangebyscore(
concurrencyLimiterKey,
now,
2024-12-11 19:51:08 -03:00
Infinity,
2024-10-01 16:04:39 -03:00
);
const maxConcurrency = getConcurrencyLimitMax(
req.auth.plan as PlanType,
req.auth.team_id,
);
return res.status(200).json({
success: true,
concurrency: activeJobsOfTeam.length,
maxConcurrency: maxConcurrency,
});
2024-10-01 16:04:39 -03:00
}