Skip to content

Instantly share code, notes, and snippets.

View kraih's full-sized avatar

Sebastian Riedel kraih

View GitHub Profile
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));
};
@kraih
kraih / c10k_client.pl
Last active September 15, 2015 20:51
#
# 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
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 {
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();
@kraih
kraih / detach.pl
Created September 18, 2013 00:46
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
@kraih
kraih / Coro.pm
Last active April 19, 2019 13:48
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');
#!/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;
@kraih
kraih / c10k.pl
Last active March 20, 2021 14:33
#
# 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
@kraih
kraih / mango_app.pl
Last active December 12, 2015 10:09
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;
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 {