Class: Amigo::Autoscaler::Checkers::PumaPoolUsage

Inherits:
Amigo::Autoscaler::Checker show all
Defined in:
lib/amigo/autoscaler/checkers/puma_pool_usage.rb

Constant Summary collapse

NAMESPACE =
"amigo/autoscaler/puma_pool_usage"
MIN_READINGS =

The minimum number of usage readings before we report pool usage, to avoid spikes.

2
WINDOW =

How long to track the pool usage.

60

Instance Method Summary collapse

Constructor Details

#initialize(redis:, namespace: NAMESPACE, uid: SecureRandom.base64(4).delete_suffix("=")) ⇒ PumaPoolUsage

Returns a new instance of PumaPoolUsage.



18
19
20
21
22
23
# File 'lib/amigo/autoscaler/checkers/puma_pool_usage.rb', line 18

def initialize(redis:, namespace: NAMESPACE, uid: SecureRandom.base64(4).delete_suffix("="))
  @redis = redis
  @key = "#{namespace}/v1"
  @uid = uid
  super()
end

Instance Method Details

#get_latenciesObject



35
# File 'lib/amigo/autoscaler/checkers/puma_pool_usage.rb', line 35

def get_latencies = {}

#get_pool_usageObject



37
38
39
40
41
42
43
44
# File 'lib/amigo/autoscaler/checkers/puma_pool_usage.rb', line 37

def get_pool_usage
  now = Time.now.to_f
  members = @redis.call("ZRANGE", @key, now - WINDOW, now, "BYSCORE")
  return nil if members.size < MIN_READINGS
  values = members.map { |m| m.split(":", 2).first }
  total_usage = values.sum(0, &:to_f)
  return total_usage / values.size
end

#record(value, now:) ⇒ Object

Set the pool usage, and trim old metrics.



26
27
28
29
30
31
32
33
# File 'lib/amigo/autoscaler/checkers/puma_pool_usage.rb', line 26

def record(value, now:)
  ts = now.to_f
  member = "#{value}:#{@uid}:#{now.to_i}"
  @redis.pipelined do |pipeline|
    pipeline.call("ZADD", @key, ts, member)
    pipeline.call("ZREMRANGEBYSCORE", @key, 0, ts - WINDOW)
  end
end