Class: Rack::MiniProfiler::SnapshotsTransporter

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_profiler/snapshots_transporter.rb

Constant Summary collapse

@@transported_snapshots_count =
0
@@successful_http_requests_count =
0
@@failed_http_requests_count =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SnapshotsTransporter

Returns a new instance of SnapshotsTransporter.



28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



25
26
27
# File 'lib/mini_profiler/snapshots_transporter.rb', line 25

def buffer
  @buffer
end

#gzip_requestsObject

Returns the value of attribute gzip_requests.



26
27
28
# File 'lib/mini_profiler/snapshots_transporter.rb', line 26

def gzip_requests
  @gzip_requests
end

#max_buffer_sizeObject

Returns the value of attribute max_buffer_size.



26
27
28
# File 'lib/mini_profiler/snapshots_transporter.rb', line 26

def max_buffer_size
  @max_buffer_size
end

Class Method Details

.failed_http_requests_countObject



15
16
17
# File 'lib/mini_profiler/snapshots_transporter.rb', line 15

def failed_http_requests_count
  @@failed_http_requests_count
end

.successful_http_requests_countObject



12
13
14
# File 'lib/mini_profiler/snapshots_transporter.rb', line 12

def successful_http_requests_count
  @@successful_http_requests_count
end

.transport(snapshot) ⇒ Object



19
20
21
22
# File 'lib/mini_profiler/snapshots_transporter.rb', line 19

def transport(snapshot)
  @transporter ||= self.new(Rack::MiniProfiler.config)
  @transporter.ship(snapshot)
end

.transported_snapshots_countObject



9
10
11
# File 'lib/mini_profiler/snapshots_transporter.rb', line 9

def transported_snapshots_count
  @@transported_snapshots_count
end

Instance Method Details

#flush_bufferObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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_intervalObject



89
90
91
# File 'lib/mini_profiler/snapshots_transporter.rb', line 89

def requests_interval
  [30 + backoff_delay, 60 * 60].min
end

#ship(snapshot) ⇒ Object



41
42
43
44
45
46
47
# 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