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";
|
2025-01-30 13:47:29 -03:00
|
|
|
import { RateLimiterMode, PlanType } from "../../types";
|
2024-10-01 16:04:39 -03:00
|
|
|
import { Response } from "express";
|
|
|
|
|
import { redisConnection } from "../../services/queue-service";
|
2025-01-30 13:47:29 -03:00
|
|
|
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
|
|
|
) {
|
2024-10-01 16:11:12 -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
|
|
|
);
|
2025-01-30 13:47:29 -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
|
|
|
}
|