class Rack::MiniProfiler::SnapshotsTransporter

Attributes

buffer[R]
gzip_requests[RW]
max_buffer_size[RW]

Public Class Methods

failed_http_requests_count() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 15
def failed_http_requests_count
  @@failed_http_requests_count
end
new(config) click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 28
def initialize(config)
  @uri = URI(config.snapshots_transport_destination_url)
  @auth_key = config.snapshots_transport_auth_key
  @gzip_requests = config.snapshots_transport_gzip_requests
  @thread = nil
  @thread_mutex = Mutex.new
  @buffer = []
  @buffer_mutex = Mutex.new
  @max_buffer_size = 100
  @consecutive_failures_count = 0
  @testing = false
end
successful_http_requests_count() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 12
def successful_http_requests_count
  @@successful_http_requests_count
end
transport(snapshot) click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 19
def transport(snapshot)
  @transporter ||= self.new(Rack::MiniProfiler.config)
  @transporter.ship(snapshot)
end
transported_snapshots_count() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 9
def transported_snapshots_count
  @@transported_snapshots_count
end

Public Instance Methods

flush_buffer() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 49
def flush_buffer
  buffer_content = @buffer_mutex.synchronize do
    @buffer.dup if @buffer.size > 0
  end
  if buffer_content
    headers = {
      'Content-Type' => 'application/json',
      'Mini-Profiler-Transport-Auth' => @auth_key
    }
    json = { snapshots: buffer_content }.to_json
    body = if @gzip_requests
      require 'zlib'
      io = StringIO.new
      gzip_writer = Zlib::GzipWriter.new(io)
      gzip_writer.write(json)
      gzip_writer.close
      headers['Content-Encoding'] = 'gzip'
      io.string
    else
      json
    end
    request = Net::HTTP::Post.new(@uri, headers)
    request.body = body
    http = Net::HTTP.new(@uri.hostname, @uri.port)
    http.use_ssl = @uri.scheme == 'https'
    res = http.request(request)
    if res.code.to_i == 200
      @@successful_http_requests_count += 1
      @@transported_snapshots_count += buffer_content.size
      @buffer_mutex.synchronize do
        @buffer -= buffer_content
      end
      @consecutive_failures_count = 0
    else
      @@failed_http_requests_count += 1
      @consecutive_failures_count += 1
    end
  end
end
requests_interval() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 89
def requests_interval
  [30 + backoff_delay, 60 * 60].min
end
ship(snapshot) click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 41
def ship(snapshot)
  @buffer_mutex.synchronize do
    @buffer << snapshot
    @buffer.shift if @buffer.size > @max_buffer_size
  end
  @thread_mutex.synchronize { start_thread }
end

Private Instance Methods

backoff_delay() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 95
def backoff_delay
  return 0 if @consecutive_failures_count == 0
  2**@consecutive_failures_count
end
start_thread() click to toggle source
# File lib/mini_profiler/snapshots_transporter.rb, line 100
def start_thread
  return if @thread&.alive? || @testing
  @thread = Thread.new do
    while true
      sleep requests_interval
      flush_buffer
    end
  end
end