instrumental_agent 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5faedb2800827d3604f4a6c0b13bd33234d55ec
4
- data.tar.gz: a0115cc25b12d9749dcfeef580bfb26734001e21
3
+ metadata.gz: 32d9f3dab94080e4c8194ab8c71e0ddf4d83bb8a
4
+ data.tar.gz: fa78452258ca2f450d5a6da975a5c1d0a114902a
5
5
  SHA512:
6
- metadata.gz: 282bb711342714bdb4be0a1afec481f39bb6fd9fce58f001a9ba662ae10e17ddade912743b85bcf87a8f026b8db0c873d420e9ecffcb213cb5e047a018794886
7
- data.tar.gz: a3114f1773d7442e74d9775906324eb8717ad799980668e0dc0be7a1f754196c34331f9859603249c115f748a7934248762329f275f6022135837019524d8f5d
6
+ metadata.gz: 5174c7be573fab85d465dc2a6db07d35a62b7bcae160668fc4182da5fefc795930911392c556fa71af0a210723a5534c8548305f2de74c1ccbaae40d535ae448
7
+ data.tar.gz: 69d4ad769e79161afa6d0c915c9a2b21835a010fc5452a975cfa438484f61906e4474e366c845ce1f46443b3effeba6b737c2f738dfaadfad9a7441604fc05f6
@@ -1,3 +1,6 @@
1
+ ### 1.0.1 [July 12, 2016]
2
+ * Make agent initialization threadsafe
3
+
1
4
  ### 1.0.0 [May 13, 2016]
2
5
  * No significant code changes
3
6
 
@@ -77,6 +77,8 @@ module Instrumental
77
77
  @certs = certificates
78
78
  @dns_resolutions = 0
79
79
  @last_connect_at = 0
80
+ @start_worker_mutex = Mutex.new
81
+ @queue = Queue.new
80
82
 
81
83
  setup_cleanup_at_exit if @enabled
82
84
  end
@@ -287,7 +289,7 @@ module Instrumental
287
289
  cmd = "%s %s\n" % [cmd, args.collect { |a| a.to_s }.join(" ")]
288
290
  if enabled?
289
291
 
290
- start_connection_worker if !running?
292
+ start_connection_worker
291
293
  if @queue && @queue.size < MAX_BUFFER
292
294
  @queue_full_warning = false
293
295
  logger.debug "Queueing: #{cmd.chomp}"
@@ -365,9 +367,15 @@ module Instrumental
365
367
  end
366
368
 
367
369
  def start_connection_worker
368
- if enabled?
370
+ # NOTE: We need a mutex around both `running?` and thread creation,
371
+ # otherwise we could create two threads.
372
+ # Return early and queue the message if another thread is
373
+ # starting the worker.
374
+ return if !@start_worker_mutex.try_lock
375
+ begin
376
+ return if running?
377
+ return unless enabled?
369
378
  disconnect
370
- @queue ||= Queue.new
371
379
  address = ipv4_address_for_host(@host, @port)
372
380
  if address
373
381
  @pid = Process.pid
@@ -379,6 +387,8 @@ module Instrumental
379
387
  run_worker_loop
380
388
  end
381
389
  end
390
+ ensure
391
+ @start_worker_mutex.unlock
382
392
  end
383
393
  end
384
394
 
@@ -1,3 +1,3 @@
1
1
  module Instrumental
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -235,6 +235,25 @@ shared_examples "Instrumental Agent" do
235
235
  end
236
236
  end
237
237
 
238
+ it "shouldn't start multiple background threads" do
239
+ # Force a wait that would cause a race condition
240
+ allow(agent).to receive(:disconnect) {
241
+ sleep 1
242
+ }
243
+
244
+ run_worker_loop_calls = 0
245
+ allow(agent).to receive(:run_worker_loop) {
246
+ run_worker_loop_calls += 1
247
+ sleep 3 # keep the worker thread alive
248
+ }
249
+
250
+ t = Thread.new { agent.increment("race") }
251
+ agent.increment("race")
252
+ wait(2)
253
+ expect(run_worker_loop_calls).to eq(1)
254
+ expect(agent.queue.size).to eq(2)
255
+ end
256
+
238
257
  it "should never let an exception reach the user" do
239
258
  expect(agent).to receive(:send_command).twice { raise(Exception.new("Test Exception")) }
240
259
  expect(agent.increment('throws_exception', 2)).to eq(nil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: instrumental_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elijah Miller
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2016-05-13 00:00:00.000000000 Z
14
+ date: 2016-07-12 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: pry