@@ -810,6 +810,8 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
810
810
php_phongo_stream_socket * base_stream = NULL ;
811
811
php_stream * stream = NULL ;
812
812
const bson_t * options ;
813
+ bson_iter_t iter ;
814
+ struct timeval * timeoutp = NULL ;
813
815
char * errmsg = NULL ;
814
816
int errcode ;
815
817
char * dsn ;
@@ -836,8 +838,23 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
836
838
837
839
options = mongoc_uri_get_options (uri );
838
840
839
- mongoc_log (MONGOC_LOG_LEVEL_DEBUG , MONGOC_LOG_DOMAIN , "Connecting to '%s'" , dsn );
840
- stream = php_stream_xport_create (dsn , dsn_len , 0 , STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT , (char * )"persistent id" , /*options->connectTimeoutMS*/ 0 , (php_stream_context * )NULL , & errmsg , & errcode );
841
+ if (bson_iter_init_find (& iter , options , "connecttimeoutms" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
842
+ struct timeval timeout = {0 , 0 };
843
+ int32_t connecttimeoutms = MONGOC_DEFAULT_CONNECTTIMEOUTMS ;
844
+
845
+ if (!(connecttimeoutms = bson_iter_int32 (& iter ))) {
846
+ connecttimeoutms = MONGOC_DEFAULT_CONNECTTIMEOUTMS ;
847
+ }
848
+
849
+ timeout .tv_sec = connecttimeoutms / 1000 ;
850
+ timeout .tv_usec = (connecttimeoutms % 1000 ) * 1000 ;
851
+
852
+ mongoc_log (MONGOC_LOG_LEVEL_DEBUG , MONGOC_LOG_DOMAIN , "Connecting to '%s' (%ld.%06ldms timeout)" , dsn , timeout .tv_sec , timeout .tv_usec );
853
+ timeoutp = & timeout ;
854
+ }
855
+
856
+ stream = php_stream_xport_create (dsn , dsn_len , 0 , STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT , (char * )"persistent id" , timeoutp , (php_stream_context * )user_data , & errmsg , & errcode );
857
+
841
858
if (!stream ) {
842
859
bson_set_error (error , MONGOC_ERROR_STREAM , MONGOC_ERROR_STREAM_CONNECT , "Failed connecting to '%s:%d': %s" , host -> host , host -> port , errmsg );
843
860
return NULL ;
0 commit comments