Permalink
Browse files

no use DWR

  • Loading branch information...
1 parent cd2481a commit 24d62e07daf84176a37338c2425f6059f5e1bb0c @nekokak committed Mar 22, 2012
Showing with 30 additions and 39 deletions.
  1. +1 −0 Changes
  2. +25 −26 lib/Clutch/Client.pm
  3. +1 −3 t/001_basic.t
  4. +1 −3 t/002_request_background.t
  5. +1 −4 t/003_request_multi.t
  6. +1 −3 t/004_request_background_multi.t
View
@@ -4,6 +4,7 @@ Revision history for Perl extension Clutch
- change protocol
- support request_multi(_background)
- remove Clutch::Admin
+ - no use DWR
0.03 2012/01/25
- more fixed some dist problem.
View
@@ -2,36 +2,23 @@ package Clutch::Client;
use strict;
use warnings;
use Clutch::Util;
-use Data::WeightedRoundRobin;
use IO::Select;
use Carp ();
sub new {
my $class = shift;
my %args = @_ == 1 ? %{$_[0]} : @_;
-
Carp::croak "Mandatory parameter 'servers'" unless $args{servers};
- %args = (
+ my $rr = delete $args{rr} || 'Clutch::Client::RR';
+
+ bless {
servers => undef,
timeout => 10,
+ rr => $rr->new($args{servers}),
%args,
- );
-
- my $self = bless \%args, $class;
-
- my @servers;
- for my $row (@{$self->{servers}}) {
- if (ref($row) eq 'HASH') {
- push @servers, +{ value => $row->{address}, weight => $row->{weight} };
- }
- else {
- push @servers, $row;
- }
- }
- $self->{dwr} = Data::WeightedRoundRobin->new(\@servers);
- $self;
+ }, $class;
}
sub request_background {
@@ -47,7 +34,7 @@ sub request {
sub _request {
my ($self, $cmd_name, $function, $args) = @_;
- my $server = $self->{dwr}->next;
+ my $server = $self->{rr}->next;
my $sock = Clutch::Util::new_client($server);
my $json_args = Clutch::Util::json->encode($args);
@@ -99,7 +86,7 @@ sub _request_multi {
my %sockets_map;
for my $i (0 .. ($request_count - 1)) {
- my $server = $self->{dwr}->next;
+ my $server = $self->{rr}->next;
my $sock = Clutch::Util::new_client($server);
$is->add($sock);
$sockets_map{$sock}=$i;
@@ -137,6 +124,22 @@ sub _request_multi {
wantarray ? @res : \@res;
}
+package
+ Clutch::Client::RR;
+
+sub new {
+ my ($class, $servers) = @_;
+ bless +{
+ servers => $servers,
+ }, $class;
+}
+
+sub next {
+ my $self = shift;
+ push(@{$self->{servers}}, shift(@{$self->{servers}}));
+ $self->{servers}[0];
+}
+
1;
__END__
@@ -153,9 +156,7 @@ Clutch::Client - distributed job system's client class
use Clutch::Client;
my $args = shift || die 'missing args';
my $client = Clutch::Client->new(
- servers => [
- +{ address => "$worker_ip:$worker_port" },
- ],
+ servers => [ "$worker_ip:$worker_port" ],
);
my $res = $client->request('echo', $args);
print $res, "\n";
@@ -170,11 +171,9 @@ Clutch::Client - distributed job system's client class
The value is a reference to an array of worker addresses.
-If hash reference, the keys are address (scalar), weight (positive rational number)
-
The server address is in the form host:port for network TCP connections
-Client will distribute Data::WeightedRoundRobin.
+Client will distribute basic RoundRobin.
=item $opts{timeout}
View
@@ -10,9 +10,7 @@ test_tcp(
client => sub {
my ($port, $server_pid) = @_;
my $client = Clutch::Client->new(
- servers => [
- +{ address => '127.0.0.1:'.$port },
- ],
+ servers => ['127.0.0.1:'.$port]
);
my $res;
@@ -10,9 +10,7 @@ test_tcp(
client => sub {
my ($port, $server_pid) = @_;
my $client = Clutch::Client->new(
- servers => [
- +{ address => '127.0.0.1:'.$port },
- ],
+ servers => ['127.0.0.1:'.$port],
);
my $res;
View
@@ -14,10 +14,7 @@ test_tcp(
client => sub {
my ($port2, $server_pid2) = @_;
my $client = Clutch::Client->new(
- servers => [
- +{ address => '127.0.0.1:'.$port },
- +{ address => '127.0.0.1:'.$port2 },
- ],
+ servers => ['127.0.0.1:'.$port, '127.0.0.1:'.$port2],
);
my $res;
@@ -10,9 +10,7 @@ test_tcp(
client => sub {
my ($port, $server_pid) = @_;
my $client = Clutch::Client->new(
- servers => [
- +{ address => '127.0.0.1:'.$port },
- ],
+ servers => ['127.0.0.1:'.$port],
);
my $res;

0 comments on commit 24d62e0

Please sign in to comment.