| 1 | package App::cdnget; | ||||||||||
| 2 | =head1 NAME | ||||||||||
| 3 | |||||||||||
| 4 | App::cdnget - CDN Reverse Proxy | ||||||||||
| 5 | |||||||||||
| 6 | =head1 VERSION | ||||||||||
| 7 | |||||||||||
| 8 | version 0.06 | ||||||||||
| 9 | |||||||||||
| 10 | =head1 ABSTRACT | ||||||||||
| 11 | |||||||||||
| 12 | CDN Reverse Proxy | ||||||||||
| 13 | |||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||
| 15 | |||||||||||
| 16 | p5-cdnget is a FastCGI application that flexible pull-mode Content Delivery Network reverse proxy. | ||||||||||
| 17 | |||||||||||
| 18 | B<This is ALPHA version> | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | ### TODO: css, js minifier. | ||||||||||
| 22 | BEGIN | ||||||||||
| 23 | 2 | ▶ | { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.3
| |||||||||||
| 24 | 2 | require Config; | |||||||||
| 25 | 2 | ▶ | if ($Config::Config{'useithreads'}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | { | ||||||||||
| 27 | 0 | require threads; | |||||||||
| 28 | 0 | threads->import(); | |||||||||
| 29 | 0 | require threads::shared; | |||||||||
| 30 | 0 | threads::shared->import(); | |||||||||
| 31 | } else | ||||||||||
| 32 | { | ||||||||||
| 33 | 2 | require forks; | |||||||||
| 34 | 1 | forks->import(); | |||||||||
| 35 | 1 | require forks::shared; | |||||||||
| 36 | 1 | forks::shared->import(); | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 1 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | ▶ | use Time::HiRes qw(sleep usleep); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 1 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 1 | ▶ | use Lazy::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 1 | ▶ | use App::cdnget::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 0 | ▶ | use App::cdnget::Worker; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||||||||||
| 49 | use App::cdnget::Downloader; | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | BEGIN | ||||||||||
| 53 | { | ||||||||||
| 54 | require Exporter; | ||||||||||
| 55 | our $VERSION = '0.06'; | ||||||||||
| 56 | our @ISA = qw(Exporter); | ||||||||||
| 57 | our @EXPORT = qw(main run); | ||||||||||
| 58 | our @EXPORT_OK = qw(); | ||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | our $DTF_RFC822 = "%a, %d %b %Y %T %Z"; | ||||||||||
| 63 | our $DTF_RFC822_GMT = "%a, %d %b %Y %T GMT"; | ||||||||||
| 64 | our $DTF_YMDHMS = "%F %T"; | ||||||||||
| 65 | our $DTF_YMDHMS_Z = "%F %T %z"; | ||||||||||
| 66 | our $DTF_SYSLOG = "%b %e %T"; | ||||||||||
| 67 | our $CHUNK_SIZE = 256*1024; | ||||||||||
| 68 | |||||||||||
| 69 | my $terminating :shared = 0; | ||||||||||
| 70 | my $terminating_force :shared = 0; | ||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | sub log_info | ||||||||||
| 74 | { | ||||||||||
| 75 | my ($msg) = @_; | ||||||||||
| 76 | $msg = "Unknown" unless $msg; | ||||||||||
| 77 | my $dts = DateTime->now(time_zone => POSIX::strftime("%z", localtime), locale => "en")->strftime('%x %T %z'); | ||||||||||
| 78 | $msg = "[$dts] $msg"; | ||||||||||
| 79 | say $msg; | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub main | ||||||||||
| 83 | { | ||||||||||
| 84 | log_info "Starting p5-cdnget/${App::cdnget::VERSION}"; | ||||||||||
| 85 | eval | ||||||||||
| 86 | { | ||||||||||
| 87 | my $cmdargs = commandArgs({ valuableArgs => 1, noCommand => 1 }, @_); | ||||||||||
| 88 | my $spares = $cmdargs->{"--spares"}; | ||||||||||
| 89 | $spares = 1 unless defined($spares) and $spares >= 1; | ||||||||||
| 90 | my $maxWorkers = $cmdargs->{"--max-workers"}; | ||||||||||
| 91 | $maxWorkers = $spares+1 unless defined($maxWorkers) and $maxWorkers > $spares; | ||||||||||
| 92 | my $cachePath = $cmdargs->{"--cache-path"}; | ||||||||||
| 93 | $cachePath = "/tmp/cdnget" unless defined($cachePath); | ||||||||||
| 94 | my $addr = $cmdargs->{"--addr"}; | ||||||||||
| 95 | $addr = "" unless defined($addr); | ||||||||||
| 96 | App::cdnget::Worker::init($spares, $maxWorkers, $cachePath, $addr); | ||||||||||
| 97 | App::cdnget::Downloader::init($maxWorkers*10); | ||||||||||
| 98 | $SIG{INT} = $SIG{TERM} = sub | ||||||||||
| 99 | { | ||||||||||
| 100 | terminate(); | ||||||||||
| 101 | }; | ||||||||||
| 102 | log_info "Started ". | ||||||||||
| 103 | "spares=$spares ". | ||||||||||
| 104 | "max-workers=$maxWorkers ". | ||||||||||
| 105 | "cache-path=\"".shellmeta($cachePath)."\" ". | ||||||||||
| 106 | "addr=\"".shellmeta($addr)."\""; | ||||||||||
| 107 | while (not App::cdnget::Worker::terminated() or not App::cdnget::Downloader::terminated()) | ||||||||||
| 108 | { | ||||||||||
| 109 | eval { App::cdnget::Worker->new() }; | ||||||||||
| 110 | warn $@ if $@; | ||||||||||
| 111 | } | ||||||||||
| 112 | App::cdnget::Worker::final(); | ||||||||||
| 113 | App::cdnget::Downloader::final(); | ||||||||||
| 114 | }; | ||||||||||
| 115 | if ($@) | ||||||||||
| 116 | { | ||||||||||
| 117 | warn $@; | ||||||||||
| 118 | } | ||||||||||
| 119 | usleep(100*1000); | ||||||||||
| 120 | log_info "Terminated p5-cdnget/${App::cdnget::VERSION}"; | ||||||||||
| 121 | return 0; | ||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | sub run | ||||||||||
| 125 | { | ||||||||||
| 126 | return main(@ARGV); | ||||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | sub terminate | ||||||||||
| 130 | { | ||||||||||
| 131 | do | ||||||||||
| 132 | { | ||||||||||
| 133 | lock($terminating); | ||||||||||
| 134 | if ($terminating) | ||||||||||
| 135 | { | ||||||||||
| 136 | log_info "Terminating..."; | ||||||||||
| 137 | lock($terminating_force); | ||||||||||
| 138 | $terminating_force = 1; | ||||||||||
| 139 | return 0; | ||||||||||
| 140 | } | ||||||||||
| 141 | $terminating = 1; | ||||||||||
| 142 | }; | ||||||||||
| 143 | log_info "Terminating gracefully..."; | ||||||||||
| 144 | async { App::cdnget::Worker::terminate() }->detach(); | ||||||||||
| 145 | async { App::cdnget::Downloader::terminate() }->detach(); | ||||||||||
| 146 | return 1; | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | |||||||||||
| 150 | 1; | ||||||||||
| 151 | __END__ | ||||||||||