| 1 | package Net::Async::Statsd::Server; | ||||||||||||||||||||
| 2 | 4 | $Net::Async::Statsd::Server::VERSION = '0.004'; | |||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 4 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Net::Async::Statsd::Server - asynchronous server for Etsy's statsd protocol | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | Version 0.004 | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | use Future; | ||||||||||||||||||||
| 19 | use IO::Async::Loop; | ||||||||||||||||||||
| 20 | use Net::Async::Statsd::Server; | ||||||||||||||||||||
| 21 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 22 | $loop->add(my $statsd = Net::Async::Statsd::Server->new( | ||||||||||||||||||||
| 23 | port => 3001, | ||||||||||||||||||||
| 24 | )); | ||||||||||||||||||||
| 25 | $statsd->bus->subscribe_to_event( | ||||||||||||||||||||
| 26 | count => sub { | ||||||||||||||||||||
| 27 | my ($ev, $k, $delta, $type) = @_; | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | Provides an asynchronous server for the statsd API. | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 4 | ▶ | use curry; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 4 | ▶ | use Socket qw(SOCK_DGRAM); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 4 | ▶ | use IO::Socket::IP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 4 | ▶ | use IO::Async::Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 4 | ▶ | use Net::Async::Statsd::Bus; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | All public methods return a L<Future> indicating when the write has completed. | ||||||||||||||||||||
| 47 | Since writes are UDP packets, there is no guarantee that the remote will | ||||||||||||||||||||
| 48 | receive the value, so this is mostly intended as a way to detect when | ||||||||||||||||||||
| 49 | statsd writes are slow. | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head2 host | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | Which host to listen on. Probably want '0.0.0.0' (set via L</configure>) | ||||||||||||||||||||
| 56 | here if you want to listen on all addresses. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub host { shift->{host} } | ||||||||||||||||||
|
Subroutine
host: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head2 port | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | The UDP port we'll accept traffic on. Use L</configure> to set it. | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 8 | ▶ | sub port { shift->{port} } | ||||||||||||||||||
|
Subroutine
port: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =head2 configure | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | Used for setting values. | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | sub configure { | ||||||||||||||||||||
| 77 | 2 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||
|
Subroutine
configure: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 78 | 2 | for (qw(port host)) { | |||||||||||||||||||
| 79 | 4 | ▶ | $self->{$_} = delete $args{$_} if exists $args{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 2 | $self->SUPER::configure(%args); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head2 listening | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | Resolves with the port number when the UDP server is listening. | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | sub listening { | ||||||||||||||||||||
| 91 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
listening: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 92 | 2 | ▶ | $self->{listening} ||= do { | ||||||||||||||||||
|
Condition33%1/3
$self->{"listening"} ||= do {
$self->listen
}
MC/DC0%0/2
$self->{"listening"} ||= do {
$self->listen
}
$self->{"listening"}
do {
$self->listen
}
Truth table33%1/3
A ||= B
A= $self->{"listening"}B= do {
$self->listen
}
| |||||||||||||||||||||
| 93 | 2 | $self->listen | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head2 listen | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | Establishes the underlying UDP socket. | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub listen { | ||||||||||||||||||||
| 104 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
listen: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 2 | my $f = $self->loop->new_future; | |||||||||||||||||||
| 107 | 2 | ▶ | my $sock = IO::Socket::IP->new( | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->port // 0
MC/DC0%0/1
$self->port // 0
$self->port
Truth table50%1/2
A
A= $self->port
| |||||||||||||||||||||
| 108 | Proto => 'udp', | ||||||||||||||||||||
| 109 | ReuseAddr => 1, | ||||||||||||||||||||
| 110 | Type => SOCK_DGRAM, | ||||||||||||||||||||
| 111 | LocalPort => $self->port // 0, | ||||||||||||||||||||
| 112 | Listen => $self->listen_backlog, | ||||||||||||||||||||
| 113 | Blocking => 0, | ||||||||||||||||||||
| 114 | ) or die "No bind: $@\n"; | ||||||||||||||||||||
| 115 | 2 | $self->{port} = $sock->sockport; | |||||||||||||||||||
| 116 | 2 | my $ias = IO::Async::Socket->new( | |||||||||||||||||||
| 117 | handle => $sock, | ||||||||||||||||||||
| 118 | on_recv => $self->curry::on_recv, | ||||||||||||||||||||
| 119 | on_recv_error => $self->curry::on_recv_error, | ||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||
| 121 | 2 | $self->add_child($ias); | |||||||||||||||||||
| 122 | 2 | $f->done($self->port); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head2 bus | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | Returns the L<Net::Async::Statsd::Bus> instance for this server. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | This object exists purely for the purpose of dispatching events. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 12 | ▶ | sub bus { shift->{bus} ||= Net::Async::Statsd::Bus->new } | ||||||||||||||||||
|
Subroutine
bus: called CC=2 SCAR=7.2
Pod
documented
Condition66%2/3
+(shift())->{"bus"} ||= "Net::Async::Statsd::Bus"->new
MC/DC0%0/2
+(shift())->{"bus"} ||= "Net::Async::Statsd::Bus"->new
+(shift())->{"bus"}
"Net::Async::Statsd::Bus"->new
Truth table66%2/3
A ||= B
A= +(shift())->{"bus"}B= "Net::Async::Statsd::Bus"->new
| |||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head2 listen_backlog | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | Default listen backlog. Immutable, set to 4096 for no particular reason. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 2 | ▶ | sub listen_backlog { 4096 } | ||||||||||||||||||
|
Subroutine
listen_backlog: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | { | ||||||||||||||||||||
| 144 | 4 | my %type = ( | |||||||||||||||||||
| 145 | ms => 'timing', | ||||||||||||||||||||
| 146 | c => 'count', | ||||||||||||||||||||
| 147 | g => 'gauge', | ||||||||||||||||||||
| 148 | ); | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 type_for_char | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Badly-named lookup method - returns the type matching the given characters. | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | sub type_for_char { | ||||||||||||||||||||
| 157 | 6 | ▶ | my ($self, $char) = @_; | ||||||||||||||||||
|
Subroutine
type_for_char: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 158 | 6 | ▶ | die "no character?" unless defined $char; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 6 | return $type{$char}; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head2 on_recv | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Called if we receive data. | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 4 | sub on_recv { | |||||||||||||||||||
| 170 | 6 | ▶ | my ($self, undef, $dgram, $addr) = @_; | ||||||||||||||||||
|
Subroutine
on_recv: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||
| 171 | $self->loop->resolver->getnameinfo( | ||||||||||||||||||||
| 172 | addr => $addr, | ||||||||||||||||||||
| 173 | numeric => 1, | ||||||||||||||||||||
| 174 | dgram => 1, | ||||||||||||||||||||
| 175 | )->on_done(sub { | ||||||||||||||||||||
| 176 | 6 | ▶ | my ($host, $port) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||
| 177 | 6 | $self->debug_printf("UDP packet received from %s", join ':', $host, $port); | |||||||||||||||||||
| 178 | 6 | ▶ | my ($k, $v, $type_char, $rate) = $dgram =~ /^([^:]+):([^|]+)\|([^|]+)(?:\|\@(.+))?$/ or warn "Invalid dgram: $dgram"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 6 | ▶ | $rate ||= 1; | ||||||||||||||||||
|
Condition50%1/2
$rate ||= 1
MC/DC0%0/1
$rate ||= 1
$rate
Truth table50%1/2
A
A= $rate
| |||||||||||||||||||||
| 180 | 6 | ▶ | my $type = $self->type_for_char($type_char) // 'unknown'; | ||||||||||||||||||
|
Condition50%1/2
$self->type_for_char($type_char) // "unknown"
MC/DC0%0/1
$self->type_for_char($type_char) // "unknown"
$self->type_for_char($type_char)
Truth table50%1/2
A
A= $self->type_for_char($type_char)
| |||||||||||||||||||||
| 181 | 6 | $self->bus->invoke_event( | |||||||||||||||||||
| 182 | $type => ($k, $v, $rate, $host, $port) | ||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||
| 184 | 6 | $self->debug_printf( | |||||||||||||||||||
| 185 | "dgram %s from %s: %s => %s (%s)", | ||||||||||||||||||||
| 186 | $dgram, | ||||||||||||||||||||
| 187 | join(':', $host, $port), | ||||||||||||||||||||
| 188 | $k, | ||||||||||||||||||||
| 189 | $v, | ||||||||||||||||||||
| 190 | $type | ||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||
| 192 | 6 | }); | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =head2 on_recv_error | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | Called if we had an error while receiving. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | sub on_recv_error { | ||||||||||||||||||||
| 202 | 0 | ▶ | my ($self, undef, $err) = @_; | ||||||||||||||||||
|
Subroutine
on_recv_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 203 | 0 | $self->debug_printf("UDP packet receive error: %s", $err); | |||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 4 | 1; | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | __END__ | ||||||||||||||||||||