Class-Shared

 view release on metacpan or  search on metacpan

lib/Class/Shared/Backend.pm  view on Meta::CPAN

	if (!$client->{greeted} && defined $client->{greet_by}) {
		return @partial if @partial && $partial[0] < $client->{greet_by};
		return ($client->{greet_by}, $self->_no_hello_reason);
	}

	return @partial if @partial;

	# A reply still being written is write_timeout's business.
	return ($client->{last_request} + $self->{idle_timeout},
		"sent nothing for $self->{idle_timeout} second(s)")
		if $self->{idle_timeout} && defined $client->{last_request}
			&& !length $client->{outbuf};

	return ();
}

sub _no_hello_reason {
	my $self = shift;

	return "no accepted hello within $self->{handshake_timeout} second(s)";
}

# Drop connections that have not sent what they should have in time.  One that
# has bytes waiting to be read ($readable) is left alone: the backend may only
# just have come back from a long call, and those bytes are its progress.
sub _expire_reads {
	my ($self, $readable) = @_;

	my $now = Time::HiRes::time();

	for my $client (values %{$self->{clients}}) {
		next if $client->{closed};
		my ($deadline, $reason) = $self->_read_deadline($client);
		next unless defined $deadline;
		next if $deadline > $now;
		next if length $readable && vec($readable, fileno($client->{fh}), 1);

		$self->_drop_client($client, $reason);
	}

	return 1;
}

# Purpose:       keep the log from filling the disk.
# Entry:         max_log_size is a number of bytes, 0 for no limit.
# Exit status:   1.
# Side effects:  copies a log over the limit to LOG.1 (replacing it) and
#                empties the log.
# Why:           copied and emptied, not renamed: standard output and error of
#                the backend, which the target writes to, hold the file open, and
#                after a rename they would go on filling a file nobody can see.
#                A log that is a symbolic link or a device is left to whoever
#                made it so.
sub _trim_log {
	my $self = shift;

	my $limit = $self->{max_log_size};
	return 1 unless $limit && defined $self->{log};

	my $path = File::Spec->rel2abs($self->{log});
	return 1 unless lstat($path) && -f _ && -s _ > $limit;

	local ($!, $@);

	# From here on only this handle is read and emptied.  The name may come to
	# mean another file at any moment, so check that what was opened is what the
	# name meant: a plain file, not a link to one.
	open(my $log, '+<', $path) or return 1;
	my @opened = stat($log);
	my @named = lstat($path);
	return 1 unless @named && -f _ && $named[0] == $opened[0] && $named[1] == $opened[1];

	my $old = "$path.1";

	# Made anew, owner only, so that nothing already there is written through.
	unlink($old);
	if (sysopen(my $to, $old, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
		while (sysread($log, my $chunk, $READ_CHUNK)) {
			last unless syswrite($to, $chunk);
		}
		close($to);
	}

	# Emptied even if the copy failed: bounding the disk is the point.
	truncate($log, 0);
	close($log);

	return 1;
}

# Drop connections whose queued reply the socket has stopped taking, so a
# client that has stopped reading cannot sit on the memory it was allowed.
sub _expire_writes {
	my $self = shift;

	my $now = Time::HiRes::time();

	for my $client (values %{$self->{clients}}) {
		next if $client->{closed};
		next unless length $client->{outbuf};
		next unless defined $client->{stalled_since};
		next if $client->{stalled_since} + $self->{write_timeout} > $now;

		$self->_drop_client($client, 'a queued reply of ' . length($client->{outbuf})
			. ' bytes made no progress for ' . $self->{write_timeout} . ' second(s)');
	}

	return 1;
}

sub _accept {
	my $self = shift;

	my $fh = $self->{listener}->accept;
	if (!$fh) {
		if ($!{EMFILE} || $!{ENFILE} || $!{ENOBUFS} || $!{ENOMEM}) {
			# Logged once for each shortage, not once for each attempt.
			$self->_log("cannot accept a connection: $!", 'warn')
				unless $self->{accept_failing}++;
			$self->{accept_after} = Time::HiRes::time() + $ACCEPT_PAUSE;
		}
		return;
	}
	delete $self->{accept_failing};

	if (keys %{$self->{clients}} >= $self->{max_clients}) {
		# A unix client can be told, signed like every other frame.  A TCP
		# client has not handshaken yet, so there is no session to sign one
		# for: it is closed, and the frontend reports a failed connection.
		if (!defined $self->{host}) {



( run in 1.318 second using v1.01-cache-2.11-cpan-eb134014566 )