/
Pool.pm
98 lines (64 loc) · 1.94 KB
/
Pool.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package Cassandra::Pool;
=pod
=encoding utf8
=head1 NAME
Cassandra::Pool
=head1 DESCRIPTION
Client Pool for Cassandra::Simple
=cut
use strict;
use warnings;
use 5.010;
use Cassandra::Cassandra;
use Cassandra::Types;
use Thrift;
use Thrift::BinaryProtocol;
use Thrift::FramedTransport;
use Thrift::Socket;
use Data::Dumper;
sub new{
my $class = shift;
my $keyspace = shift;
my $opt = shift;
my $self={};
my $server_name = $opt->{server_name} // '127.0.0.1';
my $server_port = $opt->{server_port} // 9160;
$self->{username} = $opt->{username} // '';
$self->{password} = $opt->{password} // '';
$self = bless($self, $class);
my $first = $self->create_client({server_name => $server_name, server_port => $server_port});
$first->set_keyspace($keyspace);
$self->{pool} = [];
push @{$self->{pool}}, $first;
# print Dumper map { split( /\//, $_->{endpoints}->[0]) } @{$first->describe_ring($keyspace)};
foreach( map {split( /\//, $_->{endpoints}->[0])} @{$first->describe_ring($keyspace)} ){
next if $server_name eq $_;
# print ">> ",$_," ",$keyspace,"\n";
my $cl = $self->create_client({server_name => $_, server_port => $server_port});
$cl->set_keyspace($keyspace);
push @{$self->{pool}}, $cl;
}
return $self;
}
sub create_client{
my $self = shift;
my $opt = shift // {};
my $server_name = $opt->{server_name};
my $server_port = $opt->{server_port} // 9160;
my $sock = Thrift::Socket->new( $server_name, $server_port );
my $transport = Thrift::FramedTransport->new( $sock, 1024, 1024 );
my $protocol = Thrift::BinaryProtocol->new( $transport );
my $client = Cassandra::CassandraClient->new( $protocol );
$transport->open;
# print "»opened transport \n";
my $auth = Cassandra::AuthenticationRequest->new;
$auth->{credentials} =
{ username => $self->{username}, password => $self->{password} };
$client->login($auth);
return $client;
}
sub get{
my $self = shift;
return $self->{pool}->[rand @{$self->{pool}}];
}
1