Class: DynoscaleRuby::RequestCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/dynoscale_ruby/request_calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ RequestCalculator

Returns a new instance of RequestCalculator.



4
5
6
# File 'lib/dynoscale_ruby/request_calculator.rb', line 4

def initialize(env)
  @env = env
end

Instance Method Details

#request_queue_time(time_now = Time.now) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dynoscale_ruby/request_calculator.rb', line 8

def request_queue_time(time_now = Time.now)
  is_dev = ENV['DYNOSCALE_DEV'] == 'true'

  if is_dev
    request_start = "#{Time.now - (rand*100).ceil}"
  else
    request_start = @env['HTTP_X_REQUEST_START']
  end
  raise MissingRequestStartError if request_start.nil?
  
  request_body_wait = @env['puma.request_body_wait'] || 0

  request_start_string = request_start.match(/([0-9])+/)&.[](0)
  start_at = Time.at(request_start_string.to_i / 1000)

  (time_now - start_at).to_i + request_body_wait.to_i
end