Skip to content

Commit 1db9b17

Browse files
committed
Fix big responses handling
closes gh-74 closes gh-82
1 parent cd9f59f commit 1db9b17

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

src/tarantool.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ tarantool_stream_send(tarantool_object *obj TSRMLS_DC) {
121121
* See https://bugs.launchpad.net/tarantool/+bug/1182474
122122
*/
123123
static size_t
124-
tarantool_stream_read(tarantool_object *obj, char *buf, size_t size) {
125-
return tntll_stream_read(obj->stream, buf, size);
124+
tarantool_stream_read(tarantool_connection *obj, char *buf, size_t size) {
125+
return tntll_stream_read2(obj->stream, buf, size);
126126
}
127127

128128
static void
@@ -172,8 +172,8 @@ int __tarantool_connect(tarantool_object *obj, zval *id TSRMLS_DC) {
172172
&obj->stream, &err) == -1) {
173173
continue;
174174
}
175-
if (tntll_stream_read(obj->stream, obj->greeting,
176-
GREETING_SIZE) == -1) {
175+
if (tntll_stream_read2(obj->stream, obj->greeting,
176+
GREETING_SIZE) == -1) {
177177
continue;
178178
}
179179
obj->salt = obj->greeting + SALT_PREFIX_SIZE;
@@ -257,7 +257,7 @@ static int64_t tarantool_step_recv(
257257
*header = NULL;
258258
*body = NULL;
259259
if (tarantool_stream_read(obj, pack_len, 5) != 5) {
260-
THROW_EXC("Can't read query from server");
260+
THROW_EXC("Can't read query from server (failed to read length)");
261261
goto error_con;
262262
}
263263
if (php_mp_check(pack_len, 5)) {
@@ -268,14 +268,15 @@ static int64_t tarantool_step_recv(
268268
smart_string_ensure(obj->value, body_size);
269269
if (tarantool_stream_read(obj, SSTR_POS(obj->value),
270270
body_size) != body_size) {
271-
THROW_EXC("Can't read query from server");
271+
THROW_EXC("Can't read query from server (failed to read %d "
272+
"bytes from server [header + body])", body_size);
272273
goto error;
273274
}
274275
SSTR_LEN(obj->value) += body_size;
275276

276277
char *pos = SSTR_BEG(obj->value);
277278
if (php_mp_check(pos, body_size)) {
278-
THROW_EXC("Failed verifying msgpack");
279+
THROW_EXC("Failed verifying header [bad msgpack]");
279280
goto error;
280281
}
281282
if (php_mp_unpack(header, &pos) == FAILURE ||
@@ -284,7 +285,7 @@ static int64_t tarantool_step_recv(
284285
goto error;
285286
}
286287
if (php_mp_check(pos, body_size)) {
287-
THROW_EXC("Failed verifying msgpack");
288+
THROW_EXC("Failed verifying body [bad msgpack]");
288289
goto error_con;
289290
}
290291
if (php_mp_unpack(body, &pos) == FAILURE) {
@@ -591,14 +592,15 @@ int get_spaceno_by_name(tarantool_object *obj, zval *id, zval *name TSRMLS_DC) {
591592

592593
char pack_len[5] = {0, 0, 0, 0, 0};
593594
if (tarantool_stream_read(obj, pack_len, 5) != 5) {
594-
THROW_EXC("Can't read query from server");
595+
THROW_EXC("Can't read query from server (failed to read length)");
595596
return FAILURE;
596597
}
597598
size_t body_size = php_mp_unpack_package_size(pack_len);
598599
smart_string_ensure(obj->value, body_size);
599600
if (tarantool_stream_read(obj, obj->value->c,
600601
body_size) != body_size) {
601-
THROW_EXC("Can't read query from server");
602+
THROW_EXC("Can't read query from server (failed to read %d "
603+
"bytes from server [header + body])", body_size);
602604
return FAILURE;
603605
}
604606

@@ -651,14 +653,15 @@ int get_indexno_by_name(tarantool_object *obj, zval *id,
651653

652654
char pack_len[5] = {0, 0, 0, 0, 0};
653655
if (tarantool_stream_read(obj, pack_len, 5) != 5) {
654-
THROW_EXC("Can't read query from server");
656+
THROW_EXC("Can't read query from server (failed to read length)");
655657
return FAILURE;
656658
}
657659
size_t body_size = php_mp_unpack_package_size(pack_len);
658660
smart_string_ensure(obj->value, body_size);
659661
if (tarantool_stream_read(obj, obj->value->c,
660662
body_size) != body_size) {
661-
THROW_EXC("Can't read query from server");
663+
THROW_EXC("Can't read query from server (failed to read %d "
664+
"bytes from server [header + body])", body_size);
662665
return FAILURE;
663666
}
664667

src/tarantool_network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ int tntll_stream_read2(php_stream *stream, char *buf, size_t size) {
119119
size - total_size);
120120
assert(read_size + total_size <= size);
121121
if (read_size <= 0)
122-
break;
122+
break;
123123
total_size += read_size;
124124
}
125125
return total_size;

test/RandomTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
function generateRandomString($length = 10) {
4+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
5+
$charactersLength = strlen($characters);
6+
$randomString = '';
7+
for ($i = 0; $i < $length; $i++) {
8+
$randomString .= $characters[rand(0, $charactersLength - 1)];
9+
}
10+
return $randomString;
11+
}
12+
13+
class RandomTest extends PHPUnit_Framework_TestCase
14+
{
15+
protected static $tarantool;
16+
17+
public static function setUpBeforeClass() {
18+
self::$tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'));
19+
self::$tarantool->authenticate('test', 'test');
20+
self::$tarantool->ping();
21+
}
22+
23+
public function test_01_random_big_response() {
24+
for ($i = 5000; $i < 100000; $i += 1000) {
25+
$request = "do return '" . generateRandomString($i) . "' end";
26+
self::$tarantool->evaluate($request);
27+
}
28+
$this->assertTrue(True);
29+
}
30+
public function test_02_very_big_response() {
31+
$request = "do return '" . str_repeat('x', 1024 * 1024) . "' end";
32+
self::$tarantool->evaluate($request);
33+
$this->assertTrue(True);
34+
}
35+
}

test/shared/phpunit_bas.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<file>test/CreateTest.php</file>
1010
<file>test/MsgPackTest.php</file>
1111
<file>test/AssertTest.php</file>
12+
<file>test/RandomTest.php</file>
1213
</testsuite>
1314
</testsuites>
1415

test/shared/phpunit_tap.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<file>test/CreateTest.php</file>
1111
<file>test/MsgPackTest.php</file>
1212
<file>test/AssertTest.php</file>
13+
<file>test/RandomTest.php</file>
1314
</testsuite>
1415
</testsuites>
1516

0 commit comments

Comments
 (0)