summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-16 13:32:54 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-16 13:32:54 +0200
commit8093f1bc8a54c4f205460d4fbc1706f45bb6a195 (patch)
tree89837da677b706006742ed2559d9870ed8faf42a /lib
parentadd three map/reduce operations (from the fast track) (diff)
downloadnet-riak-8093f1bc8a54c4f205460d4fbc1706f45bb6a195.tar.gz
accept multiples hosts so we don't always hit the same node
Diffstat (limited to 'lib')
-rw-r--r--lib/Net/Riak.pm26
-rw-r--r--lib/Net/Riak/Client.pm11
-rw-r--r--lib/Net/Riak/Role/Hosts.pm44
-rw-r--r--lib/Net/Riak/Role/REST.pm2
4 files changed, 75 insertions, 8 deletions
diff --git a/lib/Net/Riak.pm b/lib/Net/Riak.pm
index fa54e2e..1fb8c21 100644
--- a/lib/Net/Riak.pm
+++ b/lib/Net/Riak.pm
@@ -48,7 +48,31 @@ sub bucket {
 
 =item B<host>
 
-Hostname or IP address (default 'http://127.0.0.1:8098')
+URL of the node (default 'http://127.0.0.1:8098'). If your ring is composed with more than one node, you can configure the client to hit more than one host, instead of hitting always the same node. For this, you can do one of the following:
+
+=over 4
+
+=item B<all nodes equals>
+
+    my $riak = Net::Riak->new(
+        host => [
+            'http://10.0.0.40:8098',
+            'http://10.0.0.41:8098'
+        ]
+    );
+
+=item B<give weight to nodes>
+
+    my $riak = Net::Riak->new(
+        host => [
+            {node => 'http://10.0.0.40:8098', weight => '0.2'},
+            {node => 'http://10.0.0.41:8098', weight => '0.8'}
+        ]
+    );
+
+=back
+
+Now, when a request is made, a node is picked at random, according to weight.
 
 =item B<prefix>
 
diff --git a/lib/Net/Riak/Client.pm b/lib/Net/Riak/Client.pm
index fb4ce68..19d172f 100644
--- a/lib/Net/Riak/Client.pm
+++ b/lib/Net/Riak/Client.pm
@@ -3,13 +3,12 @@ package Net::Riak::Client;
 use Moose;
 use MIME::Base64;
 
-with qw/Net::Riak::Role::REST Net::Riak::Role::UserAgent/;
+with qw/
+  Net::Riak::Role::REST
+  Net::Riak::Role::UserAgent
+  Net::Riak::Role::Hosts
+  /;
 
-has host => (
-    is      => 'rw',
-    isa     => 'Str',
-    default => 'http://127.0.0.1:8098'
-);
 has prefix => (
     is      => 'rw',
     isa     => 'Str',
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;