Class JdbcSmsCostLedgerRepository
java.lang.Object
com.irurueta.authserver.authorization.infrastructure.persistence.sms.JdbcSmsCostLedgerRepository
- All Implemented Interfaces:
SmsCostLedgerRepository
@Repository
public class JdbcSmsCostLedgerRepository
extends Object
implements SmsCostLedgerRepository
SmsCostLedgerRepository over the sms_cost_ledger table. It opens no transaction: a reservation runs
in the caller's, where it first takes a PostgreSQL transaction-level advisory lock, so that concurrent senders (of
this or another instance) check and record their messages one after the other and a cap is never overshot. The
check and the insert are one conditional statement, so a refused message leaves no row. A refusal is logged, without
the number or the country, so that an exhausted budget can be seen.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringStatement deleting the rows older than an instant.private static final StringStatement inserting a row only when the spend of each window, the new row included, stays within its cap.private final org.springframework.jdbc.core.simple.JdbcClientJDBC client used to run the statements.private static final StringStatement taking the advisory lock, released when the caller's transaction ends.(package private) static final longKey of the advisory lock that serialises the reservations of SMS spend. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintdeleteOlderThan(Instant before) Deletes the entries older than an instant.booleantryRecord(Instant sentAt, String country, long costMicros, SmsSpendCap windowCap, SmsSpendCap dailyCap) Records a message only when it keeps the spend within both caps, atomically: of concurrent callers, the ones whose messages would together exceed a cap are refused, so a budget is never overshot.
-
Field Details
-
SPEND_LOCK_KEY
static final long SPEND_LOCK_KEYKey of the advisory lock that serialises the reservations of SMS spend.- See Also:
-
LOCK
Statement taking the advisory lock, released when the caller's transaction ends.- See Also:
-
INSERT_WITHIN_CAPS
Statement inserting a row only when the spend of each window, the new row included, stays within its cap. It runs after the lock is taken, so (in the defaultREAD COMMITTEDisolation) it sees every row a previous holder of the lock committed.- See Also:
-
DELETE_OLDER_THAN
Statement deleting the rows older than an instant.- See Also:
-
jdbcClient
private final org.springframework.jdbc.core.simple.JdbcClient jdbcClientJDBC client used to run the statements.
-
-
Constructor Details
-
JdbcSmsCostLedgerRepository
public JdbcSmsCostLedgerRepository()
-
-
Method Details
-
tryRecord
public boolean tryRecord(Instant sentAt, String country, long costMicros, SmsSpendCap windowCap, SmsSpendCap dailyCap) Description copied from interface:SmsCostLedgerRepositoryRecords a message only when it keeps the spend within both caps, atomically: of concurrent callers, the ones whose messages would together exceed a cap are refused, so a budget is never overshot. Nothing is recorded for a refused message. It runs in the caller's transaction and the reservation is kept only if that transaction commits.- Specified by:
tryRecordin interfaceSmsCostLedgerRepository- Parameters:
sentAt- instant of the message.country- ISO 3166-1 alpha-2 code of the destination country.costMicros- cost of the message, in millionths of the budget currency.windowCap- cap of the short window that ends atsentAt.dailyCap- cap of the day that ends atsentAt.- Returns:
truewhen the message was recorded;falsewhen it would exceed a cap.
-
deleteOlderThan
Description copied from interface:SmsCostLedgerRepositoryDeletes the entries older than an instant.- Specified by:
deleteOlderThanin interfaceSmsCostLedgerRepository- Parameters:
before- instant before which entries are no longer needed.- Returns:
- the number of deleted entries.
-