Exploiting the newer perl to improve your plugins
-
Upload
marian-marinov -
Category
Technology
-
view
890 -
download
3
Transcript of Exploiting the newer perl to improve your plugins
.1H com.1H com.1H com.1H com
Exploit ing the Newer Perl to Improve Your Plugins
- .Marian Marinov mm@1h com - .Marian Marinov mm@1h com- & .Co founder CEO of 1H Ltd- & .Co founder CEO of 1H Ltd
. .WWW 1H COM. .WWW 1H COM 22
AGENDAAGENDA
- Building feature rich plug ins the easy way - Building feature rich plug ins the easy way Queuing for the masses Queuing for the masses
Cache me please Cache me please
. .WWW 1H COM. .WWW 1H COM 33
$ $$ $$$ $$ $ $ $$ $$$ $$ $
, If you rate my survey I' l l hook you up , If you rate my survey I' l l hook you up .with $20 cPCache $$$ .with $20 cPCache $$$
:Go to this address to take the survey :Go to this address to take the survey :// . . /http go cpanel net b27:// . . /http go cpanel net b27
and come up to the podium once you've and come up to the podium once you've .completed it .completed it
. .WWW 1H COM. .WWW 1H COM 44
- Plug ins creation- Plug ins creation
jQueryjQuery::JavaScript Packer::JavaScript Packer
::JSON XS::JSON XS
. .WWW 1H COM. .WWW 1H COM 55
- cPanel plug ins with - cPanel plug ins withjQueryjQuery
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict;use strict;use JQuery::Demo;use JQuery::Demo;use JQuery::CSS;use JQuery::CSS;use CGI;use CGI;
package main;package main;my $tester = new JQuery::Demo;my $tester = new JQuery::Demo;$tester->run;$tester->run;
package JQuery::Demo;package JQuery::Demo;use JQuery::Tabs;use JQuery::Tabs;
. .WWW 1H COM. .WWW 1H COM 66
- cPanel plug ins with - cPanel plug ins withjQueryjQuery
sub start {sub start { my $my = shift;my $my = shift; my $q = new CGI;my $q = new CGI; $my->{info}{TITLE} = "Tabs";$my->{info}{TITLE} = "Tabs"; my $jquery = $my->{jquery};my $jquery = $my->{jquery}; my @tabs = ("tab 1","tab 2","tab 3","tab 4");my @tabs = ("tab 1","tab 2","tab 3","tab 4"); my @texts = ("line 1","line 2","line 3","line4");my @texts = ("line 1","line 2","line 3","line4");
. .WWW 1H COM. .WWW 1H COM 77
- cPanel plug ins with - cPanel plug ins withjQueryjQuery
my $tab = JQuery::Tabs->new(id => 'myTab',my $tab = JQuery::Tabs->new(id => 'myTab', addToJQuery => $jquery,addToJQuery => $jquery, tabs => \@tabs,tabs => \@tabs, remote => 'true', remote => 'true', remoteProgram => 'jquery_tabs_results.pl',remoteProgram => 'jquery_tabs_results.pl', rm => 'myMode',rm => 'myMode', spinner => 1 );spinner => 1 ); my $html = $tab->HTML;my $html = $tab->HTML; $my->{info}{BODY} = qq[<h1>START OF TAB EXAMPLE</$my->{info}{BODY} = qq[<h1>START OF TAB EXAMPLE</h1>$html</div>END OF EXAMPLE</h1>];h1>$html</div>END OF EXAMPLE</h1>];
}}
. .WWW 1H COM. .WWW 1H COM 88
- cPanel plug ins with - cPanel plug ins withjQueryjQuery
. .WWW 1H COM. .WWW 1H COM 99
: :JavaScript Packer: :JavaScript Packer
$ wc -l uncompressed.js $ wc -l uncompressed.js 1078 uncompressed.js1078 uncompressed.js$ ls -l uncompressed.js $ ls -l uncompressed.js -rw-r--r-- 1 hackman hackman 31609 Oct 8 00:43 -rw-r--r-- 1 hackman hackman 31609 Oct 8 00:43 uncompressed.jsuncompressed.js
$ ./minify-js.pl $ ./minify-js.pl $ ls -l compressed.js $ ls -l compressed.js -rw-rw-r-- 1 hackman hackman 15847 Oct 8 00:43 -rw-rw-r-- 1 hackman hackman 15847 Oct 8 00:43 compressed.jscompressed.js
$ wc -l compressed.js $ wc -l compressed.js 0 compressed.js0 compressed.js
. .WWW 1H COM. .WWW 1H COM 1010
: :JavaScript Packer: :JavaScript Packer
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict;use strict;use JavaScript::Packer;use JavaScript::Packer;my $packer = JavaScript::Packer->init();my $packer = JavaScript::Packer->init();open( UNCOMPRESSED, 'uncompressed.js' );open( UNCOMPRESSED, 'uncompressed.js' );open( COMPRESSED, '>compressed.js' );open( COMPRESSED, '>compressed.js' );my $js = join( '', <UNCOMPRESSED> );my $js = join( '', <UNCOMPRESSED> );$packer->minify( \$js, { compress => 'best' });$packer->minify( \$js, { compress => 'best' });print COMPRESSED $js;print COMPRESSED $js;close(UNCOMPRESSED);close(UNCOMPRESSED);close(COMPRESSED);close(COMPRESSED);
. .WWW 1H COM. .WWW 1H COM 1111
Why JSON and why Why JSON and why: :JSON XS: :JSON XS
We need to transfer data We need to transfer data XML is EVIL XML is EVIL The best format with the least overhead The best format with the least overhead The XS version is obviously faster The XS version is obviously faster
. .WWW 1H COM. .WWW 1H COM 1212
Example with JSON Example with JSON
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict;use strict;use JSON::XS;use JSON::XS;my %res = ( 'a' => 'line1', 'b' => 'line2' );my %res = ( 'a' => 'line1', 'b' => 'line2' );my $json = JSON::XS->new->ascii->pretty-my $json = JSON::XS->new->ascii->pretty->allow_nonref;>allow_nonref;
print $json->encode(\%res);print $json->encode(\%res);
$ ./json-ex.pl $ ./json-ex.pl {"a":"line1","b":"line2"}{"a":"line1","b":"line2"}
. .WWW 1H COM. .WWW 1H COM 1313
Why queuing is helpful Why queuing is helpful
PerformancePerformance No need to handle your own forks No need to handle your own forks
Queing slow commands Queing slow commands
" " The data is HOT " " The data is HOT
ConcurrencyConcurrency" " HOT connections" " HOT connections
No need to think about the position in a fi le No need to think about the position in a fi le
SecuritySecurity You have to encode and check the data sent You have to encode and check the data sent to the functions to the functions
. .WWW 1H COM. .WWW 1H COM 1414
Introduce Queuing Introduce Queuing
GearmanGearman - :// .http gearman org- :// .http gearman orgRabbitMQRabbitMQ - :// . .http www rabbitmq com- :// . .http www rabbitmq com
. .WWW 1H COM. .WWW 1H COM 1515
Gearman Architecture Gearman Architecture
- Server handles the queues - Server handles the queues - Client adds requests to the queue - Client adds requests to the queue - Worker handles requests from the queue - Worker handles requests from the queue
gearmand
Web App Daemon
WorkerClient
. .WWW 1H COM. .WWW 1H COM 1616
Gearman client Gearman client
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict;use strict;use Gearman::Client;use Gearman::Client;my $client = Gearman::Client->new;my $client = Gearman::Client->new;$client->job_servers('127.0.0.1:4730');$client->job_servers('127.0.0.1:4730');$str = "my client has called";$str = "my client has called";eval {eval { local $SIG{ALRM} = sub { die 'timeout'; };local $SIG{ALRM} = sub { die 'timeout'; }; alarm(3);alarm(3); $client->dispatch_background('log_me', $str);$client->dispatch_background('log_me', $str); alarm(0); alarm(0); };};
. .WWW 1H COM. .WWW 1H COM 1717
Gearman worker Gearman worker
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict;use strict;use Gearman::Worker;use Gearman::Worker;use POSIX qw(strftime);use POSIX qw(strftime);$logfile = '/home/hackman/test.log';$logfile = '/home/hackman/test.log';open LOG, '>>', $logfile;open LOG, '>>', $logfile;select((select(LOG), $| = 1)[0]);select((select(LOG), $| = 1)[0]);my $logger = sub { ... };my $logger = sub { ... };my $w = Gearman::Worker->new;my $w = Gearman::Worker->new;$w->job_servers('127.0.0.1:4730');$w->job_servers('127.0.0.1:4730');$w->register_function( log_me => $logger );$w->register_function( log_me => $logger );$w->work while 1;$w->work while 1;
. .WWW 1H COM. .WWW 1H COM 1818
Gearman setup Gearman setup
# make && make install# make && make install# gearmand -d -u gearman -p 4730# gearmand -d -u gearman -p 4730
# gearman_status.pl # gearman_status.pl function name queue exec workerfunction name queue exec worker log_me 1 1 1log_me 1 1 1 check_oneh 0 0 1check_oneh 0 0 1 gather_child 154 72 72gather_child 154 72 72
. .WWW 1H COM. .WWW 1H COM 1919
RabbitMQ architecture RabbitMQ architecture
RabbitMQ
Queue
Exchange
Server
Server
QueueServer
. .WWW 1H COM. .WWW 1H COM 2020
RabbitMQ architecture RabbitMQ architecture
RabbitMQ
AMQP XMPP STOMP HTTP
AMQP XMPP STOMP HTTP
. .WWW 1H COM. .WWW 1H COM 2121
RabbitMQ setup RabbitMQ setup
# yum install erlang# yum install erlang# - -rpm Uvh rabbitmq# - -rpm Uvh rabbitmq
- . . - . .server 2 6 1 1 noarch rpm- . . - . .server 2 6 1 1 noarch rpm
# - -rabbitmq server detached# - -rabbitmq server detached# rabbitmqctl status# rabbitmqctl status
...Status of node rabbit@gamelon ...Status of node rabbit@gamelon[{ , }, { ,pid 6829 running_applications[{ , }, { ,pid 6829 running_applications [{ ," "," . . "},rabbit RabbitMQ 2 6 1[{ ," "," . . "},rabbit RabbitMQ 2 6 1 ... .. . . .
. .WWW 1H COM. .WWW 1H COM 2222
RabbitMQ client RabbitMQ client
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict;use strict;use Net::RabbitMQ;use Net::RabbitMQ;my $mq = Net::RabbitMQ->new();my $mq = Net::RabbitMQ->new();$mq->connect("localhost", { user => "guest", password $mq->connect("localhost", { user => "guest", password => "guest" });=> "guest" });
$mq->channel_open(1);$mq->channel_open(1);$mq->publish(1, "testqueue", "Hi there!");$mq->publish(1, "testqueue", "Hi there!");$mq->disconnect();$mq->disconnect();
2323
RabbitMQ server RabbitMQ server
#!/usr/bin/perl -w#!/usr/bin/perl -wuse strict; use Data::Dumperuse strict; use Data::Dumper;; use Net::RabbitMQ; use Net::RabbitMQ;my $mq = Net::RabbitMQ->new();my $mq = Net::RabbitMQ->new();$mq->connect("localhost",$mq->connect("localhost",{user=>"guest",password=>"guest"});{user=>"guest",password=>"guest"});
$mq->channel_open(1);$mq->channel_open(1);$mq->exchange_declare(1, 'my_x',{auto_delete => 0});$mq->exchange_declare(1, 'my_x',{auto_delete => 0});$mq->queue_declare(1, 'testqueue',{auto_delete => $mq->queue_declare(1, 'testqueue',{auto_delete => 0});0});
$mq->queue_bind(1, 'testqueue', 'my_x', 'foo');$mq->queue_bind(1, 'testqueue', 'my_x', 'foo');while(1){while(1){ my $hashref = $mq->get(1, 'testqueue');my $hashref = $mq->get(1, 'testqueue'); print Dumper($hashref) if (defined($hashref));print Dumper($hashref) if (defined($hashref));}}
. .WWW 1H COM. .WWW 1H COM 2424
CachingCaching
Why cache ? Why cache ?
::Cache memcached::Cache memcached:: - Cache Redis written especially for this:: - Cache Redis written especially for this
:)talk :)talk
. .WWW 1H COM. .WWW 1H COM 2525
: :Cache Memcached: :Cache Memcached
memcached
CGI CGI CGI CGI CGI
. .WWW 1H COM. .WWW 1H COM 2626
: :Cache Redis: :Cache Redis
. .WWW 1H COM. .WWW 1H COM 2727
ConclusionsConclusions
. .WWW 1H COM. .WWW 1H COM 2828
QuestionsQuestions
? ? ? ? ? ???
? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
. .WWW 1H COM. .WWW 1H COM 2929
Thank you Thank you
Please visit us at Booth 23 Please visit us at Booth 23
- .Marian Marinov mm@1h com - .Marian Marinov mm@1h com- & .Co founder CIO at 1H Ltd- & .Co founder CIO at 1H Ltd
.1H com.1H com.1H com.1H com
. .WWW 1H COM. .WWW 1H COM 3030
$ $$ $$$ $$ $ $ $$ $$$ $$ $
, If you rate my survey I' l l hook you up , If you rate my survey I' l l hook you up .with $20 cPCache $$$ .with $20 cPCache $$$
:Go to this address to take the survey :Go to this address to take the survey :// . . /http go cpanel net b27:// . . /http go cpanel net b27
and come up to the podium once you've and come up to the podium once you've .completed it .completed it