Commit | Line | Data |
fb691af9 |
1 | package Catalyst::TraitFor::Model::DBIC::Schema::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 | |
7314403a |
8 | use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/; |
9 | use MooseX::Types::Moose qw/Str HashRef/; |
c4fee9b8 |
10 | |
c4fee9b8 |
11 | =head1 NAME |
12 | |
fb691af9 |
13 | Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for |
c4fee9b8 |
14 | L<Catalyst::Model::DBIC::Schema> |
15 | |
16 | =head1 SYNOPSiS |
17 | |
18 | __PACKAGE__->config({ |
c34bcab6 |
19 | traits => ['Replicated'] |
7314403a |
20 | connect_info => |
c4fee9b8 |
21 | ['dbi:mysql:master', 'user', 'pass'], |
22 | replicants => [ |
23 | ['dbi:mysql:slave1', 'user', 'pass'], |
24 | ['dbi:mysql:slave2', 'user', 'pass'], |
25 | ['dbi:mysql:slave3', 'user', 'pass'], |
bd309c0c |
26 | ], |
27 | balancer_args => { |
28 | master_read_weight => 0.3 |
29 | } |
c4fee9b8 |
30 | }); |
31 | |
32 | =head1 DESCRIPTION |
33 | |
c4fee9b8 |
34 | Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects |
35 | replicants provided in config. See that module for supported resultset |
36 | attributes. |
37 | |
bd309c0c |
38 | The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is |
39 | C<::Random>. |
40 | |
41 | Sets the |
42 | L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to |
43 | C<1> by default, meaning that you have the same chance of reading from master as |
44 | you do from replicants. Set to C<0> to turn off reads from master. |
45 | |
c4fee9b8 |
46 | =head1 CONFIG PARAMETERS |
47 | |
48 | =head2 replicants |
49 | |
50 | Array of connect_info settings for every replicant. |
51 | |
7314403a |
52 | The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or |
53 | as their own parameters. If set via separate parameters, they will override the |
54 | settings in C<connect_info>. |
55 | |
56 | =head2 pool_type |
57 | |
58 | See L<DBIx::Class::Storage::DBI::Replicated/pool_type>. |
59 | |
60 | =head2 pool_args |
61 | |
62 | See L<DBIx::Class::Storage::DBI::Replicated/pool_args>. |
63 | |
64 | =head2 balancer_type |
65 | |
66 | See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>. |
67 | |
68 | =head2 balancer_args |
69 | |
70 | See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>. |
71 | |
c4fee9b8 |
72 | =cut |
73 | |
74 | has replicants => ( |
75 | is => 'ro', isa => ConnectInfos, coerce => 1, required => 1 |
76 | ); |
77 | |
7314403a |
78 | has pool_type => (is => 'ro', isa => LoadedClass); |
79 | has pool_args => (is => 'ro', isa => HashRef); |
80 | has balancer_type => (is => 'ro', isa => Str); |
81 | has balancer_args => (is => 'ro', isa => HashRef); |
82 | |
c4fee9b8 |
83 | after setup => sub { |
84 | my $self = shift; |
85 | |
86 | # check storage_type compatibility (if configured) |
87 | if (my $storage_type = $self->storage_type) { |
88 | my $class = $storage_type =~ /^::/ ? |
89 | "DBIx::Class::Storage$storage_type" |
90 | : $storage_type; |
91 | |
92 | croak "This storage_type cannot be used with replication" |
93 | unless $class->isa('DBIx::Class::Storage::DBI::Replicated'); |
94 | } else { |
95 | $self->storage_type('::DBI::Replicated'); |
96 | } |
39f5f008 |
97 | |
7314403a |
98 | my $connect_info = $self->connect_info; |
bd309c0c |
99 | |
7314403a |
100 | $connect_info->{pool_type} = $self->pool_type |
101 | if $self->pool_type; |
102 | |
103 | $connect_info->{pool_args} = $self->pool_args |
104 | if $self->pool_args; |
105 | |
106 | $connect_info->{balancer_type} = $self->balancer_type || |
107 | $connect_info->{balancer_type} || '::Random'; |
108 | |
109 | $connect_info->{balancer_args} = $self->balancer_args || |
110 | $connect_info->{balancer_args} || {}; |
111 | |
112 | $connect_info->{balancer_args}{master_read_weight} = 1 |
113 | unless exists $connect_info->{balancer_args}{master_read_weight}; |
c4fee9b8 |
114 | }; |
115 | |
73f72d28 |
116 | sub BUILD {} |
7314403a |
117 | |
73f72d28 |
118 | after BUILD => sub { |
c4fee9b8 |
119 | my $self = shift; |
120 | |
ac63f7fa |
121 | $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten); |
c4fee9b8 |
122 | }; |
123 | |
124 | =head1 SEE ALSO |
125 | |
126 | L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, |
127 | L<DBIx::Class::Storage::DBI::Replicated>, |
e203cd42 |
128 | L<Catalyst::TraitFor::Model::DBIC::Schema::Caching> |
c4fee9b8 |
129 | |
130 | =head1 AUTHOR |
131 | |
c7d7b849 |
132 | Rafael Kitover, C<rkitover at cpan.org> |
c4fee9b8 |
133 | |
134 | =head1 COPYRIGHT |
135 | |
136 | This program is free software, you can redistribute it and/or modify it |
137 | under the same terms as Perl itself. |
138 | |
139 | =cut |
140 | |
141 | 1; |