class Kiev::SubrequestHelper

Public Class Methods

headers(metadata: false) click to toggle source
# File lib/kiev/subrequest_helper.rb, line 8
def headers(metadata: false)
  Config.instance.all_http_propagated_fields.map do |key, http_key|
    field = field_value(key, true)
    [metadata ? key : http_key, field.to_s] if field
  end.compact.to_h
end
payload() click to toggle source
# File lib/kiev/subrequest_helper.rb, line 15
def payload
  Config.instance.all_jobs_propagated_fields.map do |key|
    field = field_value(key, false)
    [key.to_s, field] if field
  end.compact.to_h
end
root_path(synchronous:) click to toggle source
# File lib/kiev/subrequest_helper.rb, line 22
def root_path(synchronous:)
  encode(0, synchronous)
end
subrequest_path(synchronous:) click to toggle source
# File lib/kiev/subrequest_helper.rb, line 26
def subrequest_path(synchronous:)
  current_path + encode(counter, synchronous)
end

Private Class Methods

counter() click to toggle source
# File lib/kiev/subrequest_helper.rb, line 50
def counter
  if RequestStore.store[:subrequest_count]
    # generally this is not atomic operation,
    # but because RequestStore.store is tied to current thread this is ok
    RequestStore.store[:subrequest_count] += 1
  else
    RequestStore.store[:subrequest_count] = 0
  end
end
current_path() click to toggle source
# File lib/kiev/subrequest_helper.rb, line 46
def current_path
  RequestStore.store[:tree_path] || ""
end
encode(value, synchronous) click to toggle source
# File lib/kiev/subrequest_helper.rb, line 41
def encode(value, synchronous)
  # this scheme can encode up to 26 consequent requests (synchronous or asynchronous)
  Base52.encode(value * 2 + (synchronous ? 0 : 1))
end
field_value(key, synchronous) click to toggle source
# File lib/kiev/subrequest_helper.rb, line 32
def field_value(key, synchronous)
  if key == :tree_path
    subrequest_path(synchronous: synchronous)
  else
    request_store = Kiev::RequestStore.store
    request_store.dig(key) || request_store.dig(:payload, key)
  end
end