kreait / firebase-php

Unofficial Firebase Admin SDK for PHP
https://firebase-php.readthedocs.io/
MIT License
2.26k stars 429 forks source link

firebase realtime database connect timeout #952

Closed honguri76 closed 6 hours ago

honguri76 commented 12 hours ago

Describe the bug

Hi. We use Firebase Admin SDK for PHP to use firebase realtime database.

$database->getReference() // this is the root reference ->update($updates);

When updating in the above manner, realtime database connection sometimes fails. Is there a way to find the cause of the connection failure?

I think the connection attempt times out after 15 seconds. Even if there is no timeout, the update attempt may take about 10 seconds.

Is there a way to change the timeout time?

Thank you.

Installed packages

beste/clock                      2.1.0    A collection of Clock implementations
beste/json                       1.0.0    A simple JSON helper to decode and encode JSON
fig/http-message-util            1.1.5    Utility classes and constants for use with PSR-7 (psr/http-message)
firebase/php-jwt                 6.10.0   A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.
google/apiclient                 2.16.0   Client library for Google APIs
google/apiclient-services        0.355.0  Client library for Google APIs
google/auth                      1.37.1   Google Auth Library for PHP
google/cloud-core                1.53.1   Google Cloud PHP shared dependency, providing functionality useful to all components.
google/cloud-firestore           0.1.0    Cloud Firestore client for PHP
google/cloud-storage             1.30.3   Cloud Storage Client for PHP
google/crc32                     0.2.0    Various CRC32 implementations
google/gax                       0.25.0   Google API Extensions for PHP
google/proto-client              0.25.0   Generated proto and gRPC classes for Google Cloud Platform in PHP
google/protobuf                  3.25.5   proto library for PHP
grpc/grpc                        1.57.0   gRPC library for PHP
guzzlehttp/guzzle                7.9.2    Guzzle is a PHP HTTP client library
guzzlehttp/promises              2.0.3    Guzzle promises library
guzzlehttp/psr7                  2.7.0    PSR-7 message implementation that also provides common utility methods
kreait/firebase-php              6.9.6    Firebase Admin SDK
kreait/firebase-tokens           3.0.3    A library to work with Firebase tokens
lcobucci/clock                   2.0.0    Yet another clock abstraction
lcobucci/jwt                     4.3.0    A simple library to work with JSON Web Token and JSON Web Signature
monolog/monolog                  2.9.3    Sends your logs to files, sockets, inboxes, databases and various web services
mtdowling/jmespath.php           2.8.0    Declaratively specify how to extract elements from a JSON document
paragonie/constant_time_encoding 2.7.0    Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)
paragonie/random_compat          9.99.100 PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
phpseclib/phpseclib              3.0.42   PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.
psr/cache                        1.0.1    Common interface for caching libraries
psr/clock                        1.0.0    Common interface for reading the clock.
psr/http-client                  1.0.3    Common interface for HTTP clients
psr/http-factory                 1.1.0    PSR-17: Common interfaces for PSR-7 HTTP message factories
psr/http-message                 2.0      Common interface for HTTP messages
psr/log                          1.1.4    Common interface for logging libraries
ralouphie/getallheaders          3.0.3    A polyfill for getallheaders.
ramsey/uuid                      3.9.7    Formerly rhumsaa/uuid. A PHP 5.4+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).
riverline/multipart-parser       2.1.2    One class library to parse multipart content with encoding and charset support.
rize/uri-template                0.3.8    PHP URI Template (RFC 6570) supports both expansion & extraction
stella-maris/clock               0.1.7    A pre-release of the proposed PSR-20 Clock-Interface
symfony/deprecation-contracts    2.5.3    A generic function and convention to trigger deprecation notices
symfony/polyfill-ctype           1.31.0   Symfony polyfill for ctype functions
symfony/polyfill-mbstring        1.31.0   Symfony polyfill for the Mbstring extension
symfony/polyfill-php80           1.31.0   Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/polyfill-php81           1.31.0   Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions

