Skip to content

Commit 1284eba

Browse files
authored
Merge pull request #91 from clue-labs/buffers
Use constructor parameters instead of public buffer property
2 parents e65c2b1 + d69ad7b commit 1284eba

8 files changed

Lines changed: 67 additions & 73 deletions

README.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -753,21 +753,22 @@ take care of the underlying stream resource.
753753
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
754754
stream resource manually.
755755

756-
The `$bufferSize` property controls the maximum buffer size in bytes to read
757-
at once from the stream.
756+
This class takes an optional `int|null $readChunkSize` parameter that controls
757+
the maximum buffer size in bytes to read at once from the stream.
758+
You can use a `null` value here in order to apply its default value.
758759
This value SHOULD NOT be changed unless you know what you're doing.
759760
This can be a positive number which means that up to X bytes will be read
760761
at once from the underlying stream resource. Note that the actual number
761762
of bytes read may be lower if the stream resource has less than X bytes
762763
currently available.
763-
This can be `null` which means "read everything available" from the
764+
This can be `-1` which means "read everything available" from the
764765
underlying stream resource.
765766
This should read until the stream resource is not readable anymore
766767
(i.e. underlying buffer drained), note that this does not neccessarily
767768
mean it reached EOF.
768769

769770
```php
770-
$stream->bufferSize = 8192;
771+
$stream = new ReadableResourceStream(STDIN, $loop, 8192);
771772
```
772773

773774
### WritableResourceStream
@@ -819,11 +820,14 @@ ready to accept data.
819820
For this, it uses an in-memory buffer string to collect all outstanding writes.
820821
This buffer has a soft-limit applied which defines how much data it is willing
821822
to accept before the caller SHOULD stop sending further data.
822-
It currently defaults to 64 KiB and can be controlled through the public
823-
`$softLimit` property like this:
823+
824+
This class takes an optional `int|null $writeBufferSoftLimit` parameter that controls
825+
this maximum buffer size in bytes.
826+
You can use a `null` value here in order to apply its default value.
827+
This value SHOULD NOT be changed unless you know what you're doing.
824828

825829
```php
826-
$stream->softLimit = 8192;
830+
$stream = new WritableResourceStream(STDOUT, $loop, 8192);
827831
```
828832

