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) booleantryRecord(Instant sentAt, String country, long costMicros, SmsSpendCap windowCap, SmsSpendCap dailyCap)
-
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) - Specified by:
tryRecordin interfaceSmsCostLedgerRepository
-
deleteOlderThan
- Specified by:
deleteOlderThanin interfaceSmsCostLedgerRepository
-