diff options
author | franck cuny <franck@lumberjaph.net> | 2010-06-16 13:32:54 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-06-16 13:32:54 +0200 |
commit | 8093f1bc8a54c4f205460d4fbc1706f45bb6a195 (patch) | |
tree | 89837da677b706006742ed2559d9870ed8faf42a /lib/Net/Riak/Role | |
parent | add three map/reduce operations (from the fast track) (diff) | |
download | net-riak-8093f1bc8a54c4f205460d4fbc1706f45bb6a195.tar.gz |
accept multiples hosts so we don't always hit the same node
Diffstat (limited to 'lib/Net/Riak/Role')
-rw-r--r-- | lib/Net/Riak/Role/Hosts.pm | 44 | ||||
-rw-r--r-- | lib/Net/Riak/Role/REST.pm | 2 |
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/Net/Riak/Role/Hosts.pm b/lib/Net/Riak/Role/Hosts.pm new file mode 100644 index 0000000..5171928 --- /dev/null +++ b/lib/Net/Riak/Role/Hosts.pm @@ -0,0 +1,44 @@ +package Net::Riak::Role::Hosts; + +use Moose::Role; +use Moose::Util::TypeConstraints; + +subtype 'RiakHost' => as 'ArrayRef[HashRef]'; + +coerce 'RiakHost' => from 'Str' => via { + [{node => $_, weight => 1}]; +}; +coerce 'RiakHost' => from 'ArrayRef' => via { + my $backends = $_; + my $weight = 1 / @$backends; + [map { {node => $_, weight => $weight} } @$backends]; +}; +coerce 'RiakHost' => from 'HashRef' => via { + my $backends = $_; + my $total = 0; + $total += $_ for values %$backends; + [map { {node => $_, weight => $backends->{$_} / $total} } + keys %$backends]; +}; + +has host => ( + is => 'rw', + isa => 'RiakHost', + coerce => 1, + default => sub {'http://127.0.0.1:8098'} +); + +sub get_host { + my $self = shift; + + my $choice; + my $rand = rand; + + for (@{$self->host}) { + $choice = $_->{node}; + ($rand -= $_->{weight}) <= 0 and last; + } + $choice; +} + +1; diff --git a/lib/Net/Riak/Role/REST.pm b/lib/Net/Riak/Role/REST.pm index 591bd6c..1a18ff7 100644 --- a/lib/Net/Riak/Role/REST.pm +++ b/lib/Net/Riak/Role/REST.pm @@ -14,7 +14,7 @@ sub _build_path { sub _build_uri { my ($self, $path, $params) = @_; - my $uri = URI->new($self->host); + my $uri = URI->new($self->get_host); $uri->path($self->_build_path($path)); $uri->query_form(%$params); $uri; |