Skip to content

Commit 4cf9536

Browse files
committed
PHPC-164: Throw MongoDB\Driver\ConnectionTimeoutException
1 parent 4acca26 commit 4cf9536

File tree

5 files changed

+119
-3
lines changed

5 files changed

+119
-3
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ if test "$PHONGO" != "no"; then
195195
src/MongoDB/SSLConnectionException.c \
196196
src/MongoDB/DuplicateKeyException.c \
197197
src/MongoDB/ExecutionTimeoutException.c \
198+
src/MongoDB/ConnectionTimeoutException.c \
198199
src/MongoDB/WriteException.c \
199200
";
200201

php_phongo.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_d
9393
switch(code) {
9494
case 50: /* ExceededTimeLimit */
9595
return php_phongo_executiontimeoutexception_ce;
96+
case MONGOC_ERROR_STREAM_SOCKET:
97+
return php_phongo_connectiontimeoutexception_ce;
9698
case 11000: /* DuplicateKey */
9799
return php_phongo_duplicatekeyexception_ce;
98100
case MONGOC_ERROR_CLIENT_AUTHENTICATE:
@@ -101,7 +103,6 @@ zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_d
101103
case MONGOC_ERROR_STREAM_INVALID_TYPE:
102104
case MONGOC_ERROR_STREAM_INVALID_STATE:
103105
case MONGOC_ERROR_STREAM_NAME_RESOLUTION:
104-
case MONGOC_ERROR_STREAM_SOCKET:
105106
case MONGOC_ERROR_STREAM_CONNECT:
106107
case MONGOC_ERROR_STREAM_NOT_ESTABLISHED:
107108
return php_phongo_connectionexception_ce;
@@ -747,8 +748,6 @@ ssize_t phongo_stream_writev(mongoc_stream_t *stream, mongoc_iovec_t *iov, size_
747748
php_phongo_stream_socket *base_stream = (php_phongo_stream_socket *)stream;
748749
TSRMLS_FETCH_FROM_CTX(base_stream->tsrm_ls);
749750

750-
php_phongo_set_timeout(base_stream, timeout_msec);
751-
752751
for (i = 0; i < iovcnt; i++) {
753752
sent += php_stream_write(base_stream->stream, iov[i].iov_base, iov[i].iov_len);
754753
}
@@ -767,6 +766,8 @@ ssize_t phongo_stream_readv(mongoc_stream_t *stream, mongoc_iovec_t *iov, size_t
767766
size_t cur = 0;
768767
TSRMLS_FETCH_FROM_CTX(base_stream->tsrm_ls);
769768

769+
php_phongo_set_timeout(base_stream, timeout_msec);
770+
770771
do {
771772
read = php_stream_read(base_stream->stream, iov[cur].iov_base, iov[cur].iov_len);
772773
mongoc_log(MONGOC_LOG_LEVEL_DEBUG, MONGOC_LOG_DOMAIN, "Reading got: %ld wanted: %ld", read, min_bytes);
@@ -1622,6 +1623,7 @@ PHP_MINIT_FUNCTION(phongo)
16221623
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
16231624
PHP_MINIT(DuplicateKeyException)(INIT_FUNC_ARGS_PASSTHRU);
16241625
PHP_MINIT(ExecutionTimeoutException)(INIT_FUNC_ARGS_PASSTHRU);
1626+
PHP_MINIT(ConnectionTimeoutException)(INIT_FUNC_ARGS_PASSTHRU);
16251627

16261628
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);
16271629
PHP_MINIT(Serializable)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ extern PHONGO_API zend_class_entry *php_phongo_authenticationexception_ce;
207207
extern PHONGO_API zend_class_entry *php_phongo_sslconnectionexception_ce;
208208
extern PHONGO_API zend_class_entry *php_phongo_duplicatekeyexception_ce;
209209
extern PHONGO_API zend_class_entry *php_phongo_executiontimeoutexception_ce;
210+
extern PHONGO_API zend_class_entry *php_phongo_connectiontimeoutexception_ce;
210211
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
211212

212213
extern PHONGO_API zend_class_entry *php_phongo_type_ce;
@@ -247,6 +248,7 @@ PHP_MINIT_FUNCTION(AuthenticationException);
247248
PHP_MINIT_FUNCTION(SSLConnectionException);
248249
PHP_MINIT_FUNCTION(DuplicateKeyException);
249250
PHP_MINIT_FUNCTION(ExecutionTimeoutException);
251+
PHP_MINIT_FUNCTION(ConnectionTimeoutException);
250252
PHP_MINIT_FUNCTION(WriteException);
251253

252254
PHP_MINIT_FUNCTION(Type);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
+---------------------------------------------------------------------------+
3+
| PHP Driver for MongoDB |
4+
+---------------------------------------------------------------------------+
5+
| Copyright 2013-2014 MongoDB, Inc. |
6+
| |
7+
| Licensed under the Apache License, Version 2.0 (the "License"); |
8+
| you may not use this file except in compliance with the License. |
9+
| You may obtain a copy of the License at |
10+
| |
11+
| http://www.apache.org/licenses/LICENSE-2.0 |
12+
| |
13+
| Unless required by applicable law or agreed to in writing, software |
14+
| distributed under the License is distributed on an "AS IS" BASIS, |
15+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16+
| See the License for the specific language governing permissions and |
17+
| limitations under the License. |
18+
+---------------------------------------------------------------------------+
19+
| Copyright (c) 2014, MongoDB, Inc. |
20+
+---------------------------------------------------------------------------+
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
# include "config.h"
25+
#endif
26+
27+
/* External libs */
28+
#include <bson.h>
29+
#include <mongoc.h>
30+
31+
/* PHP Core stuff */
32+
#include <php.h>
33+
#include <php_ini.h>
34+
#include <ext/standard/info.h>
35+
#include <Zend/zend_interfaces.h>
36+
#include <ext/spl/spl_iterators.h>
37+
/* Our Compatability header */
38+
#include "php_compat_53.h"
39+
40+
/* Our stuffz */
41+
#include "php_phongo.h"
42+
#include "php_bson.h"
43+
#include <ext/spl/spl_exceptions.h>
44+
45+
46+
PHONGO_API zend_class_entry *php_phongo_connectiontimeoutexception_ce;
47+
48+
/* {{{ MongoDB\Driver\ConnectionTimeoutException */
49+
50+
static zend_function_entry php_phongo_connectiontimeoutexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(ConnectionTimeoutException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "ConnectionTimeoutException", php_phongo_connectiontimeoutexception_me);
65+
php_phongo_connectiontimeoutexception_ce = zend_register_internal_class_ex(&ce, php_phongo_connectionexception_ce, NULL TSRMLS_CC);
66+
php_phongo_connectiontimeoutexception_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
67+
68+
return SUCCESS;
69+
}
70+
/* }}} */
71+
72+
73+
74+
/*
75+
* Local variables:
76+
* tab-width: 4
77+
* c-basic-offset: 4
78+
* End:
79+
* vim600: noet sw=4 ts=4 fdm=marker
80+
* vim<600: noet sw=4 ts=4
81+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
ConnectionTimeoutException: exceeding sockettimeoutms
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI . "/?sockettimeoutms=1004");
10+
11+
$cmd = array(
12+
"sleep" => 1,
13+
"w" => false,
14+
"secs" => 2,
15+
);
16+
$command = new MongoDB\Driver\Command($cmd);
17+
18+
throws(function() use ($manager, $command) {
19+
$result = $manager->executeCommand("admin", $command);
20+
}, "MongoDB\Driver\ConnectionTimeoutException");
21+
22+
/* Sleep one second longer then needing to make sure later processes don't get affected */
23+
sleep(3);
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECT--
29+
OK: Got MongoDB\Driver\ConnectionTimeoutException
30+
===DONE===

0 commit comments

Comments
 (0)