Skip to content

Instantly share code, notes, and snippets.

@s1037989
Last active April 10, 2020 13:56
Show Gist options
  • Select an option

  • Save s1037989/8993ddb90778befb51a4f334087a97ca to your computer and use it in GitHub Desktop.

Select an option

Save s1037989/8993ddb90778befb51a4f334087a97ca to your computer and use it in GitHub Desktop.
package Provider;
use Mojo::Base -base, -signatures;
has 'log' => sub { Mojo::Log->new };
has 'model';
has [qw/ident token/] => sub { die };
sub get_token {
my ($self, $ident) = @_;
$self->ident($ident)->token(_token($ident)) if $ident;
$self->log->debug(sprintf 'model.provider.get_token | %s / %s', $self->ident, $self->token);
return $self->token;
}
sub use_token {
my $self = shift;
$self->log->debug(sprintf 'model.provider.use_token | %s / %s', $self->ident, $self->token);
return;
}
sub stash { Mojo::Util::_stash(stash => @_) }
sub _token { substr(Mojo::Util::b64_encode(Mojo::Util::md5_sum(shift)), 0, 8) };
package Database;
use Mojo::Base -base;
has 'model';
package Model;
use Mojo::Base -base;
has
app =>
sub { $_[0]{app_ref} = Mojo::Server->new->build_app('Mojo::HelloWorld') },
weak => 1;
has 'database';
sub new {
my $self = shift->SUPER::new;
$self->database(Database->new->model($self));
}
sub provider { Provider->new->log(pop)->model(pop) }
package MyApp;
use Mojo::Base 'Mojolicious';
# This method will run once at server start
sub startup {
my $self = shift;
# Render Exceptions as JSON
$self->hook(before_render => sub {
my ($c, $args) = @_;
# Make sure we are rendering the exception template
return unless my $template = $args->{template};
return unless $template eq 'exception';
# Switch to JSON rendering if content negotiation allows it
return unless $c->accepts('json');
$args->{json} = {exception => $c->stash('exception')};
});
# Model
my $app = $self->app;
my $model = Model->new->app($app);
$self->helper(model => sub {$model});
# Router
my $r = $self->routes->under('/:ident' => {ident => ''} => sub {
my $c = shift;
my $ident = $c->param('ident') || $c->req->request_id;
my $provider = $c->model->provider($c->model, $c->log);
my $token = $provider->get_token($ident);
$c->log->debug(sprintf 'under.get_token | %s / %s', $ident, $token);
$c->stash(provider => $provider);
});
# Normal route to controller
$r->get('/' => sub {
my $c = shift->render_later;
Mojo::IOLoop->timer(1 => sub {
my $provider = $c->stash('provider');
$provider->use_token;
my $ident = $provider->ident;
my $token = $provider->token;
my $check_ident = $c->param('ident') || $c->req->request_id;
my $check_token = Provider::_token($check_ident);
die "ident: $ident - $check_token" unless $ident eq $check_ident;
die "token: $token - $check_token" unless $token eq $check_token;
$c->log->debug(sprintf 'route.use_token | %s / %s', $ident, $token);
$c->render(data => '', status => 204);
});
});
}
1;
$ (sleep 3 ; ab -n 10 -c 2 http://localhost:3000/) & perl script/my_app4 prefork -c 20
[2020-04-10 13:49:38.69484] [5047] [debug] [156ca5bd] GET "/"
[2020-04-10 13:49:38.69504] [5047] [debug] [156ca5bd] Routing to a callback
[2020-04-10 13:49:38.69513] [5047] [debug] [156ca5bd] model.provider.get_token | 156ca5bd / Yzc3ZjVj
[2020-04-10 13:49:38.69518] [5047] [debug] [156ca5bd] under.get_token | 156ca5bd / Yzc3ZjVj
[2020-04-10 13:49:38.69523] [5047] [debug] [156ca5bd] Routing to a callback
[2020-04-10 13:49:38.69548] [5048] [debug] [0793ff43] model.provider.use_token | 0793ff43 / NjE4M2Ey
[2020-04-10 13:49:38.69557] [5048] [debug] [0793ff43] route.use_token | 0793ff43 / NjE4M2Ey
[2020-04-10 13:49:38.69577] [5048] [debug] [0793ff43] 204 No Content (1.003934s, 0.996/s)
[2020-04-10 13:49:38.69601] [5048] [debug] [1a7286d6] model.provider.use_token | 1a7286d6 / ZmJmYTA0
[2020-04-10 13:49:38.69606] [5048] [debug] [1a7286d6] route.use_token | 1a7286d6 / ZmJmYTA0
[2020-04-10 13:49:38.69616] [5048] [debug] [1a7286d6] 204 No Content (1.001592s, 0.998/s)
[2020-04-10 13:49:38.69757] [5048] [debug] [156ca5bd] GET "/"
[2020-04-10 13:49:38.69776] [5048] [debug] [156ca5bd] Routing to a callback
[2020-04-10 13:49:38.69784] [5048] [debug] [156ca5bd] model.provider.get_token | 156ca5bd / Yzc3ZjVj
[2020-04-10 13:49:38.69788] [5048] [debug] [156ca5bd] under.get_token | 156ca5bd / Yzc3ZjVj
[2020-04-10 13:49:38.69793] [5048] [debug] [156ca5bd] Routing to a callback
[2020-04-10 13:49:38.69839] [5048] [debug] [11188e80] GET "/"
[2020-04-10 13:49:38.69851] [5048] [debug] [11188e80] Routing to a callback
[2020-04-10 13:49:38.69857] [5048] [debug] [11188e80] model.provider.get_token | 11188e80 / Y2FiZmY3
[2020-04-10 13:49:38.69860] [5048] [debug] [11188e80] under.get_token | 11188e80 / Y2FiZmY3
[2020-04-10 13:49:38.69864] [5048] [debug] [11188e80] Routing to a callback
[2020-04-10 13:49:38.69891] [5049] [debug] [f855bcbf] model.provider.use_token | f855bcbf / MDM2ODg5
[2020-04-10 13:49:38.69900] [5049] [debug] [f855bcbf] route.use_token | f855bcbf / MDM2ODg5
[2020-04-10 13:49:38.69919] [5049] [debug] [f855bcbf] 204 No Content (1.001848s, 0.998/s)
[2020-04-10 13:49:38.69984] [5047] [debug] [1a7286d6] model.provider.use_token | 1a7286d6 / ZmJmYTA0
[2020-04-10 13:49:38.69992] [5047] [debug] [1a7286d6] route.use_token | 1a7286d6 / ZmJmYTA0
[2020-04-10 13:49:38.70010] [5047] [debug] [1a7286d6] 204 No Content (1.006926s, 0.993/s)
[2020-04-10 13:49:38.70034] [5050] [debug] [25381b36] GET "/"
[2020-04-10 13:49:38.70068] [5050] [debug] [25381b36] Routing to a callback
[2020-04-10 13:49:38.70087] [5050] [debug] [25381b36] model.provider.get_token | 25381b36 / ZGMyOWU3
[2020-04-10 13:49:38.70088] [5049] [debug] [4bff5898] model.provider.use_token | 4bff5898 / MGQ0MTIx
[2020-04-10 13:49:38.70103] [5049] [debug] [4bff5898] route.use_token | 4bff5898 / MGQ0MTIx
[2020-04-10 13:49:38.70105] [5050] [debug] [25381b36] under.get_token | 25381b36 / ZGMyOWU3
[2020-04-10 13:49:38.70119] [5049] [debug] [4bff5898] 204 No Content (1.002743s, 0.997/s)
[2020-04-10 13:49:38.70125] [5050] [debug] [25381b36] Routing to a callback
[2020-04-10 13:49:38.70150] [5050] [debug] [aa652a7f] model.provider.use_token | aa652a7f / NDFlNmIw
[2020-04-10 13:49:38.70169] [5050] [debug] [aa652a7f] route.use_token | aa652a7f / NDFlNmIw
[2020-04-10 13:49:38.70182] [5047] [debug] [abc8c490] model.provider.use_token | abc8c490 / OGU3MDFh
[2020-04-10 13:49:38.70190] [5047] [debug] [abc8c490] route.use_token | abc8c490 / OGU3MDFh
[2020-04-10 13:49:38.70197] [5050] [debug] [aa652a7f] 204 No Content (1.003626s, 0.996/s)
[2020-04-10 13:49:38.70204] [5047] [debug] [abc8c490] 204 No Content (1.000826s, 0.999/s)
[2020-04-10 13:49:38.70598] [5048] [debug] [abc8c490] model.provider.use_token | abc8c490 / OGU3MDFh
[2020-04-10 13:49:38.70613] [5048] [debug] [abc8c490] route.use_token | abc8c490 / OGU3MDFh
[2020-04-10 13:49:38.70634] [5048] [debug] [abc8c490] 204 No Content (1.001536s, 0.998/s)
[2020-04-10 13:49:38.70690] [5048] [debug] [717ca790] model.provider.use_token | 717ca790 / YjkwNDY5
[2020-04-10 13:49:38.70698] [5048] [debug] [717ca790] route.use_token | 717ca790 / YjkwNDY5
[2020-04-10 13:49:38.70714] [5048] [debug] [717ca790] 204 No Content (1.001287s, 0.999/s)
[2020-04-10 13:49:38.70786] [5050] [debug] [0caa758d] model.provider.use_token | 0caa758d / MGI0MDRm
[2020-04-10 13:49:38.70804] [5050] [debug] [0caa758d] route.use_token | 0caa758d / MGI0MDRm
[2020-04-10 13:49:38.70825] [5050] [debug] [0caa758d] 204 No Content (1.002342s, 0.998/s)
[2020-04-10 13:49:38.70877] [5049] [debug] [0793ff43] model.provider.use_token | 0793ff43 / NjE4M2Ey
[2020-04-10 13:49:38.70887] [5049] [debug] [0793ff43] route.use_token | 0793ff43 / NjE4M2Ey
[2020-04-10 13:49:38.70896] [5050] [debug] [d9a2bdd2] model.provider.use_token | d9a2bdd2 / OWViMjNk
[2020-04-10 13:49:38.70902] [5049] [debug] [0793ff43] 204 No Content (1.002475s, 0.998/s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment