Created
May 12, 2014 15:23
-
-
Save mje113/3850178f89a74525f0bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'jbundler' | |
require 'manticore' | |
class HisterixCommand < com.netflix.hystrix.HystrixCommand | |
java_import com.netflix.hystrix.HystrixCommand::Setter | |
java_import com.netflix.hystrix.HystrixCommandKey | |
java_import com.netflix.hystrix.HystrixCommandGroupKey | |
java_import com.netflix.hystrix.HystrixCommandProperties | |
DEFAULT_TIMEOUT = 10_000 | |
DEFAULT_GROUP = "default" | |
def initialize(name_or_setter, group=DEFAULT_GROUP, timeout=DEFAULT_TIMEOUT, &block) | |
@block = block | |
setter = name_or_setter if name_or_setter.is_a?(com.netflix.hystrix.HystrixCommand::Setter) | |
setter ||= Setter.withGroupKey(HystrixCommandGroupKey::Factory.asKey(group)) | |
.andCommandKey(HystrixCommandKey::Factory.asKey(name_or_setter)) | |
.andCommandPropertiesDefaults(HystrixCommandProperties::Setter().withExecutionIsolationThreadTimeoutInMilliseconds(timeout)) | |
super(setter) | |
end | |
end | |
class GoogleGetter < HisterixCommand | |
HTTP_CLIENT = Manticore::Client.new( | |
request_timeout: 6000, | |
connect_timeout: 25, | |
socket_timeout: 25, | |
pool_max: 10, | |
pool_max_per_route: 2, | |
follow_redirects: true | |
) | |
def http_client | |
HTTP_CLIENT | |
end | |
def run | |
http_client.get('http://www.google.com').body | |
sleep 12 # simulating a Tomeout | |
end | |
def getFallback | |
FallbackGetter.new.execute | |
end | |
end | |
class FallbackGetter < HisterixCommand | |
def initialize | |
super('default') | |
end | |
def run | |
'fellback' | |
end | |
end | |
puts GoogleGetter.new('default').execute | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment