init socket in constructor, to avoid overhead in DNS, etc.

This commit is contained in:
Steve Sanbeg 2012-01-24 11:54:46 -05:00
Родитель 8d7c6652e9
Коммит 9ecd4a7339
1 изменённых файлов: 11 добавлений и 9 удалений

Просмотреть файл

@ -2,6 +2,7 @@ package Etsy::StatsD;
use strict; use strict;
use warnings; use warnings;
use IO::Socket; use IO::Socket;
use Carp;
=head1 NAME =head1 NAME
@ -23,7 +24,14 @@ sub new {
my ($class, $host, $port, $sample_rate) = @_; my ($class, $host, $port, $sample_rate) = @_;
$host = 'localhost' unless defined $host; $host = 'localhost' unless defined $host;
$port = 8125 unless defined $port; $port = 8125 unless defined $port;
bless {host=>$host, port=>$port, sample_rate=>$sample_rate}, $class;
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'udp',
) or croak "Failed to initialize socket: $!";
bless {socket=>$sock, sample_rate=>$sample_rate}, $class;
} }
=item timing(STAT, TIME, SAMPLE_RATE) =item timing(STAT, TIME, SAMPLE_RATE)
@ -100,19 +108,13 @@ sub send {
return '0 but true' unless keys %$sampled_data; return '0 but true' unless keys %$sampled_data;
my $sock = new IO::Socket::INET(
PeerAddr => $self->{host},
PeerPort => $self->{port},
Proto => 'udp',
) or return undef;
#failures in any of this can be silently ignored #failures in any of this can be silently ignored
my $count=0; my $count=0;
my $socket = $self->{socket};
while (my($stat,$value) = each %$sampled_data) { while (my($stat,$value) = each %$sampled_data) {
print $sock "$stat:$value\n"; print $socket "$stat:$value\n";
++$count; ++$count;
} }
close $sock;
return $count; return $count;
} }