1package HTTP::Proxy::Engine::Threaded;
2use strict;
3use HTTP::Proxy;
4use threads;
5
6# A massive hack of Engine::Fork to use the threads stuff
7# Basically created to work under win32 so that the filters
8# can share global caches among themselves
9# Angelos Karageorgiou angelos@unix.gr
10
11our @ISA = qw( HTTP::Proxy::Engine );
12our %defaults = (
13    max_clients => 60,
14);
15
16
17__PACKAGE__->make_accessors( qw( kids select ), keys %defaults );
18
19sub start {
20    my $self = shift;
21    $self->kids( [] );
22    $self->select( IO::Select->new( $self->proxy->daemon ) );
23}
24
25sub run {
26    my $self   = shift;
27    my $proxy  = $self->proxy;
28    my $kids   = $self->kids;
29
30    # check for new connections
31    my @ready = $self->select->can_read(1);
32    for my $fh (@ready) {    # there's only one, anyway
33        # single-process proxy (useful for debugging)
34
35        # accept the new connection
36        my $conn  = $fh->accept;
37	my $child=threads->new(\&worker,$proxy,$conn);
38        if ( !defined $child ) {
39            $conn->close;
40            $proxy->log( HTTP::Proxy::ERROR, "PROCESS", "Cannot spawn thread" );
41            next;
42        }
43	$child->detach();
44
45    }
46
47}
48
49sub stop {
50    my $self = shift;
51    my $kids = $self->kids;
52
53   # not needed
54}
55
56sub worker {
57	my $proxy=shift;
58	my $conn=shift;
59       $proxy->serve_connections($conn);
60	$conn->close();
61       return;
62}
63
641;
65
66__END__
67
68=head1 NAME
69
70HTTP::Proxy::Engine::Threaded - A scoreboard-based HTTP::Proxy engine
71
72=head1 SYNOPSIS
73
74    my $proxy = HTTP::Proxy->new( engine => 'Threaded' );
75
76=head1 DESCRIPTION
77
78This module provides a threaded engine to HTTP::Proxy.
79
80=head1 METHODS
81
82The module defines the following methods, used by HTTP::Proxy main loop:
83
84=over 4
85
86=item start()
87
88Initialize the engine.
89
90=item run()
91
92Implements the forking logic: a new process is forked for each new
93incoming TCP connection.
94
95=item stop()
96
97Reap remaining child processes.
98
99=back
100
101=head1 SEE ALSO
102
103L<HTTP::Proxy>, L<HTTP::Proxy::Engine>.
104
105=head1 AUTHOR
106
107Angelos Karageorgiou C<< <angelos@unix.gr> >>. (Actual code)
108
109Philippe "BooK" Bruhat, C<< <book@cpan.org> >>. (Documentation)
110
111=head1 COPYRIGHT
112
113Copyright 2010, Philippe Bruhat.
114
115=head1 LICENSE
116
117This module is free software; you can redistribute it or modify it under
118the same terms as Perl itself.
119
120=cut
121
122