Class: Insika::UsageLedger

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/usage_ledger.rb

Overview

Fixed-window counters on the KV Store (item 33 / §12 G7): the durable side of the edge limits. One scope, keys shaped "kind:id:window_start" — the window start bucketed on the epoch keeps every process/worker on the SAME bucket without coordination (the Store's transaction serializes the read-modify-write, in-process via the fiber semaphore and cross-process via BEGIN IMMEDIATE).

Growth is bounded: each add garbage-collects the (kind, id) pair's PREVIOUS window key, so an active entity holds at most 2 keys at any time and an idle one converges to 1.

Constant Summary collapse

SCOPE =
"usage_counters"

Instance Method Summary collapse

Constructor Details

#initialize(store:) ⇒ UsageLedger

Returns a new instance of UsageLedger.



16
17
18
# File 'lib/insika/usage_ledger.rb', line 16

def initialize(store:)
  @store = store
end

Instance Method Details

#add(kind, id, window:, by: 1, now: Time.now) ⇒ Object

Adds by to the current window's counter of (kind, id) -> the NEW total.



21
22
23
24
25
26
27
28
29
30
# File 'lib/insika/usage_ledger.rb', line 21

def add(kind, id, window:, by: 1, now: Time.now)
  start = window_start(now, window)
  key = key_for(kind, id, start)
  @store.transaction do
    total = @store.get(SCOPE, key).to_i + by
    @store.set(SCOPE, key, total)
    @store.delete(SCOPE, key_for(kind, id, start - window))
    total
  end
end

#count(kind, id, window:, now: Time.now) ⇒ Object

Current window's total for (kind, id). Missing/expired -> 0.



33
34
35
# File 'lib/insika/usage_ledger.rb', line 33

def count(kind, id, window:, now: Time.now)
  @store.get(SCOPE, key_for(kind, id, window_start(now, window))).to_i
end