<commit_before>package Grpc::Client::BaseStub;
use strict;
use warnings;

## Base class for generated client stubs. Stub methods are expected to call
## _simpleRequest or _streamRequest and return the result.

use Grpc::XS;
use Grpc::XS::Channel;
use Grpc::XS::Timeval;

use Grpc::Constants;

use Grpc::Client::UnaryCall;
use Grpc::Client::ClientStreamingCall;
use Grpc::Client::ServerStreamingCall;
use Grpc::Client::BidiStreamingCall;

use constant true  => 1;
use constant false => 0;

## params:
##    - 'hostname': string
##    - 'update_metadata': (optional) a callback function which takes in a
##                         metadata array, and returns an updated metadata array
##    - 'grpc.primary_user_agent': (optional) a user-agent string

sub new {
	my $proto    = shift;
	my $hostname = shift;
	my %param		 = @_;
	my $update_metadata    = $param{"update_metadata"};
	my $primary_user_agent = $param{"grpc.primary_user_agent"};
	my $credentials        = $param{"credentials"};

	if (defined($primary_user_agent)) {
		$primary_user_agent .= " ";
	} else {
		$primary_user_agent = "";
	}
	$primary_user_agent = "grpc-perl/".($Grpc::XS::VERSION);

	if (!exists($param{"credentials"})) {
		die("The 'credentials' key is now required. Please see one of the ".
			  "Grpc::XS::ChannelCredentials::create methods");
	} elsif (!defined($param{"credentials"})) {
		delete $param{"credentials"};
	}

	my $channel = new Grpc::XS::Channel($hostname,%param);

	my $self = {
		'_hostname'           => $hostname,
		'_channel'            => undef,
		'_update_metadata'    => $update_metadata,	## a callback function
		'_primary_user_agent' => $primary_user_agent,
		'_credentials'        => $credentials,
		'_channel'            => $channel,
	};
	bless $self,$proto;

	return $self;
}

## return string The URI of the endpoint.

sub getTarget {
	my $self = shift;
	return $self->{_channel}->getTarget();
}

## $try_to_connect bool
## return int The grpc connectivity state

sub getConnectivityState {
	my $self = shift;
	my $try_to_connect = shift||false;
	return $self->{_channel}->getConnectivityState($try_to_connect);
}

## $timeout in microseconds
## return bool true if channel is ready
## dies if channel is in FATAL_ERROR state

sub waitForReady {
	my $self = shift;
	my $timeout = shift;

    my $new_state = $self->getConnectivityState(true);
    if ($self->_checkConnectivityState($new_state)) {
    	return true;
	}

  my $now = Grpc::XS::Timeval::now();
 	my $delta = new Grpc::XS::Timeval($timeout);
  my $deadline = Grpc::XS::Timevall::add($now,$delta);

  while ($self->{_channel}->watchConnectivityState($new_state, $deadline)) {
		## state has changed before deadline
		$new_state = $self->getConnectivityState();
	  if ($self->_checkConnectivityState($new_state)) {
			return true;
	  }
	}

  ## deadline has passed
  $new_state = $self->getConnectivityState();

	return $self->_checkConnectivityState($new_state);
}

sub _checkConnectivityState {
	my $self = shift;
	my $new_state = shift;

	if ($new_state == Grpc::Constants::GRPC_CHANNEL_READY()){
    	return true;
	}
	if ($new_state == Grpc::Constants::GRPC_CHANNEL_FATAL_FAILURE()){
    	die('Failed to connect to server');
	}

	return false;
}

## Close the communication channel associated with this stub.<commit_msg>Fix timeout typo<commit_after>package Grpc::Client::BaseStub;
use strict;
use warnings;

## Base class for generated client stubs. Stub methods are expected to call
## _simpleRequest or _streamRequest and return the result.

use Grpc::XS;
use Grpc::XS::Channel;
use Grpc::XS::Timeval;

use Grpc::Constants;

use Grpc::Client::UnaryCall;
use Grpc::Client::ClientStreamingCall;
use Grpc::Client::ServerStreamingCall;
use Grpc::Client::BidiStreamingCall;

use constant true  => 1;
use constant false => 0;

## params:
##    - 'hostname': string
##    - 'update_metadata': (optional) a callback function which takes in a
##                         metadata array, and returns an updated metadata array
##    - 'grpc.primary_user_agent': (optional) a user-agent string

sub new {
	my $proto    = shift;
	my $hostname = shift;
	my %param		 = @_;
	my $update_metadata    = $param{"update_metadata"};
	my $primary_user_agent = $param{"grpc.primary_user_agent"};
	my $credentials        = $param{"credentials"};

	if (defined($primary_user_agent)) {
		$primary_user_agent .= " ";
	} else {
		$primary_user_agent = "";
	}
	$primary_user_agent = "grpc-perl/".($Grpc::XS::VERSION);

	if (!exists($param{"credentials"})) {
		die("The 'credentials' key is now required. Please see one of the ".
			  "Grpc::XS::ChannelCredentials::create methods");
	} elsif (!defined($param{"credentials"})) {
		delete $param{"credentials"};
	}

	my $channel = new Grpc::XS::Channel($hostname,%param);

	my $self = {
		'_hostname'           => $hostname,
		'_channel'            => undef,
		'_update_metadata'    => $update_metadata,	## a callback function
		'_primary_user_agent' => $primary_user_agent,
		'_credentials'        => $credentials,
		'_channel'            => $channel,
	};
	bless $self,$proto;

	return $self;
}

## return string The URI of the endpoint.

sub getTarget {
	my $self = shift;
	return $self->{_channel}->getTarget();
}

## $try_to_connect bool
## return int The grpc connectivity state

sub getConnectivityState {
	my $self = shift;
	my $try_to_connect = shift||false;
	return $self->{_channel}->getConnectivityState($try_to_connect);
}

## $timeout in microseconds
## return bool true if channel is ready
## dies if channel is in FATAL_ERROR state

sub waitForReady {
	my $self = shift;
	my $timeout = shift;

    my $new_state = $self->getConnectivityState(true);
    if ($self->_checkConnectivityState($new_state)) {
    	return true;
	}

  my $now = Grpc::XS::Timeval::now();
 	my $delta = new Grpc::XS::Timeval($timeout);
  my $deadline = Grpc::XS::Timeval::add($now,$delta);

  while ($self->{_channel}->watchConnectivityState($new_state, $deadline)) {
		## state has changed before deadline
		$new_state = $self->getConnectivityState();
	  if ($self->_checkConnectivityState($new_state)) {
			return true;
	  }
	}

  ## deadline has passed
  $new_state = $self->getConnectivityState();

	return $self->_checkConnectivityState($new_state);
}

sub _checkConnectivityState {
	my $self = shift;
	my $new_state = shift;

	if ($new_state == Grpc::Constants::GRPC_CHANNEL_READY()){
    	return true;
	}
	if ($new_state == Grpc::Constants::GRPC_CHANNEL_FATAL_FAILURE()){
    	die('Failed to connect to server');
	}

	return false;
}

## Close the communication channel associated with this stub.
{'fraction_non_alphanumeric': 0.1452446065025828, 'fraction_numerical': 0.0006077180188392585, 'mean_word_length': 4.382665576451349, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2945121', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2140, 'n_words': 541}