829833
See also [`write()`](#write) for more details.
@@ -873,21 +877,23 @@ take care of the underlying stream resource.
873877
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
874878
stream resource manually.
875879

876-
The `$bufferSize` property controls the maximum buffer size in bytes to read
877-
at once from the stream.
880+
This class takes an optional `int|null $readChunkSize` parameter that controls
881+
the maximum buffer size in bytes to read at once from the stream.
882+
You can use a `null` value here in order to apply its default value.
878883
This value SHOULD NOT be changed unless you know what you're doing.
879884
This can be a positive number which means that up to X bytes will be read
880885
at once from the underlying stream resource. Note that the actual number
881886
of bytes read may be lower if the stream resource has less than X bytes
882887
currently available.
883-
This can be `null` which means "read everything available" from the
888+
This can be `-1` which means "read everything available" from the
884889
underlying stream resource.
885890
This should read until the stream resource is not readable anymore
886891
(i.e. underlying buffer drained), note that this does not neccessarily
887892
mean it reached EOF.
888893

889894
```php
890-
$stream->bufferSize = 8192;
895+
$conn = stream_socket_client('tcp://google.com:80');
896+
$stream = new DuplexResourceStream($conn, $loop, 8192);
891897
```
892898

893899
Any `write()` calls to this class will not be performaned instantly, but will
@@ -896,15 +902,22 @@ ready to accept data.
896902
For this, it uses an in-memory buffer string to collect all outstanding writes.
897903
This buffer has a soft-limit applied which defines how much data it is willing
898904
to accept before the caller SHOULD stop sending further data.
899-
It currently defaults to 64 KiB and can be controlled through the public
900-
`$softLimit` property like this:
905+
906+
This class takes another optional `WritableStreamInterface|null $buffer` parameter
907+
that controls this write behavior of this stream.
908+
You can use a `null` value here in order to apply its default value.
909+
This value SHOULD NOT be changed unless you know what you're doing.
910+
911+
If you want to change the write buffer soft limit, you can pass an instance of
912+
[`WritableResourceStream`](#writableresourcestream) like this:
901913

902914
```php
903-
$buffer = $stream->getBuffer();
904-
$buffer->softLimit = 8192;
915+
$conn = stream_socket_client('tcp://google.com:80');
916+
$buffer = new WritableResourceStream($conn, $loop, 8192);
917+
$stream = new DuplexResourceStream($conn, $loop, null, $buffer);
905918
```
906919

907-
See also [`write()`](#write) for more details.
920+
See also [`WritableResourceStream`](#writableresourcestream) for more details.
908921

909922
### ThroughStream
910923

src/DuplexResourceStream.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
1616
* of bytes read may be lower if the stream resource has less than X bytes
1717
* currently available.
1818
*
19-
* This can be `null` which means read everything available from the
19+
* This can be `-1` which means read everything available from the
2020
* underlying stream resource.
2121
* This should read until the stream resource is not readable anymore
2222
* (i.e. underlying buffer drained), note that this does not neccessarily
2323
* mean it reached EOF.
2424
*
25-
* @var int|null
25+
* @var int
2626
*/
27-
public $bufferSize = 65536;
27+
private $bufferSize;
2828

2929
private $stream;
3030
protected $readable = true;
@@ -33,7 +33,7 @@ class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
3333
protected $loop;
3434
protected $buffer;
3535

36-
public function __construct($stream, LoopInterface $loop, WritableStreamInterface $buffer = null)
36+
public function __construct($stream, LoopInterface $loop, $readChunkSize = null, WritableStreamInterface $buffer = null)
3737
{
3838
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
3939
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -69,6 +69,7 @@ public function __construct($stream, LoopInterface $loop, WritableStreamInterfac
6969

7070
$this->stream = $stream;
7171
$this->loop = $loop;
72+
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
7273
$this->buffer = $buffer;
7374

7475
$that = $this;
@@ -169,7 +170,7 @@ public function handleData($stream)
169170
);
170171
});
171172

172-
$data = stream_get_contents($stream, $this->bufferSize === null ? -1 : $this->bufferSize);
173+
$data = stream_get_contents($stream, $this->bufferSize);
173174

174175
restore_error_handler();
175176

@@ -195,14 +196,6 @@ public function handleClose()
195196
}
196197
}
197198

198-
/**
199-
* @return WritableStreamInterface
200-
*/
201-
public function getBuffer()
202-
{
203-
return $this->buffer;
204-
}
205-
206199
/**
207200
* Returns whether this is a pipe resource in a legacy environment
208201
*

src/ReadableResourceStream.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class ReadableResourceStream extends EventEmitter implements ReadableStreamInter
1818
* of bytes read may be lower if the stream resource has less than X bytes
1919
* currently available.
2020
*
21-
* This can be `null` which means read everything available from the
21+
* This can be `-1` which means read everything available from the
2222
* underlying stream resource.
2323
* This should read until the stream resource is not readable anymore
2424
* (i.e. underlying buffer drained), note that this does not neccessarily
2525
* mean it reached EOF.
2626
*
27-
* @var int|null
27+
* @var int
2828
*/
29-
public $bufferSize = 65536;
29+
private $bufferSize;
3030

3131
/**
3232
* @var resource
@@ -36,7 +36,7 @@ class ReadableResourceStream extends EventEmitter implements ReadableStreamInter
3636
private $closed = false;
3737
private $loop;
3838

39-
public function __construct($stream, LoopInterface $loop)
39+
public function __construct($stream, LoopInterface $loop, $readChunkSize = null)
4040
{
4141
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
4242
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -68,6 +68,7 @@ public function __construct($stream, LoopInterface $loop)
6868

6969
$this->stream = $stream;
7070
$this->loop = $loop;
71+
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
7172

7273
$this->resume();
7374
}
@@ -123,7 +124,7 @@ public function handleData()
123124
);
124125
});
125126

126-
$data = stream_get_contents($this->stream, $this->bufferSize === null ? -1 : $this->bufferSize);
127+
$data = stream_get_contents($this->stream, $this->bufferSize);
127128

128129
restore_error_handler();
129130

src/WritableResourceStream.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
class WritableResourceStream extends EventEmitter implements WritableStreamInterface
99
{
1010
private $stream;
11-
public $softLimit = 65536;
11+
private $loop;
12+
private $softLimit;
1213

1314
private $listening = false;
1415
private $writable = true;
1516
private $closed = false;
16-
private $loop;
1717
private $data = '';
1818

19-
public function __construct($stream, LoopInterface $loop)
19+
public function __construct($stream, LoopInterface $loop, $writeBufferSoftLimit = null)
2020
{
2121
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
2222
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
@@ -35,6 +35,7 @@ public function __construct($stream, LoopInterface $loop)
3535

3636
$this->stream = $stream;
3737
$this->loop = $loop;
38+
$this->softLimit = ($writeBufferSoftLimit === null) ? 65536 : (int)$writeBufferSoftLimit;
3839
}
3940

4041
public function isWritable()

tests/DuplexResourceStreamIntegrationTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ public function testBufferReadsLargeChunks($condition, $loopFactory)
3131

3232
list($sockA, $sockB) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
3333

34-
$streamA = new DuplexResourceStream($sockA, $loop);
35-
$streamB = new DuplexResourceStream($sockB, $loop);
36-
3734
$bufferSize = 4096;
38-
$streamA->bufferSize = $bufferSize;
39-
$streamB->bufferSize = $bufferSize;
35+
$streamA = new DuplexResourceStream($sockA, $loop, $bufferSize);
36+
$streamB = new DuplexResourceStream($sockB, $loop, $bufferSize);
4037

41-
$testString = str_repeat("*", $streamA->bufferSize + 1);
38+
$testString = str_repeat("*", $bufferSize + 1);
4239

4340
$buffer = "";
4441
$streamB->on('data', function ($data) use (&$buffer) {

tests/DuplexResourceStreamTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use React\Stream\DuplexResourceStream;
66
use Clue\StreamFilter as Filter;
7+
use React\Stream\WritableResourceStream;
78

89
class DuplexResourceStreamTest extends TestCase
910
{
@@ -70,9 +71,7 @@ public function testConstructorAcceptsBuffer()
7071

7172
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
7273

73-
$conn = new DuplexResourceStream($stream, $loop, $buffer);
74-
75-
$this->assertSame($buffer, $conn->getBuffer());
74+
$conn = new DuplexResourceStream($stream, $loop, null, $buffer);
7675
}
7776

7877
public function testCloseShouldEmitCloseEvent()
@@ -97,7 +96,7 @@ public function testEndShouldEndBuffer()
9796
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
9897
$buffer->expects($this->once())->method('end')->with('foo');
9998

100-
$conn = new DuplexResourceStream($stream, $loop, $buffer);
99+
$conn = new DuplexResourceStream($stream, $loop, null, $buffer);
101100
$conn->end('foo');
102101
}
103102

@@ -149,7 +148,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
149148

150149
$capturedData = null;
151150

152-
$conn = new DuplexResourceStream($stream, $loop);
151+
$conn = new DuplexResourceStream($stream, $loop, 4321);
153152
$conn->on('data', function ($data) use (&$capturedData) {
154153
$capturedData = $data;
155154
});
@@ -160,7 +159,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
160159
$conn->handleData($stream);
161160

162161
$this->assertTrue($conn->isReadable());
163-
$this->assertEquals($conn->bufferSize, strlen($capturedData));
162+
$this->assertEquals(4321, strlen($capturedData));
164163
}
165164

166165
/**
@@ -174,8 +173,7 @@ public function testDataEventDoesEmitOneChunkUntilStreamEndsWhenBufferSizeIsInfi
174173

175174
$capturedData = null;
176175

177-
$conn = new DuplexResourceStream($stream, $loop);
178-
$conn->bufferSize = null;
176+
$conn = new DuplexResourceStream($stream, $loop, -1);
179177

180178
$conn->on('data', function ($data) use (&$capturedData) {
181179
$capturedData = $data;
@@ -285,12 +283,12 @@ public function testBufferEventsShouldBubbleUp()
285283
$stream = fopen('php://temp', 'r+');
286284
$loop = $this->createLoopMock();
287285

288-
$conn = new DuplexResourceStream($stream, $loop);
286+
$buffer = new WritableResourceStream($stream, $loop);
287+
$conn = new DuplexResourceStream($stream, $loop, null, $buffer);
289288

290289
$conn->on('drain', $this->expectCallableOnce());
291290
$conn->on('error', $this->expectCallableOnce());
292291

293-
$buffer = $conn->getBuffer();
294292
$buffer->emit('drain');
295293
$buffer->emit('error', array(new \RuntimeException('Whoops')));
296294
}

tests/ReadableResourceStreamTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
120120

121121
$capturedData = null;
122122

123-
$conn = new ReadableResourceStream($stream, $loop);
123+
$conn = new ReadableResourceStream($stream, $loop, 4321);
124124
$conn->on('data', function ($data) use (&$capturedData) {
125125
$capturedData = $data;
126126
});
@@ -131,7 +131,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
131131
$conn->handleData($stream);
132132

133133
$this->assertTrue($conn->isReadable());
134-
$this->assertEquals($conn->bufferSize, strlen($capturedData));
134+
$this->assertEquals(4321, strlen($capturedData));
135135
}
136136

137137
/**
@@ -145,8 +145,7 @@ public function testDataEventDoesEmitOneChunkUntilStreamEndsWhenBufferSizeIsInfi
145145

146146
$capturedData = null;
147147

148-
$conn = new ReadableResourceStream($stream, $loop);
149-
$conn->bufferSize = null;
148+
$conn = new ReadableResourceStream($stream, $loop, -1);
150149

151150
$conn->on('data', function ($data) use (&$capturedData) {
152151
$capturedData = $data;

0 commit comments

Comments
 (0)