From 2886e4349e81f7437fe22fc069b7ba7b31965287 Mon Sep 17 00:00:00 2001 From: Alex Mironov Date: Thu, 11 Apr 2024 21:28:31 +0300 Subject: [PATCH 1/4] gRPC client options setting feature --- README.md | 16 ++++++++++++++++ src/AuthService.php | 4 +--- src/Discovery.php | 4 +--- src/Operations.php | 4 +--- src/Scheme.php | 4 +--- src/Scripting.php | 4 +--- src/Table.php | 4 +--- src/Traits/RequestTrait.php | 4 +--- src/Ydb.php | 14 ++++++++++++++ 9 files changed, 37 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 628c5e8d..f901a6cc 100644 --- a/README.md +++ b/README.md @@ -562,3 +562,19 @@ $config = [ $ydb = new \YdbPlatform\Ydb\Ydb($config); ``` +## gRPC options + +You can customize the gRPC client's behavior by setting options in config array + +Example of using: +```php +$config = [ + // ... + '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); +``` diff --git a/src/AuthService.php b/src/AuthService.php index bccfbf11..42ca09da 100644 --- a/src/AuthService.php +++ b/src/AuthService.php @@ -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->opts()); $this->credentials = $ydb->iam(); $this->meta = [ 'x-ydb-database' => [$ydb->database()], diff --git a/src/Discovery.php b/src/Discovery.php index be10fd27..27916e1b 100644 --- a/src/Discovery.php +++ b/src/Discovery.php @@ -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->opts()); $this->meta = $ydb->meta(); diff --git a/src/Operations.php b/src/Operations.php index a738bdad..5d39694c 100644 --- a/src/Operations.php +++ b/src/Operations.php @@ -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->opts()); $this->meta = $ydb->meta(); diff --git a/src/Scheme.php b/src/Scheme.php index 967cdc76..d9bd35fa 100644 --- a/src/Scheme.php +++ b/src/Scheme.php @@ -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->opts()); $this->meta = $ydb->meta(); diff --git a/src/Scripting.php b/src/Scripting.php index b9a8dd06..24da2ff4 100644 --- a/src/Scripting.php +++ b/src/Scripting.php @@ -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->opts()); $this->meta = $ydb->meta(); diff --git a/src/Table.php b/src/Table.php index 6a3072b0..a29acb05 100644 --- a/src/Table.php +++ b/src/Table.php @@ -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->opts()); $this->meta = $ydb->meta(); diff --git a/src/Traits/RequestTrait.php b/src/Traits/RequestTrait.php index e71b190d..f34006b0 100644 --- a/src/Traits/RequestTrait.php +++ b/src/Traits/RequestTrait.php @@ -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->opts()); if (isset(self::$grpcExceptions[$status->code])) { throw new self::$grpcExceptions[$status->code]($message); } else { diff --git a/src/Ydb.php b/src/Ydb.php index 1b6f3a33..b7909be5 100644 --- a/src/Ydb.php +++ b/src/Ydb.php @@ -37,6 +37,11 @@ class Ydb */ protected $iam_config; + /** + * @var array + */ + protected $opts; + /** * @var Iam */ @@ -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->opts = $config['opts'] ?? []; if (!is_null($logger) && isset($config['logger'])){ throw new \Exception('Logger set in 2 places'); @@ -173,6 +179,14 @@ public function meta(): array return $meta; } + public function opts(): array + { + $opts = $this->opts; + $opts['credentials'] = $this->iam()->getCredentials(); + + return $opts; + } + /** * Discover available endpoints to connect to. * From 37d38623970675a1e82174b8a101a83256cd5aa0 Mon Sep 17 00:00:00 2001 From: Alex Mironov Date: Thu, 11 Apr 2024 21:55:21 +0300 Subject: [PATCH 2/4] updated CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf52f9b..07a8abdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* added `$opts` array for customize gRPC client options + ## 1.14.0 * added `ScanQueryMode` for `Table::scanQuery` From f53126b3d975b089864dce83533221be5f3c68db Mon Sep 17 00:00:00 2001 From: Alex Mironov Date: Fri, 12 Apr 2024 15:45:15 +0300 Subject: [PATCH 3/4] Separated grpc options to own section: `grpc` --- README.md | 15 +++++++++------ src/AuthService.php | 2 +- src/Discovery.php | 2 +- src/Operations.php | 2 +- src/Scheme.php | 2 +- src/Scripting.php | 2 +- src/Table.php | 2 +- src/Traits/RequestTrait.php | 2 +- src/Ydb.php | 12 ++++++------ 9 files changed, 22 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f901a6cc..c35d973b 100644 --- a/README.md +++ b/README.md @@ -562,19 +562,22 @@ $config = [ $ydb = new \YdbPlatform\Ydb\Ydb($config); ``` -## gRPC options +## gRPC +### gRPC client's options You can customize the gRPC client's behavior by setting options in config array Example of using: ```php $config = [ // ... - 'opts' => [ - 'grpc.max_receive_message_length' => 8*1024*1024, - 'grpc.default_compression_algorithm' => 2, - 'grpc.default_compression_level' => 2, + '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); ``` diff --git a/src/AuthService.php b/src/AuthService.php index 42ca09da..40964713 100644 --- a/src/AuthService.php +++ b/src/AuthService.php @@ -28,7 +28,7 @@ public function __construct(Ydb $ydb, $logger) { $this->ydb = $ydb; $this->logger = $logger; - $this->client = new ServiceClient($ydb->endpoint(), $ydb->opts()); + $this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts()); $this->credentials = $ydb->iam(); $this->meta = [ 'x-ydb-database' => [$ydb->database()], diff --git a/src/Discovery.php b/src/Discovery.php index 27916e1b..34c6cee3 100644 --- a/src/Discovery.php +++ b/src/Discovery.php @@ -43,7 +43,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null) { $this->ydb = $ydb; - $this->client = new ServiceClient($ydb->endpoint(), $ydb->opts()); + $this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts()); $this->meta = $ydb->meta(); diff --git a/src/Operations.php b/src/Operations.php index 5d39694c..dcd243bc 100644 --- a/src/Operations.php +++ b/src/Operations.php @@ -38,7 +38,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null) { $this->ydb = $ydb; - $this->client = new ServiceClient($ydb->endpoint(), $ydb->opts()); + $this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts()); $this->meta = $ydb->meta(); diff --git a/src/Scheme.php b/src/Scheme.php index d9bd35fa..df0f2a78 100644 --- a/src/Scheme.php +++ b/src/Scheme.php @@ -41,7 +41,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null) { $this->ydb = $ydb; - $this->client = new ServiceClient($ydb->endpoint(), $ydb->opts()); + $this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts()); $this->meta = $ydb->meta(); diff --git a/src/Scripting.php b/src/Scripting.php index 24da2ff4..850b4fcc 100644 --- a/src/Scripting.php +++ b/src/Scripting.php @@ -38,7 +38,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null) { $this->ydb = $ydb; - $this->client = new ServiceClient($ydb->endpoint(), $ydb->opts()); + $this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts()); $this->meta = $ydb->meta(); diff --git a/src/Table.php b/src/Table.php index a29acb05..adde9bda 100644 --- a/src/Table.php +++ b/src/Table.php @@ -75,7 +75,7 @@ public function __construct(Ydb $ydb, LoggerInterface $logger = null, Retry &$re { $this->ydb = $ydb; - $this->client = new ServiceClient($ydb->endpoint(), $ydb->opts()); + $this->client = new ServiceClient($ydb->endpoint(), $ydb->grpcOpts()); $this->meta = $ydb->meta(); diff --git a/src/Traits/RequestTrait.php b/src/Traits/RequestTrait.php index f34006b0..312dd487 100644 --- a/src/Traits/RequestTrait.php +++ b/src/Traits/RequestTrait.php @@ -193,7 +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, $this->ydb->opts()); + $this->client = new $this->client($endpoint, $this->ydb->grpcOpts()); if (isset(self::$grpcExceptions[$status->code])) { throw new self::$grpcExceptions[$status->code]($message); } else { diff --git a/src/Ydb.php b/src/Ydb.php index b7909be5..7de8b98a 100644 --- a/src/Ydb.php +++ b/src/Ydb.php @@ -40,7 +40,7 @@ class Ydb /** * @var array */ - protected $opts; + protected $grpc_config; /** * @var Iam @@ -111,7 +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->opts = $config['opts'] ?? []; + $this->grpc_config = (array) ($config['grpc'] ?? []); if (!is_null($logger) && isset($config['logger'])){ throw new \Exception('Logger set in 2 places'); @@ -179,12 +179,12 @@ public function meta(): array return $meta; } - public function opts(): array + public function grpcOpts(): array { - $opts = $this->opts; - $opts['credentials'] = $this->iam()->getCredentials(); + $grpcOpts = (array) ($this->grpc_config['opts'] ?? []); + $grpcOpts['credentials'] = $this->iam()->getCredentials(); - return $opts; + return $grpcOpts; } /** From 499e8b997959cc0c443b6b0dcaede84dd96657dc Mon Sep 17 00:00:00 2001 From: Alex Mironov Date: Fri, 12 Apr 2024 15:57:01 +0300 Subject: [PATCH 4/4] updated CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07a8abdc..c8bb4569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -* added `$opts` array for customize gRPC client options +* added `$grpc_config` array for customize gRPC behavior ## 1.14.0 * added `ScanQueryMode` for `Table::scanQuery`