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

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    Statement deleting the rows older than an instant.
    private static final String
    Statement 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.JdbcClient
    JDBC client used to run the statements.
    private static final String
    Statement taking the advisory lock, released when the caller's transaction ends.
    (package private) static final long
    Key of the advisory lock that serialises the reservations of SMS spend.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Deletes the entries older than an instant.
    boolean
    tryRecord(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SPEND_LOCK_KEY

      static final long SPEND_LOCK_KEY
      Key of the advisory lock that serialises the reservations of SMS spend.
      See Also:
    • LOCK

      private static final String LOCK
      Statement taking the advisory lock, released when the caller's transaction ends.
      See Also:
    • INSERT_WITHIN_CAPS

      private static final String 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 default READ COMMITTED isolation) it sees every row a previous holder of the lock committed.
      See Also:
    • DELETE_OLDER_THAN

      private static final String DELETE_OLDER_THAN
      Statement deleting the rows older than an instant.
      See Also:
    • jdbcClient

      private final org.springframework.jdbc.core.simple.JdbcClient jdbcClient
      JDBC 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: SmsCostLedgerRepository
      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. 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:
      tryRecord in interface SmsCostLedgerRepository
      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 at sentAt.
      dailyCap - cap of the day that ends at sentAt.
      Returns:
      true when the message was recorded; false when it would exceed a cap.
    • deleteOlderThan

      public int deleteOlderThan(Instant before)
      Description copied from interface: SmsCostLedgerRepository
      Deletes the entries older than an instant.
      Specified by:
      deleteOlderThan in interface SmsCostLedgerRepository
      Parameters:
      before - instant before which entries are no longer needed.
      Returns:
      the number of deleted entries.