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