@chevre/domain
Version:
Chevre Domain Library for Node.js
91 lines (90 loc) • 4.26 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OfferRateLimitRepo = void 0;
const moment = require("moment");
const factory = require("../../factory");
const concurrentLock_1 = require("../concurrentLock");
/**
* オファーレート制限リポジトリ
*/
class OfferRateLimitRepo {
constructor(redisClient) {
this.concurrentLockRepo = new concurrentLock_1.ConcurrentLockRepo({ redisClient });
this.redisClient = redisClient;
}
static createKey(ratelimitKey) {
const dateNow = moment(ratelimitKey.reservationFor.startDate);
const unitInSeconds = Number(ratelimitKey.reservedTicket.ticketType.validRateLimit.unitInSeconds.toString());
const validFrom = dateNow.unix() - (dateNow.unix() % unitInSeconds);
// tslint:disable-next-line:max-line-length
return `${OfferRateLimitRepo.KEY_PREFIX_NEW}:${ratelimitKey.reservedTicket.ticketType.validRateLimit.scope}:${validFrom.toString()}`;
}
/**
* ロックする
* discontinue array params(2025-05-26~)
*/
// public async lock(ratelimitKeys: IRateLimitKey[]): Promise<void> {
lock(params) {
return __awaiter(this, void 0, void 0, function* () {
const key = OfferRateLimitRepo.createKey(params);
const value = params.reservationNumber;
const ttl = moment(params.reservationFor.startDate)
.add(params.reservedTicket.ticketType.validRateLimit.unitInSeconds, 'seconds')
.diff(moment(), 'seconds');
const expires = moment()
.add(ttl, 'seconds')
.toDate();
// reimplement using concurrentLockRepo(2025-05-27~)
try {
yield this.concurrentLockRepo.lock({
project: { id: params.project.id, typeOf: factory.organizationType.Project },
about: { identifier: key, typeOf: 'Thing' },
expires,
audience: { identifier: value, typeOf: 'Audience' }
});
}
catch (error) {
throw new factory.errors.RateLimitExceeded('Offer');
}
// const multi = this.redisClient.multi();
// multi.setNX(key, value)
// .expire(key, ttl);
// const results = await multi.exec();
// if (Array.isArray(results) && (results[0] === 1 || (<any>results)[0] === true)) {
// return;
// } else {
// throw new factory.errors.RateLimitExceeded('Offer');
// }
});
}
// discontinue array params(2025-05-26~)
// public async unlock(ratelimitKeys: IRateLimitKey[]): Promise<void> {
unlock(params) {
return __awaiter(this, void 0, void 0, function* () {
const key = OfferRateLimitRepo.createKey(params);
// reimplement using concurrentLockRepo(2025-05-27~)
yield this.concurrentLockRepo.unlock({
about: { identifier: key, typeOf: 'Thing' },
audience: { identifier: params.reservationNumber, typeOf: 'Audience' }
});
// await this.redisClient.del(key);
});
}
getHolder(ratelimitKey) {
return __awaiter(this, void 0, void 0, function* () {
const key = OfferRateLimitRepo.createKey(ratelimitKey);
return this.redisClient.get(key);
});
}
}
exports.OfferRateLimitRepo = OfferRateLimitRepo;
OfferRateLimitRepo.KEY_PREFIX_NEW = 'rateLimit:offer';
;