PHP version and extensions

composer                2.8.0     Composer package
composer-plugin-api     2.6.0     The Composer Plugin API
composer-runtime-api    2.2.2     The Composer Runtime API
ext-bz2                 7.4.3     The bz2 PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-calendar            7.4.3     The calendar PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-ctype               7.4.3     The ctype PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-curl                7.4.3     The curl PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-date                7.4.3     The date PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-dba                 7.4.3     The dba PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-dom                 20031129  The dom PHP extension
ext-exif                7.4.3     The exif PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-ffi                 7.4.3     The FFI PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-fileinfo            7.4.3     The fileinfo PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-filter              7.4.3     The filter PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-ftp                 7.4.3     The ftp PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-gd                  7.4.3     The gd PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-gettext             7.4.3     The gettext PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-gmp                 7.4.3     The gmp PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-hash                7.4.3     The hash PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-iconv               7.4.3     The iconv PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-imagick             3.4.4     The imagick PHP extension
ext-imap                7.4.3     The imap PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-json                7.4.3     The json PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-ldap                7.4.3     The ldap PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-libxml              7.4.3     The libxml PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-mbstring            7.4.3     The mbstring PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-mysqli              7.4.3     The mysqli PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-mysqlnd             0         The mysqlnd PHP extension (actual version: mysqlnd 7.4.3-4ubuntu2.24)
ext-odbc                7.4.3     The odbc PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-openssl             7.4.3     The openssl PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pcntl               7.4.3     The pcntl PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pcre                7.4.3     The pcre PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pdo                 7.4.3     The PDO PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pdo_mysql           7.4.3     The pdo_mysql PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pdo_odbc            7.4.3     The PDO_ODBC PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pdo_pgsql           7.4.3     The pdo_pgsql PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pdo_sqlite          7.4.3     The pdo_sqlite PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-pgsql               7.4.3     The pgsql PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-phar                7.4.3     The Phar PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-posix               7.4.3     The posix PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-readline            7.4.3     The readline PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-reflection          7.4.3     The Reflection PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-session             7.4.3     The session PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-shmop               7.4.3     The shmop PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-simplexml           7.4.3     The SimpleXML PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-snmp                7.4.3     The snmp PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-soap                7.4.3     The soap PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-sockets             7.4.3     The sockets PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-sodium              7.4.3     The sodium PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-spl                 7.4.3     The SPL PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-sqlite3             7.4.3     The sqlite3 PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-sysvmsg             7.4.3     The sysvmsg PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-sysvsem             7.4.3     The sysvsem PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-sysvshm             7.4.3     The sysvshm PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-tidy                7.4.3     The tidy PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-tokenizer           7.4.3     The tokenizer PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-xml                 7.4.3     The xml PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-xmlreader           7.4.3     The xmlreader PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-xmlrpc              7.4.3     The xmlrpc PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-xmlwriter           7.4.3     The xmlwriter PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-xsl                 7.4.3     The xsl PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-zend-opcache        7.4.3     The Zend OPcache PHP extension (actual version: 7.4.3-4ubuntu2.24)
ext-zip                 1.15.6    The zip PHP extension
ext-zlib                7.4.3     The zlib PHP extension (actual version: 7.4.3-4ubuntu2.24)
lib-bz2                 1.0.8     The bz2 library
lib-curl                7.68.0    The curl library
lib-curl-libssh         0.9.3     curl libssh version
lib-curl-openssl        1.1.1.6   curl OpenSSL version (1.1.1.6)
lib-curl-zlib           1.2.11    curl zlib version
lib-date-timelib        2018.03   date timelib version
lib-date-zoneinfo       0         zoneinfo ("Olson") database for date
lib-fileinfo-libmagic   537       fileinfo libmagic version
lib-gd                  2.2.5     The gd library
lib-gmp                 6.2.0     The gmp library
lib-iconv               2.31      The iconv library
lib-imagick-imagemagick 6.9.10.23 The imagick-imagemagick library
lib-ldap-openldap       2.4.49    OpenLDAP version of ldap
lib-libsodium           1.0.18    The libsodium library
lib-libxml              2.9.10    libxml library version
lib-libxslt             1.1.34    The libxslt library
lib-libxslt-libxml      2.9.10    libxml version libxslt is compiled against
lib-mbstring-libmbfl    1.3.2     mbstring libmbfl version
lib-mbstring-oniguruma  6.9.4     mbstring oniguruma version
lib-openssl             1.1.1.6   OpenSSL 1.1.1f  31 Mar 2020
lib-pcre                10.34     The pcre library
lib-pcre-unicode        12.1.0    PCRE Unicode version support
lib-pdo_sqlite-sqlite   3.31.1    The pdo_sqlite-sqlite library
lib-sqlite3-sqlite      3.31.1    The sqlite3-sqlite library
lib-zip-libzip          1.5.1     The zip-libzip library
lib-zlib                1.2.11    The zlib library
php                     7.4.3     The PHP interpreter
php-64bit               7.4.3     The PHP interpreter, 64bit
php-ipv6                7.4.3     The PHP interpreter, with IPv6 support

