Apache2-BalanceLogic

 view release on metacpan or  search on metacpan

Config/MainConfig.yaml  view on Meta::CPAN

#--- select one Plugin module and config file
Plugin:
  Name: 'DistByTime' 
  Config: '/var/www/perl/Apache2/BalanceLogic/Config/PluginConfig/DistByTime.yaml'
  #Name: 'DistByCookie' 
  #Config: '/var/www/perl/Apache2/BalanceLogic/Config/PluginConfig/DistByCookie.yaml'
  #Name: 'DistByURL' 
  #Config: '/var/www/perl/Apache2/BalanceLogic/Config/PluginConfig/DistByURL.yaml'

#--- your server admin ipaddress. it can use __force__ option.
ADMIN_IP:
  - 127.0.0.1
  #- 192.168.1.0/24

MANIFEST  view on Meta::CPAN

Changes
Config/MainConfig.yaml
Config/PluginConfig/DistByCookie.yaml
Config/PluginConfig/DistByTime.yaml
Config/PluginConfig/DistByURL.yaml
lib/Apache2/BalanceLogic.pm
lib/Apache2/BalanceLogic/Plugin.pm
lib/Apache2/BalanceLogic/Plugin/DistByCookie.pm
lib/Apache2/BalanceLogic/Plugin/DistByTime.pm
lib/Apache2/BalanceLogic/Plugin/DistByURL.pm
Makefile.PL
MANIFEST			This list of files
README
t/Apache2-BalanceLogic.t
META.yml                                 Module meta-data (added by MakeMaker)

META.yml  view on Meta::CPAN

abstract:            Perl extension for mod_proxy_balancer
license:             ~
generated_by:        ExtUtils::MakeMaker version 6.36
distribution_type:   module
requires:     
    Apache2::Connection:           2.000002
    Apache2::Const:                2.000002
    Apache2::RequestIO:            2.000002
    Apache2::RequestRec:           2.000002
    APR::Table:                    0.009
    CGI::Cookie:                   1.26
    Net::CIDR::Lite:               0.2
    UNIVERSAL::require:            0.11
    YAML:                          0.65
meta-spec:
    url:     http://module-build.sourceforge.net/META-spec-v1.2.html
    version: 1.2
author:
    - Takeshi Miki <miki@cpan.org>

Makefile.PL  view on Meta::CPAN

WriteMakefile(
    NAME              => 'Apache2::BalanceLogic',
    VERSION_FROM      => 'lib/Apache2/BalanceLogic.pm', # finds $VERSION
    PREREQ_PM         => {
	Apache2::RequestRec	=> 2.000002,
	Apache2::RequestIO	=> 2.000002,
	Apache2::Connection	=> 2.000002,
	APR::Table		=> 0.009000,
	Apache2::Const		=> 2.000002,
	YAML			=> 0.65,
	CGI::Cookie		=> 1.26,
	Net::CIDR::Lite		=> 0.20,
	UNIVERSAL::require	=> 0.11,
	},
    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (ABSTRACT_FROM  => 'lib/Apache2/BalanceLogic.pm', # retrieve abstract from module
       AUTHOR         => 'Takeshi Miki <miki@cpan.org>') : ()),
    LIBS              => [''], # e.g., '-lm'
    DEFINE            => '', # e.g., '-DHAVE_SOMETHING'
    INC               => '-I.', # e.g., '-I. -I/usr/include/other'
	# Un-comment this if you add C files to link with later:

lib/Apache2/BalanceLogic.pm  view on Meta::CPAN

package Apache2::BalanceLogic;

use strict;
use warnings;
use Apache2::RequestRec ();
use Apache2::RequestIO  ();
use Apache2::Connection ();
use APR::Table          ();
use Apache2::Const -compile => qw( OK DECLINED );
use YAML qw 'LoadFile';
use CGI::Cookie;
use Net::CIDR::Lite;
use UNIVERSAL::require;

our $VERSION = '0.0.1';

our $conf;
our $plugin;

sub handler {

lib/Apache2/BalanceLogic.pm  view on Meta::CPAN

        my $ip       = $r->connection->remote_ip;
        my $cidr     = Net::CIDR::Lite->new;
        my $admin_ip = $conf->{ADMIN_IP} or die($!);
        for (@$admin_ip) {
            $cidr->add_any($_);
        }
        $route_id = $force if $cidr->find($ip);
    }

    # a inner cookie trick for "stickysession" in mod_proxy_balancer.
    my $cookie_str = $r->headers_in->get('Cookie');
    $cookie_str =~ s/route_id=\d+\;?//;
    $cookie_str = 'route_id=x.' . $route_id . '; ' . $cookie_str . ';';
    $r->headers_in->set( 'Cookie' => $cookie_str );

    # return OK
    return Apache2::Const::OK;
}

1;

__END__

=head1 NAME

lib/Apache2/BalanceLogic.pm  view on Meta::CPAN

Applcation Config file ( MainConfig.yaml )

  #--- select one Plugin module and config file
  Plugin:
    Name: 'DistByURL' 
    Config: '/foo/bar/perl/Apache2/BalanceLogic/Config/PluginConfig/DistByURL.yaml'
    #---
    #Name: 'DistByTime' 
    #Config: '/foo/bar/perl/Apache2/BalanceLogic/Config/PluginConfig/DistByTime.yaml'
    #---
    #Name: 'DistByCookie' 
    #Config: '/foo/bar/perl/Apache2/BalanceLogic/Config/PluginConfig/DistByCookie.yaml'
  #--- your server admin ipaddress. it can use __force__ option.
  ADMIN_IP:
    - 192.168.1.0/24


=head1 DESCRIPTION

This is a simple extention for 'mod_proxy_balancer'.
You can put your original Plungin code that distribute the requests among the backend servers by your original algorithm.
In other words, this is a  "inner cookie trick" for stickysession in mod_proxy_balancer.
Let's enjoy!

=head1 Plugin 

There are 3 sample Plugin modules. ( in Plugin directory )

  L<Apache2::BalanceLogic::Plugin::DistByCookie>
    distribute the requests by unique user cookie that maybe generated by usertrac module.
    this is implemented by a simple slurp division code.
  L<Apache2::BalanceLogic::Plugin::DistByTime>
    distribute the requests by time number ( hour ). see config file.
  L<Apache2::BalanceLogic::Plugin::DistByURL>
    by reqular expression on URL path string.


=head1 SEE ALSO

lib/Apache2/BalanceLogic/Plugin/DistByCookie.pm  view on Meta::CPAN

package Apache2::BalanceLogic::Plugin::DistByCookie;

use strict;
use warnings;
use base qw( Apache2::BalanceLogic::Plugin );
use CGI::Cookie;

sub run {
    my $self = shift;
    my $r    = shift;

    my $distribute_num = $self->{conf}->{distribute_num};
    my $cookie_name    = $self->{conf}->{cookie_name};
    my $route_id;

    my %cookies = parse CGI::Cookie( $r->headers_in->get('Cookie') );
    $cookies{$cookie_name} and my $str = $cookies{$cookie_name}->value();

    if ($str) {
        for ( split( //, $str ) ) {
            $route_id += unpack( "C*", $_ );
        }
        $route_id = $route_id % $distribute_num + 1;
    }

    return $route_id;



( run in 0.326 second using v1.01-cache-2.11-cpan-e9199f4ba4c )