Commit | Line | Data |
c34bcab6 |
1 | package Catalyst::Model::DBIC::Schema::Trait::Replicated; |
c4fee9b8 |
2 | |
73f72d28 |
3 | use namespace::autoclean; |
c4fee9b8 |
4 | use Moose::Role; |
5 | use Moose::Autobox; |
bd309c0c |
6 | use Carp::Clan '^Catalyst::Model::DBIC::Schema'; |
c4fee9b8 |
7 | |
8 | use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos'; |
9 | |
c4fee9b8 |
10 | =head1 NAME |
11 | |
c34bcab6 |
12 | Catalyst::Model::DBIC::Schema::Trait::Replicated - Replicated storage support for |
c4fee9b8 |
13 | L<Catalyst::Model::DBIC::Schema> |
14 | |
15 | =head1 SYNOPSiS |
16 | |
17 | __PACKAGE__->config({ |
c34bcab6 |
18 | traits => ['Replicated'] |
c4fee9b8 |
19 | connect_info => |
20 | ['dbi:mysql:master', 'user', 'pass'], |
21 | replicants => [ |
22 | ['dbi:mysql:slave1', 'user', 'pass'], |
23 | ['dbi:mysql:slave2', 'user', 'pass'], |
24 | ['dbi:mysql:slave3', 'user', 'pass'], |
bd309c0c |
25 | ], |
26 | balancer_args => { |
27 | master_read_weight => 0.3 |
28 | } |
c4fee9b8 |
29 | }); |
30 | |
31 | =head1 DESCRIPTION |
32 | |
c4fee9b8 |
33 | Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects |
34 | replicants provided in config. See that module for supported resultset |
35 | attributes. |
36 | |
bd309c0c |
37 | The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is |
38 | C<::Random>. |
39 | |
40 | Sets the |
41 | L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to |
42 | C<1> by default, meaning that you have the same chance of reading from master as |
43 | you do from replicants. Set to C<0> to turn off reads from master. |
44 | |
c4fee9b8 |
45 | =head1 CONFIG PARAMETERS |
46 | |
47 | =head2 replicants |
48 | |
49 | Array of connect_info settings for every replicant. |
50 | |
51 | =cut |
52 | |
53 | has replicants => ( |
54 | is => 'ro', isa => ConnectInfos, coerce => 1, required => 1 |
55 | ); |
56 | |
57 | after setup => sub { |
58 | my $self = shift; |
59 | |
60 | # check storage_type compatibility (if configured) |
61 | if (my $storage_type = $self->storage_type) { |
62 | my $class = $storage_type =~ /^::/ ? |
63 | "DBIx::Class::Storage$storage_type" |
64 | : $storage_type; |
65 | |
66 | croak "This storage_type cannot be used with replication" |
67 | unless $class->isa('DBIx::Class::Storage::DBI::Replicated'); |
68 | } else { |
69 | $self->storage_type('::DBI::Replicated'); |
70 | } |
39f5f008 |
71 | |
bd309c0c |
72 | $self->connect_info->{balancer_type} ||= '::Random' |
73 | unless $self->connect_info->{balancer_type}; |
74 | |
75 | unless ($self->connect_info->{balancer_args} && |
76 | exists $self->connect_info->{balancer_args}{master_read_weight}) { |
77 | $self->connect_info->{balancer_args}{master_read_weight} = 1; |
78 | } |
c4fee9b8 |
79 | }; |
80 | |
73f72d28 |
81 | sub BUILD {} |
82 | after BUILD => sub { |
c4fee9b8 |
83 | my $self = shift; |
84 | |
ac63f7fa |
85 | $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten); |
c4fee9b8 |
86 | }; |
87 | |
88 | =head1 SEE ALSO |
89 | |
90 | L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, |
91 | L<DBIx::Class::Storage::DBI::Replicated>, |
92 | L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached> |
93 | |
94 | =head1 AUTHOR |
95 | |
c7d7b849 |
96 | Rafael Kitover, C<rkitover at cpan.org> |
c4fee9b8 |
97 | |
98 | =head1 COPYRIGHT |
99 | |
100 | This program is free software, you can redistribute it and/or modify it |
101 | under the same terms as Perl itself. |
102 | |
103 | =cut |
104 | |
105 | 1; |