Steps to reproduce the issue.

# Insert the commands issued in the terminal if they are needed
# to reproduce the issue. Otherwise, delete this code block.
// Insert the PHP code to reproduce the issue. Please ensure that it is code that
// can be copy pasted to reproduce it.

require_once ('vendor/autoload.php'); use Kreait\Firebase\Factory; use Kreait\Firebase\Exception\FirebaseException; use Throwable;

$factory = (new Factory) ->withServiceAccount('../../.ssh/account.json') ->withDatabaseUri('https://databaseUri/');

$database = $factory->createDatabase();

$update_value = array(); foreach( $post_data['topic_list'] AS $aRow ) { $micro_time_array = explode(" ", microtime()); $micro_time_last_value = explode(".", $micro_time_array[0]); $update_value[$server_text.$aRow.'/data'] = $data.'/'.substr($micro_time_array[1],-4).'.'.substr($micro_time_last_value[1],0,6); }

$database->getReference() ->update($update_value);

Error message/Stack trace

-

Additional information

No response

jeromegamez commented 7 hours ago

You can customize the Guzzle HTTP Client options with https://firebase-php.readthedocs.io/en/7.15.0/setup.html#setting-guzzle-config-options

However, requests to the Firebase APIs, including the Realtime Database, should take a few milliseconds - if requests are slow and the connection even times out, that must be a networking issue in your environment. You could try enabling debug logs in the Guzzle Config to hopefully get some hints as why that is.

honguri76 commented 6 hours ago

Thank you for your reply. We checked the time using the code below.

$start = microtime(true); $update_value = array(); foreach( $post_data['topic_list'] AS $aRow ) { $micro_time_array = explode(" ", microtime()); $micro_time_last_value = explode(".", $micro_time_array[0]); $update_value[$server_text.$aRow.'/data'] = $data.'/'.substr($micro_time_array[1],6,9).'.'.substr($micro_time_last_value[1],0,6); } ​ $database->getReference() ->update($update_value);

$end = microtime(true); $time = sprintf('%0.5f', $end - $start);

echo $time;

The time it takes for an update to complete is 500 milliseconds on average, but sometimes it takes 5000 milliseconds. In this case, does it mean that it is most likely a network problem in our system environment?

jeromegamez commented 6 hours ago

I think so, yes. If it's fast most of the time, but slow sometimes, there's nothing I can do in the code of the SDK to avoid that. That's why I can't "accept" this as a bug of the SDK.

Another possibility is that the realtime database API sometimes responds slowly (the request is fast, but the response is slow) - in that case this is a problem that might solve itself with time, when Firebase addresses the issue. That's where enabling debug logging via the Guzzle Client Config can help finding that out. If it's not your network connection, you should raise an issue with Firebase support directly (this SDK is not an official Firebase SDK)