Class: DispatchPolicy::Gates::Throttle

Inherits:
DispatchPolicy::Gate show all
Defined in:
lib/dispatch_policy/gates/throttle.rb

Instance Attribute Summary

Attributes inherited from DispatchPolicy::Gate

#name, #partition_by, #policy

Instance Method Summary collapse

Methods inherited from DispatchPolicy::Gate

#initialize, #partition_key_for, register, registry

Constructor Details

This class inherits a constructor from DispatchPolicy::Gate

Instance Method Details

#configure(rate:, per:, burst: nil) ⇒ Object



6
7
8
9
10
# File 'lib/dispatch_policy/gates/throttle.rb', line 6

def configure(rate:, per:, burst: nil)
  @rate  = rate
  @per   = per
  @burst = burst
end

#filter(batch, context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dispatch_policy/gates/throttle.rb', line 17

def filter(batch, context)
  by_partition = batch.group_by { |staged| partition_key_for(context.for(staged)) }

  admitted = []
  # Sort keys before acquiring per-partition row locks: two ticks
  # processing overlapping partitions in different group_by orders
  # would otherwise deadlock on each other's FOR UPDATE rows.
  by_partition.keys.sort.each do |partition_key|
    jobs       = by_partition[partition_key]
    sample_ctx = context.for(jobs.first)
    rate       = resolve(@rate, sample_ctx).to_f
    per        = @per.to_f
    burst      = (resolve(@burst, sample_ctx) || rate).to_f

    bucket = ThrottleBucket.lock(
      policy_name:   policy.name,
      gate_name:     name,
      partition_key: partition_key,
      burst:         burst
    )
    bucket.refill!(rate: rate, per: per, burst: burst)

    jobs.each do |staged|
      if bucket.consume(1)
        admitted << [ staged, partition_key ]
      else
        break
      end
    end
    bucket.save!
  end

  context.record_partitions(admitted, gate: name)
  admitted.map(&:first)
end

#tracks_inflight?Boolean

Consumed tokens refill over time, no release step.

Returns:

  • (Boolean)


13
14
15
# File 'lib/dispatch_policy/gates/throttle.rb', line 13

def tracks_inflight?
  false
end