Skip to content

Commit

Permalink
Merge pull request #6 from trobrock/timeout
Browse files Browse the repository at this point in the history
Add configurable timeouts
  • Loading branch information
trobrock committed Jul 7, 2013
2 parents d2fb5cf + 4302eb3 commit 528ac08
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Configure the Slate client
```ruby
client = Slate.configure do |config|
config.endpoint = "http://your.graphite-server.com"
config.timeout = 30 # In seconds (default: 10)
end
```

Expand Down
1 change: 1 addition & 0 deletions lib/slate.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "slate/version"
require "slate/error"
require "slate/client"
require "slate/target"
require "slate/graph"
Expand Down
2 changes: 1 addition & 1 deletion lib/slate/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Slate
class Client
# Public: Gets/Sets the URL endpoint of the Graphite server.
attr_accessor :endpoint
attr_accessor :endpoint, :timeout
end
end
5 changes: 5 additions & 0 deletions lib/slate/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Slate
module Error
class TimeoutError < StandardError ; end
end
end
13 changes: 11 additions & 2 deletions lib/slate/graph.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'cgi'
require 'rest-client'
require 'faraday'

module Slate
class Graph
Expand Down Expand Up @@ -65,11 +65,20 @@ def url(format=:png)
# download(:json)
# # => '{"targets":[]}'
def download(format=:png)
RestClient.get url(format)
connection.get(url(format)).body
rescue Faraday::Error::TimeoutError
raise Slate::Error::TimeoutError
end

private

def connection
@connection ||= Faraday.new do |faraday|
faraday.options[:timeout] = @client.timeout || 10
faraday.adapter Faraday.default_adapter
end
end

def url_options
options = []
options += @targets.map { |t| ["target", t.to_s] }
Expand Down
6 changes: 3 additions & 3 deletions slate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency "rest-client", "~> 1.6.7"
gem.add_dependency "json", "~> 1.7.5"
gem.add_dependency "faraday", "~> 0.8"
gem.add_dependency "json", "~> 1.8"
gem.add_dependency "jruby-openssl" if RUBY_PLATFORM == 'java'
gem.add_dependency "treetop", "~> 1.4.12"
gem.add_dependency "treetop", "~> 1.4"

gem.add_development_dependency "rake"
gem.add_development_dependency "rspec"
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/slate/graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@
graph.download(:json).should eq(@json_stub)
graph.download(:svg).should eq(@svg_stub)
end

it "should respect the configured timeout" do
stub_request(:get, "http://graphite/render?format=png&target=app.server01.timeout").
to_timeout

target = Slate::Target.build("app.server01.timeout")
graph = Slate::Graph.new(@client)
graph << target

expect {
graph.download(:png).should eq(@png_stub)
}.to raise_error(Slate::Error::TimeoutError)
end
end

def stub_download(format, body="")
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/slate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@

client.endpoint.should == "http://graphite"
end

it "should be able to configure the request timeout" do
client = Slate.configure do |c|
c.timeout = 30
end

client.timeout.should == 30
end
end

0 comments on commit 528ac08

Please sign in to comment.