Skip to content

Commit

Permalink
Merge pull request #137 from aneterial/grpc_opts_feature add grpc opt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
rekby authored Apr 16, 2024
2 parents 5fdd2e4 + 499e8b9 commit 78b5db4
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* added `$grpc_config` array for customize gRPC behavior

## 1.14.0
* added `ScanQueryMode` for `Table::scanQuery`

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,22 @@ $config = [
$ydb = new \YdbPlatform\Ydb\Ydb($config);
```

## gRPC

### gRPC client's options
You can customize the gRPC client's behavior by setting options in config array

Example of using:
```php
$config = [
// ...
'grpc' => [
'opts' => [
'grpc.max_receive_message_length' => 8*1024*1024,
'grpc.default_compression_algorithm' => 2,
'grpc.default_compression_level' => 2,
],
],
];
$ydb = new \YdbPlatform\Ydb\Ydb($config);
```
4 changes: 1 addition & 3 deletions src/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public function __construct(Ydb $ydb, $logger)
{
$this->ydb = $ydb;
$this->logger = $logger;
$this->client = new ServiceClient($ydb->endpoint(), [
'credentials' => $ydb->iam()->getCredentials(),
]);
$this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts());
$this->credentials = $ydb->iam();
$this->meta = [
'x-ydb-database' => [$ydb->database()],
Expand Down
4 changes: 1 addition & 3 deletions src/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
{
$this->ydb = $ydb;

$this->client = new ServiceClient($ydb->endpoint(), [
'credentials' => $ydb->iam()->getCredentials(),
]);
$this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts());

$this->meta = $ydb->meta();

Expand Down
4 changes: 1 addition & 3 deletions src/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
{
$this->ydb = $ydb;

$this->client = new ServiceClient($ydb->endpoint(), [
'credentials' => $ydb->iam()->getCredentials(),
]);
$this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts());

$this->meta = $ydb->meta();

Expand Down
4 changes: 1 addition & 3 deletions src/Scheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
{
$this->ydb = $ydb;

$this->client = new ServiceClient($ydb->endpoint(), [
'credentials' => $ydb->iam()->getCredentials(),
]);
$this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts());

$this->meta = $ydb->meta();

Expand Down
4 changes: 1 addition & 3 deletions src/Scripting.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null)
{
$this->ydb = $ydb;

$this->client = new ServiceClient($ydb->endpoint(), [
'credentials' => $ydb->iam()->getCredentials(),
]);
$this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts());

$this->meta = $ydb->meta();

Expand Down
4 changes: 1 addition & 3 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null, Retry &$re
{
$this->ydb = $ydb;

$this->client = new ServiceClient($ydb->endpoint(), [
'credentials' => $ydb->iam()->getCredentials(),
]);
$this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts());

$this->meta = $ydb->meta();

Expand Down
4 changes: 1 addition & 3 deletions src/Traits/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ protected function handleGrpcStatus($service, $method, $status)
if ($this->ydb->needDiscovery() && count($this->ydb->cluster()->all()) > 0){
$endpoint = $this->ydb->cluster()->all()[array_rand($this->ydb->cluster()->all())]->endpoint();
}
$this->client = new $this->client($endpoint,[
'credentials' => $this->ydb->iam()->getCredentials()
]);
$this->client = new $this->client($endpoint, $this->ydb->grpcOpts());
if (isset(self::$grpcExceptions[$status->code])) {
throw new self::$grpcExceptions[$status->code]($message);
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/Ydb.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Ydb
*/
protected $iam_config;

/**
* @var array
*/
protected $grpc_config;

/**
* @var Iam
*/
Expand Down Expand Up @@ -106,6 +111,7 @@ public function __construct($config = [], LoggerInterface $logger = null)
$this->endpoint = $config['endpoint'] ?? null;
$this->database = $config['database'] ?? null;
$this->iam_config = $config['iam_config'] ?? [];
$this->grpc_config = (array) ($config['grpc'] ?? []);

if (!is_null($logger) && isset($config['logger'])){
throw new \Exception('Logger set in 2 places');
Expand Down Expand Up @@ -173,6 +179,14 @@ public function meta(): array
return $meta;
}

public function grpcOpts(): array
{
$grpcOpts = (array) ($this->grpc_config['opts'] ?? []);
$grpcOpts['credentials'] = $this->iam()->getCredentials();

return $grpcOpts;
}

/**
* Discover available endpoints to connect to.
*
Expand Down

0 comments on commit 78b5db4

Please sign in to comment.