| 1 | # -*- coding: utf-8; -*- | ||||||||||||||||||||||||||||
| 2 | package Fluent::Logger; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | our $VERSION = '0.30'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Data::MessagePack; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use Scalar::Util qw/ refaddr /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use Time::HiRes qw/ time /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use UUID::Tiny qw/ create_uuid UUID_V4 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use MIME::Base64 qw/ encode_base64 /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 9 | ▶ | use constant RECONNECT_WAIT => 0.5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 9 | ▶ | use constant RECONNECT_WAIT_INCR_RATE => 1.5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 9 | ▶ | use constant RECONNECT_WAIT_MAX => 60; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use constant RECONNECT_WAIT_MAX_COUNT => 12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use constant MP_HEADER_3ELM_ARRAY => "\x93"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | use constant MP_HEADER_4ELM_ARRAY => "\x94"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use constant MP_HEADER_EVENT_TIME => "\xd7\x00"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | use subs 'prefer_integer'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | ▶ | use Class::Tiny +{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.3
| |||||||||||||||||||||||||||||
| 32 | 9 | ▶ | tag_prefix => sub {}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | host => sub { "127.0.0.1" }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | port => sub { 24224 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 9 | ▶ | socket => sub {}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 10 | ▶ | timeout => sub { 3.0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | buffer_limit => sub { 8 * 1024 * 1024 }, # fixme | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | buffer_overflow_handler => sub { undef }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | truncate_buffer_at_overflow => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | max_write_retry => sub { 5 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 7 | ▶ | write_length => sub { 8 * 1024 * 1024 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 20 | ▶ | socket_io => sub {}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | errors => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | prefer_integer => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 8 | ▶ | utf8 => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 9 | ▶ | packer => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 48 | 9 | my $mp = Data::MessagePack->new; | |||||||||||||||||||||||||||
| 49 | 9 | $mp->utf8( $self->utf8 ); | |||||||||||||||||||||||||||
| 50 | 9 | $mp->prefer_integer( $self->prefer_integer ); | |||||||||||||||||||||||||||
| 51 | 9 | $mp; | |||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | pending => sub { "" }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | connect_error_history => sub { +[] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | owner_pid => sub {}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 9 | ▶ | event_time => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | ack => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | pending_acks => sub { +[] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | unpacker => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | 0 | require Data::MessagePack::Stream; | |||||||||||||||||||||||||||
| 61 | 0 | Data::MessagePack::Stream->new; | |||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | selector => sub { }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 9 | ▶ | retry_immediately => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 9 | }; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 10 | $self->_connect; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 9 | ▶ | sub prefer_integer { | ||||||||||||||||||||||||||
|
Subroutine
prefer_integer: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 73 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 9 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 76 | 0 | $self->{prefer_integer} = shift; | |||||||||||||||||||||||||||
| 77 | 0 | $self->packer->prefer_integer( $self->prefer_integer ); | |||||||||||||||||||||||||||
| 78 | } elsif ( exists $self->{prefer_integer} ) { | ||||||||||||||||||||||||||||
| 79 | 4 | return $self->{prefer_integer}; | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 5 | my $defaults = Class::Tiny->get_all_attribute_defaults_for( ref $self ); | |||||||||||||||||||||||||||
| 82 | 5 | return $self->{prefer_integer} = $defaults->{prefer_integer}->(); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 28 | ▶ | sub _carp { | ||||||||||||||||||||||||||
|
Subroutine
_carp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 28 | my $msg = shift; | |||||||||||||||||||||||||||
| 89 | 28 | chomp $msg; | |||||||||||||||||||||||||||
| 90 | 28 | carp( | |||||||||||||||||||||||||||
| 91 | sprintf "%s %s[%s](%s): %s", | ||||||||||||||||||||||||||||
| 92 | localtime->strftime("%Y-%m-%dT%H:%M:%S%z"), | ||||||||||||||||||||||||||||
| 93 | ref $self, | ||||||||||||||||||||||||||||
| 94 | refaddr $self, | ||||||||||||||||||||||||||||
| 95 | $self->_connect_info, | ||||||||||||||||||||||||||||
| 96 | $msg, | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 24 | ▶ | sub _add_error { | ||||||||||||||||||||||||||
|
Subroutine
_add_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 24 | my $msg = shift; | |||||||||||||||||||||||||||
| 103 | 24 | $self->_carp($msg); | |||||||||||||||||||||||||||
| 104 | 24 | push @{ $self->errors }, $msg; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub errstr { | ||||||||||||||||||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 0 | return join ("\n", @{ $self->errors }); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 28 | ▶ | sub _connect_info { | ||||||||||||||||||||||||||
|
Subroutine
_connect_info: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 113 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 28 | ▶ | $self->socket || sprintf "%s:%d", $self->host, $self->port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 22 | ▶ | sub _connect { | ||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||
| 118 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 22 | my $force = shift; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 22 | ▶ | return if $self->socket_io && !$force; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->socket_io and not $force
MC/DC0%0/2
$self->socket_io and not $force
$self->socket_io
not $force
Truth table33%1/3
A and not(B)
A= $self->socket_ioB= not $force
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 22 | ▶ | my $sock = defined $self->socket | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | ? IO::Socket::UNIX->new( Peer => $self->socket ) | ||||||||||||||||||||||||||||
| 125 | : IO::Socket::INET->new( | ||||||||||||||||||||||||||||
| 126 | PeerAddr => $self->host, | ||||||||||||||||||||||||||||
| 127 | PeerPort => $self->port, | ||||||||||||||||||||||||||||
| 128 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 129 | Timeout => $self->timeout, | ||||||||||||||||||||||||||||
| 130 | ReuseAddr => 1, | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | 22 | ▶ | if (!$sock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 14 | $self->_add_error("Can't connect: $!"); | |||||||||||||||||||||||||||
| 134 | 14 | push @{ $self->connect_error_history }, time; | |||||||||||||||||||||||||||
| 135 | 14 | ▶ | if (@{ $self->connect_error_history } > RECONNECT_WAIT_MAX_COUNT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | shift @{ $self->connect_error_history }; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 14 | return; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 8 | $self->connect_error_history([]); | |||||||||||||||||||||||||||
| 141 | 8 | $self->owner_pid($$); | |||||||||||||||||||||||||||
| 142 | 8 | $self->selector(IO::Select->new($sock)); | |||||||||||||||||||||||||||
| 143 | 8 | $self->socket_io($sock); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 17 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 17 | ▶ | if ( length $self->{pending} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 2 | $self->_carp("flushing pending data on close"); | |||||||||||||||||||||||||||
| 151 | 2 | ▶ | $self->_connect unless $self->socket_io; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 2 | my $written = eval { | |||||||||||||||||||||||||||
| 153 | 2 | $self->_write( $self->{pending} ); | |||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||
| 155 | 2 | ▶ | if ($@ || !$written) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $written
MC/DC0%0/2
$@ or not $written
$@
not $written
Truth table33%1/3
A or not(B)
A= $@B= not $written
| |||||||||||||||||||||||||||||
| 156 | 2 | my $size = length $self->{pending}; | |||||||||||||||||||||||||||
| 157 | 2 | $self->_carp("Can't send pending data. LOST $size bytes.: $@"); | |||||||||||||||||||||||||||
| 158 | 2 | $self->_call_buffer_overflow_handler(); | |||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 0 | $self->_carp("pending data was flushed successfully"); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | 17 | $self->{pending} = ""; | |||||||||||||||||||||||||||
| 164 | 17 | $self->{pending_acks} = []; | |||||||||||||||||||||||||||
| 165 | 17 | delete $self->{selector}; | |||||||||||||||||||||||||||
| 166 | 17 | my $socket = delete $self->{socket_io}; | |||||||||||||||||||||||||||
| 167 | 17 | ▶ | $socket->close if $socket; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 33 | ▶ | sub post { | ||||||||||||||||||||||||||
|
Subroutine
post: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 33 | my($self, $tag, $msg) = @_; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 33 | ▶ | $self->_post( $tag || "", $msg, time() ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$tag || ''
MC/DC0%0/1
$tag || ''
$tag
Truth table50%1/2
A
A= $tag
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub post_with_time { | ||||||||||||||||||||||||||
|
Subroutine
post_with_time: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 0 | my ($self, $tag, $msg, $time) = @_; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | $self->_post( $tag || "", $msg, $time ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$tag || ''
MC/DC0%0/1
$tag || ''
$tag
Truth table0%0/2
A
A= $tag
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 33 | ▶ | sub _pack_time { | ||||||||||||||||||||||||||
|
Subroutine
_pack_time: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 183 | 33 | my ($self, $time) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 33 | ▶ | if ($self->event_time) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | my $time_i = int $time; | |||||||||||||||||||||||||||
| 187 | 0 | my $nanosec = int(($time - $time_i) * 10 ** 9); | |||||||||||||||||||||||||||
| 188 | 0 | return MP_HEADER_EVENT_TIME . pack("NN", $time_i, $nanosec); | |||||||||||||||||||||||||||
| 189 | } else { | ||||||||||||||||||||||||||||
| 190 | 33 | return $self->packer->pack(int $time); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 33 | ▶ | sub _post { | ||||||||||||||||||||||||||
|
Subroutine
_post: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||
| 195 | 33 | my ($self, $tag, $msg, $time) = @_; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 33 | ▶ | if (ref $msg ne "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | $self->_add_error("message '$msg' must be a HashRef"); | |||||||||||||||||||||||||||
| 199 | 0 | return; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 33 | ▶ | $tag = join('.', $self->tag_prefix, $tag) if $self->tag_prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 33 | my $p = $self->packer; | |||||||||||||||||||||||||||
| 204 | # tag must always be packed as a str (not an integer by prefer_integer, | ||||||||||||||||||||||||||||
| 205 | # nor a bin by utf8 => 0). fluentd drops events whose tag is an integer. | ||||||||||||||||||||||||||||
| 206 | 33 | ▶ | my $tp = $self->{_tag_packer} ||= Data::MessagePack->new->utf8(1); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_tag_packer"} ||= "Data::MessagePack"->new->utf8(1)
MC/DC0%0/2
$self->{"_tag_packer"} ||= "Data::MessagePack"->new->utf8(1)
$self->{"_tag_packer"}
"Data::MessagePack"->new->utf8(1)
Truth table66%2/3
A ||= B
A= $self->{"_tag_packer"}B= "Data::MessagePack"->new->utf8(1)
| |||||||||||||||||||||||||||||
| 207 | 33 | $self->_send( | |||||||||||||||||||||||||||
| 208 | $tp->pack("$tag"), | ||||||||||||||||||||||||||||
| 209 | $self->_pack_time($time), | ||||||||||||||||||||||||||||
| 210 | $p->pack($msg), | ||||||||||||||||||||||||||||
| 211 | ); | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 33 | ▶ | sub _send { | ||||||||||||||||||||||||||
|
Subroutine
_send: called CC=20 SCAR=32.4
| |||||||||||||||||||||||||||||
| 215 | 33 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 33 | my ($data, $unique_key); | |||||||||||||||||||||||||||
| 218 | 33 | ▶ | if ( $self->ack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 0 | $unique_key = encode_base64(create_uuid(UUID_V4)); | |||||||||||||||||||||||||||
| 220 | 0 | $data = join('', MP_HEADER_4ELM_ARRAY, @args, $self->{packer}->pack({ chunk => $unique_key })); | |||||||||||||||||||||||||||
| 221 | 0 | push @{$self->{pending_acks}}, $unique_key; | |||||||||||||||||||||||||||
| 222 | } else { | ||||||||||||||||||||||||||||
| 223 | 33 | $data = join('', MP_HEADER_3ELM_ARRAY, @args); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 33 | my $prev_size = length($self->{pending}); | |||||||||||||||||||||||||||
| 227 | 33 | my $current_size = length($data); | |||||||||||||||||||||||||||
| 228 | 33 | $self->{pending} .= $data; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 33 | my $errors = @{ $self->connect_error_history }; | |||||||||||||||||||||||||||
| 231 | 33 | ▶ | if ( $errors && length $self->pending <= $self->buffer_limit ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$errors and length $self->pending <= $self->buffer_limit
MC/DC100%2/2
$errors and length $self->pending <= $self->buffer_limit
$errors
length $self->pending <= $self->buffer_limit
Truth table100%3/3
A and B
A= $errorsB= length $self->pending <= $self->buffer_limit
| |||||||||||||||||||||||||||||
| 232 | { | ||||||||||||||||||||||||||||
| 233 | 16 | my $suppress_sec; | |||||||||||||||||||||||||||
| 234 | 16 | ▶ | if ( $errors < RECONNECT_WAIT_MAX_COUNT ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 16 | $suppress_sec = RECONNECT_WAIT * (RECONNECT_WAIT_INCR_RATE ** ($errors - 1)); | |||||||||||||||||||||||||||
| 236 | } else { | ||||||||||||||||||||||||||||
| 237 | 0 | $suppress_sec = RECONNECT_WAIT_MAX; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | 16 | ▶ | if ( time - $self->connect_error_history->[-1] < $suppress_sec ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 16 | return; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 17 | my ($written, $error); | |||||||||||||||||||||||||||
| 245 | 17 | for ( 0 .. $self->retry_immediately ) { | |||||||||||||||||||||||||||
| 246 | # check owner pid for fork safe | ||||||||||||||||||||||||||||
| 247 | 17 | ▶ | if (!$self->socket_io || $self->owner_pid != $$) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->socket_io or $self->owner_pid != $$
MC/DC50%1/2
not $self->socket_io or $self->owner_pid != $$
not $self->socket_io
$self->owner_pid != $$
Truth table66%2/3
not(A) or B
A= not $self->socket_ioB= $self->owner_pid != $$
| |||||||||||||||||||||||||||||
| 248 | 10 | $self->_connect(1); | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | 17 | eval { | |||||||||||||||||||||||||||
| 251 | 17 | $written = $self->_write( $self->{pending} ); | |||||||||||||||||||||||||||
| 252 | my $acked = $self->ack | ||||||||||||||||||||||||||||
| 253 | 0 | ▶ | ? $self->_wait_ack(@{ $self->{pending_acks} }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | : 1; | ||||||||||||||||||||||||||||
| 255 | 7 | ▶ | if ($written && $acked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$written and $acked
MC/DC0%0/2
$written and $acked
$written
$acked
Truth table33%1/3
A and B
A= $writtenB= $acked
| |||||||||||||||||||||||||||||
| 256 | 7 | $self->{pending} = ""; | |||||||||||||||||||||||||||
| 257 | 7 | $self->{pending_acks} = []; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | }; | ||||||||||||||||||||||||||||
| 260 | 17 | ▶ | if (!$@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 7 | return $written; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 10 | my $e = $@; | |||||||||||||||||||||||||||
| 264 | 10 | $error = "Cannot send data: $e"; | |||||||||||||||||||||||||||
| 265 | 10 | my $sock = delete $self->{socket_io}; | |||||||||||||||||||||||||||
| 266 | 10 | ▶ | $sock->close if $sock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 10 | delete $self->{selector}; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 10 | ▶ | if ( length($self->{pending}) > $self->buffer_limit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 10 | ▶ | if ( defined $self->buffer_overflow_handler ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 271 | 1 | $self->_call_buffer_overflow_handler(); | |||||||||||||||||||||||||||
| 272 | 1 | $self->{pending} = ""; | |||||||||||||||||||||||||||
| 273 | 1 | ▶ | $self->{pending_acks} = [] if $self->ack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | } elsif ( $self->truncate_buffer_at_overflow ) { | ||||||||||||||||||||||||||||
| 275 | 9 | substr($self->{pending}, $prev_size, $current_size, ""); | |||||||||||||||||||||||||||
| 276 | 0 | ▶ | pop @{$self->{pending_acks}} if $self->ack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 10 | ▶ | $self->_add_error($error) if defined $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 10 | return $written; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | sub _wait_ack { | ||||||||||||||||||||||||||
|
Subroutine
_wait_ack: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 286 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 287 | 0 | my @acks = @_; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 0 | my $up = $self->unpacker; | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | local $SIG{"PIPE"} = sub { die $! }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 291 | READ: | ||||||||||||||||||||||||||||
| 292 | 0 | while (1) { | |||||||||||||||||||||||||||
| 293 | 0 | my ($s) = $self->selector->can_read($self->timeout); | |||||||||||||||||||||||||||
| 294 | 0 | ▶ | if (!$s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 0 | die "ack read timed out"; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 0 | $s->sysread(my $buf, 1024); | |||||||||||||||||||||||||||
| 298 | 0 | ▶ | return if @acks > 0 && length($buf) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@acks > 0 and length $buf == 0
MC/DC0%0/2
@acks > 0 and length $buf == 0
@acks > 0
length $buf == 0
Truth table0%0/3
A and B
A= @acks > 0B= length $buf == 0
| |||||||||||||||||||||||||||||
| 299 | 0 | $up->feed($buf); | |||||||||||||||||||||||||||
| 300 | 0 | ▶ | while ($up->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 0 | my $ack = $up->data; | |||||||||||||||||||||||||||
| 302 | 0 | my $unique_key = shift @acks; | |||||||||||||||||||||||||||
| 303 | 0 | ▶ | if ($unique_key && ref $ack eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$unique_key and ref $ack eq "HASH"
MC/DC0%0/2
$unique_key and ref $ack eq "HASH"
$unique_key
ref $ack eq "HASH"
Truth table0%0/3
A and B
A= $unique_keyB= ref $ack eq "HASH"
| |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | if ($ack->{ack} ne $unique_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | die "ack is not expected: " . $ack->{ack}; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | } else { | ||||||||||||||||||||||||||||
| 308 | 0 | unshift @{ $self->{pending_acks} }, $unique_key; | |||||||||||||||||||||||||||
| 309 | 0 | die "Can't send data. ack is not expected. $@"; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | last READ if @acks == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | 0 | return 1; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 3 | ▶ | sub _call_buffer_overflow_handler { | ||||||||||||||||||||||||||
|
Subroutine
_call_buffer_overflow_handler: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 318 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 319 | 3 | ▶ | if (my $handler = $self->buffer_overflow_handler) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 2 | eval { | |||||||||||||||||||||||||||
| 321 | 2 | $handler->($self->{pending}); | |||||||||||||||||||||||||||
| 322 | }; | ||||||||||||||||||||||||||||
| 323 | 2 | ▶ | if (my $error = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | $self->_add_error("Can't call buffer overflow handler: $error"); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 19 | ▶ | sub _write { | ||||||||||||||||||||||||||
|
Subroutine
_write: called CC=6 SCAR=19.4
| |||||||||||||||||||||||||||||
| 330 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 331 | 19 | my $data = shift; | |||||||||||||||||||||||||||
| 332 | 19 | my $length = length($data); | |||||||||||||||||||||||||||
| 333 | 19 | my $retry = my $written = 0; | |||||||||||||||||||||||||||
| 334 | 19 | ▶ | die "Connection is not available" unless $self->socket_io; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | local $SIG{"PIPE"} = sub { die $! }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 7 | ▶ | while ($written < $length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 7 | my ($s) = $self->selector->can_write($self->timeout); | |||||||||||||||||||||||||||
| 340 | 7 | ▶ | die "send write timed out" unless $s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | 7 | my $nwrite | |||||||||||||||||||||||||||
| 342 | = $s->syswrite($data, $self->write_length, $written); | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 7 | ▶ | if (!$nwrite) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if ($retry > $self->max_write_retry) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 0 | die "failed write retry; max write retry count. $!"; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | 0 | $retry++; | |||||||||||||||||||||||||||
| 349 | } else { | ||||||||||||||||||||||||||||
| 350 | 7 | $written += $nwrite; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | 7 | $written; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 10 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 357 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 358 | 10 | $self->close; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 9 | 1; | |||||||||||||||||||||||||||
| 363 | __END__ | ||||||||||||||||||||||||||||