From 35c6369daa258c47c5ba5b2219774b1ab4daf27d Mon Sep 17 00:00:00 2001 From: Hannah Shi Date: Fri, 1 Nov 2019 21:10:11 +0000 Subject: [PATCH] init ssl roots only once --- src/php/ext/grpc/channel_credentials.c | 16 ++++++++++++++++ src/php/lib/Grpc/BaseStub.php | 10 ++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/php/ext/grpc/channel_credentials.c b/src/php/ext/grpc/channel_credentials.c index af1372878df..add896ac517 100644 --- a/src/php/ext/grpc/channel_credentials.c +++ b/src/php/ext/grpc/channel_credentials.c @@ -100,6 +100,17 @@ PHP_METHOD(ChannelCredentials, setDefaultRootsPem) { memcpy(default_pem_root_certs, pem_roots, pem_roots_length + 1); } +/** + * if default roots pem is set + * @return TRUE/FALSE + */ +PHP_METHOD(ChannelCredentials, isDefaultRootsPemSet) { + if (default_pem_root_certs) { + RETURN_TRUE; + } + RETURN_FALSE; +} + /** * Create a default channel credentials object. * @return ChannelCredentials The new default channel credentials object @@ -214,6 +225,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_setDefaultRootsPem, 0, 0, 1) ZEND_ARG_INFO(0, pem_roots) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_isDefaultRootsPemSet, 0, 0, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_createDefault, 0, 0, 0) ZEND_END_ARG_INFO() @@ -234,6 +248,8 @@ ZEND_END_ARG_INFO() static zend_function_entry channel_credentials_methods[] = { PHP_ME(ChannelCredentials, setDefaultRootsPem, arginfo_setDefaultRootsPem, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) + PHP_ME(ChannelCredentials, isDefaultRootsPemSet, arginfo_isDefaultRootsPemSet, + ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(ChannelCredentials, createDefault, arginfo_createDefault, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(ChannelCredentials, createSsl, arginfo_createSsl, diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index f33dbd4a80c..a154f6b41f0 100644 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -43,10 +43,12 @@ class BaseStub */ public function __construct($hostname, $opts, $channel = null) { - $ssl_roots = file_get_contents( - dirname(__FILE__).'/../../../../etc/roots.pem' - ); - ChannelCredentials::setDefaultRootsPem($ssl_roots); + if (!ChannelCredentials::isDefaultRootsPemSet()) { + $ssl_roots = file_get_contents( + dirname(__FILE__).'/../../../../etc/roots.pem' + ); + ChannelCredentials::setDefaultRootsPem($ssl_roots); + } $this->hostname = $hostname; $this->update_metadata = null;