Skip to content

Commit

Permalink
sleep for 0.1 seconds if timer group is empty (#21 #22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyal-Shalev authored Jun 2, 2019
1 parent 6656fc4 commit 5c1976f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lib/graphite-api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ class Client
def_delegator :buffer, :stream

attr_reader :options, :buffer, :connectors, :mu
private :options, :buffer, :connectors, :mu
private :options, :buffer, :connectors, :mu

def initialize opt
@options = build_options validate opt.clone
@buffer = GraphiteAPI::Buffer.new options, timers
@buffer = GraphiteAPI::Buffer.new options, timers
@connectors = GraphiteAPI::Connector::Group.new options
@mu = Mutex.new

timers.every(options[:interval], true, &method(:send_metrics!)) unless options[:direct]
end

def timers
@timers ||= Timers::Group.new.tap {|t| Thread.new { loop { t.wait } } }
@timers ||= Timers::Group.new.tap { |t| Thread.new { loop {
t.wait { |n| sleep(n.nil? ? 0.1 : [n, 0].max) }
} } }
end

# throw exception on Socket error
Expand All @@ -46,9 +48,9 @@ def metrics metric, time = nil, aggregation_method = nil
def increment(*keys)
opt = {}
opt.merge! keys.pop if keys.last.is_a? Hash
by = opt.fetch(:by,1)
time = opt.fetch(:time,Time.now)
metric = keys.inject({}) {|h,k| h.merge k => by }
by = opt.fetch(:by, 1)
time = opt.fetch(:time, Time.now)
metric = keys.inject({}) { |h, k| h.merge k => by }
metrics(metric, time)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/graphite-api/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GraphiteAPI
VERSION = "1.1.0"
VERSION = "1.1.1"
end

0 comments on commit 5c1976f

Please sign in to comment.