Interface SmsCostLedgerRepository
public interface SmsCostLedgerRepository
Port that keeps the ledger of what text messages cost, in the database so that a restart cannot reset a budget.
-
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.
-
Method Details
-
tryRecord
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. Nothing is recorded for a refused message. It runs in the caller's transaction and the reservation is kept only if that transaction commits.- 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
Deletes the entries older than an instant.- Parameters:
before- instant before which entries are no longer needed.- Returns:
- the number of deleted entries.
-