Skip to content

Commit 390ef25

Browse files
committed
PHON-100: Implement connectiontimeoutms
1 parent 0bfb534 commit 390ef25

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

php_phongo.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
810810
php_phongo_stream_socket *base_stream = NULL;
811811
php_stream *stream = NULL;
812812
const bson_t *options;
813+
bson_iter_t iter;
814+
struct timeval *timeoutp = NULL;
813815
char *errmsg = NULL;
814816
int errcode;
815817
char *dsn;
@@ -836,8 +838,23 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
836838

837839
options = mongoc_uri_get_options(uri);
838840

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+
841858
if (!stream) {
842859
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Failed connecting to '%s:%d': %s", host->host, host->port, errmsg);
843860
return NULL;

0 commit comments

Comments
 (0)