Async-Microservice
view release on metacpan or search on metacpan
lib/Async/Microservice/Time.pm view on Meta::CPAN
my $years_counter = $counter_dt->in_units('years');
my $date_from_str = $s_date_dt->strftime('%Y-%m-%d');
my $date_to_str = $r_age_dt->strftime('%Y-%m-%d');
return {
msg => sprintf(
'%d weeks left until date span age of %d (date from %s to %s)',
$week_counter, $r_age, $date_from_str, $date_to_str,
),
days => $days_counter,
weeks => $week_counter,
months => $months_counter,
years => $years_counter,
date_from => $date_from_str,
date_to => $date_to_str,
( $m_income ? ( income => $months_counter * $m_income ) : () ),
};
}
sub _datetime_as_data {
my ($dt) = @_;
return {
datetime => $dt->strftime('%Y-%m-%d %H:%M:%S %z'),
date => $dt->strftime('%Y-%m-%d'),
time => $dt->strftime('%H:%M:%S'),
time_zone => $dt->strftime('%z'),
time_zone_name => $dt->strftime('%Z'),
day => $dt->strftime('%d'),
month => $dt->strftime('%m'),
year => $dt->strftime('%Y'),
hour => $dt->strftime('%H'),
minute => $dt->strftime('%M'),
second => $dt->strftime('%S'),
epoch => $dt->epoch,
};
}
__PACKAGE__->meta->make_immutable;
no Moose;
1;
__END__
=head1 NAME
Async::Microservice::Time - example time async microservice
=head1 SYNOPSIS
# can be started using:
plackup --port 8085 -Ilib --access-log /dev/null --server Twiggy bin/async-microservice-time.psgi
curl "http://localhost:8085/v1/hcheck" -H "accept: application/json"
curl "http://localhost:8085/v1/epoch" -H "accept: application/json"
curl "http://localhost:8085/v1/datetime?time_zone=local" -H "accept: application/json"
=head1 DESCRIPTION
This is an example asynchronous HTTP microservice using
L<Async::Microservice>. View the source code; it's minimal.
=head1 METHODS
=head2 service_name
Just a name, used to identify process and look for OpenAPI documentation.
=head2 get_routes
L<Path::Router> configuration for dispatching
=head2 http response methods
=head3 GET_datetime
L<https://time.meon.eu/v1/datetime>
=head3 POST_datetime
$ curl -X POST "https://time.meon.eu/v1/datetime" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"epoch\":-42}"
{
"date" : "1969-12-31",
"datetime" : "1969-12-31 23:59:18 +0000",
"day" : "31",
"epoch" : -42,
"hour" : "23",
"minute" : "59",
"month" : "12",
"second" : "18",
"time" : "23:59:18",
"time_zone" : "+0000",
"time_zone_name" : "UTC",
"year" : "1969"
}
=head3 GET_epoch
L<https://time.meon.eu/v1/epoch>
=head3 GET_sleep
L<https://time.meon.eu/v1/sleep?duration=2.5>
This is the only response method processed in parallel (the other ones are
pure CPU-bound) that sleeps for a given (or random) number of seconds and
only then returns the request response with when it started and how long
it took. Normally this is the same as what is in the duration parameter,
but in case the server is overloaded with requests, the event loop may call the
timer handler much later than the duration. Try:
ab -n 1000 -c 500 http://localhost:8085/v1/sleep?duration=3
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 259 432.8 21 1033
Processing: 3001 3090 72.5 3061 3253
Waiting: 3001 3090 72.5 3061 3253
Total: 3022 3349 394.1 3155 4065
Then try to run together with 100% CPU load:
( run in 0.820 second using v1.01-cache-2.11-cpan-39bf76dae61 )