This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use 5.20.0; | |
use feature qw(postderef signatures); | |
use Mojolicious::Lite; | |
use PadWalker 'peek_sub'; | |
hook around_action => sub ($next, $c, $action, @) { | |
my @params = sort grep {s/^\$param_//} keys peek_sub($action)->%*; | |
return $c->$action($c->param(\@params)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Tested on OS X 10.9.2 (MacBook Air) with Perl 5.18.2 and EV 4.17 (kqueue) | |
# | |
# 267.0MB memory usage, 0% CPU usage once roundtrips are finished after 29s | |
# (with all 10k sockets still open) | |
# | |
# Get the latest version of Mojolicious from http://github.com/kraih/mojo | |
# $ sudo sysctl -w kern.maxfiles=40960 kern.maxfilesperproc=20480 | |
# $ ulimit -n 20480 | |
# $ LIBEV_FLAGS=8 perl c10k_client.pl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base -strict; | |
use Devel::Cycle; | |
use Mojo::UserAgent; | |
my $ua = Mojo::UserAgent->new; | |
$ua->get('mojolicio.us' => sub { | |
my ($ua, $tx) = @_; | |
say $tx->res->code; | |
$ua->get('mojolicio.us' => sub { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base -strict; | |
use Devel::Cycle; | |
use Mojo::UserAgent; | |
use Promises 'deferred'; | |
my $ua = Mojo::UserAgent->new; | |
sub fetch_it { | |
my $url = shift; | |
my $d = deferred(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
# Catch magical detach exception and rethrow everything else | |
hook around_dispatch => sub { | |
my ($next, $c) = @_; | |
return if eval { $next->(); 1 }; | |
die $@ unless $@ eq "MOJO_DETACH\n"; | |
}; | |
# Throw magical detach exception |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojolicious::Plugin::Coro; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
use Coro; | |
use Mojo::IOLoop; | |
# Wrap application in coroutine and reschedule main coroutine in event loop | |
sub register { | |
my ($self, $app) = @_; | |
my $subscribers = $app->plugins->subscribers('around_dispatch'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
use Coro; | |
use Mojo::IOLoop; | |
# Magical class for calling a method non-blocking (with a closure) and | |
# rescheduling the current coroutine until it is done | |
package with::coro { | |
use Coro; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Open 10k concurrent WebSocket connections with the client and server running | |
# in the same process and perform 10k WebSocket message roundtrips | |
# | |
# Tested on OS X 10.9.2 (MacBook Air) with Perl 5.18.2 and EV 4.17 (kqueue) | |
# | |
# 560.9MB memory usage, 0% CPU usage once roundtrips are finished after 36s | |
# (with all 20k sockets still open) | |
# | |
# Get the latest version of Mojolicious from http://github.com/kraih/mojo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojolicious::Lite; | |
use Mango; | |
use Mango::BSON ':bson'; | |
my $uri = 'mongodb://<user>:<pass>@<server>/<database>'; | |
helper mango => sub { state $mango = Mango->new($uri) }; | |
# Store and retrieve information non-blocking | |
get '/' => sub { | |
my $self = shift; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojolicious::Lite::MyKeywords; | |
use Mojo::Base -base; | |
use Mojo::Util 'monkey_patch'; | |
sub import { | |
my $caller = caller; | |
# Export "get_post" keyword (reuse "any" keyword to make "under" work) | |
monkey_patch $caller, 'get_post', sub { |