Skip to content

Commit 528ac08

Browse files
committed
Merge pull request #6 from trobrock/timeout
Add configurable timeouts
2 parents d2fb5cf + 4302eb3 commit 528ac08

File tree

8 files changed

+43
-6
lines changed

8 files changed

+43
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Configure the Slate client
2929
```ruby
3030
client = Slate.configure do |config|
3131
config.endpoint = "http://your.graphite-server.com"
32+
config.timeout = 30 # In seconds (default: 10)
3233
end
3334
```
3435

lib/slate.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "slate/version"
2+
require "slate/error"
23
require "slate/client"
34
require "slate/target"
45
require "slate/graph"

lib/slate/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Slate
22
class Client
33
# Public: Gets/Sets the URL endpoint of the Graphite server.
4-
attr_accessor :endpoint
4+
attr_accessor :endpoint, :timeout
55
end
66
end

lib/slate/error.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Slate
2+
module Error
3+
class TimeoutError < StandardError ; end
4+
end
5+
end

lib/slate/graph.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'cgi'
2-
require 'rest-client'
2+
require 'faraday'
33

44
module Slate
55
class Graph
@@ -65,11 +65,20 @@ def url(format=:png)
6565
# download(:json)
6666
# # => '{"targets":[]}'
6767
def download(format=:png)
68-
RestClient.get url(format)
68+
connection.get(url(format)).body
69+
rescue Faraday::Error::TimeoutError
70+
raise Slate::Error::TimeoutError
6971
end
7072

7173
private
7274

75+
def connection
76+
@connection ||= Faraday.new do |faraday|
77+
faraday.options[:timeout] = @client.timeout || 10
78+
faraday.adapter Faraday.default_adapter
79+
end
80+
end
81+
7382
def url_options
7483
options = []
7584
options += @targets.map { |t| ["target", t.to_s] }

slate.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ Gem::Specification.new do |gem|
1717
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
1818
gem.require_paths = ["lib"]
1919

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

2525
gem.add_development_dependency "rake"
2626
gem.add_development_dependency "rspec"

spec/lib/slate/graph_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@
124124
graph.download(:json).should eq(@json_stub)
125125
graph.download(:svg).should eq(@svg_stub)
126126
end
127+
128+
it "should respect the configured timeout" do
129+
stub_request(:get, "http://graphite/render?format=png&target=app.server01.timeout").
130+
to_timeout
131+
132+
target = Slate::Target.build("app.server01.timeout")
133+
graph = Slate::Graph.new(@client)
134+
graph << target
135+
136+
expect {
137+
graph.download(:png).should eq(@png_stub)
138+
}.to raise_error(Slate::Error::TimeoutError)
139+
end
127140
end
128141

129142
def stub_download(format, body="")

spec/lib/slate_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@
88

99
client.endpoint.should == "http://graphite"
1010
end
11+
12+
it "should be able to configure the request timeout" do
13+
client = Slate.configure do |c|
14+
c.timeout = 30
15+
end
16+
17+
client.timeout.should == 30
18+
end
1119
end

0 commit comments

Comments
 (0)