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.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mini_profiler/snapshots_transporter.rb', line 30

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.



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

def buffer
  @buffer
end

#gzip_requestsObject

Returns the value of attribute gzip_requests.



28
29
30
# File 'lib/mini_profiler/snapshots_transporter.rb', line 28

def gzip_requests
  @gzip_requests
end

#max_buffer_sizeObject

Returns the value of attribute max_buffer_size.



28
29
30
# File 'lib/mini_profiler/snapshots_transporter.rb', line 28

def max_buffer_size
  @max_buffer_size
end

Class Method Details

.failed_http_requests_countObject



17
18
19
# File 'lib/mini_profiler/snapshots_transporter.rb', line 17

def failed_http_requests_count
  @@failed_http_requests_count
end

.successful_http_requests_countObject



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

def successful_http_requests_count
  @@successful_http_requests_count
end

.transport(snapshot) ⇒ Object



21
22
23
24
# File 'lib/mini_profiler/snapshots_transporter.rb', line 21

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



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
88
89
# File 'lib/mini_profiler/snapshots_transporter.rb', line 51

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



91
92
93
# File 'lib/mini_profiler/snapshots_transporter.rb', line 91

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

#ship(snapshot) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/mini_profiler/snapshots_transporter.rb', line 43

def ship(snapshot)
  @buffer_mutex.synchronize do
    @buffer << snapshot
    @buffer.shift if @buffer.size > @max_buffer_size
  end
  @thread_mutex.synchronize { start_thread }
end