Skip to content

Commit d69ad7b

Browse files
committed
Use constructor parameter instead of public $softLimit property
1 parent c1a0765 commit d69ad7b

5 files changed

Lines changed: 33 additions & 39 deletions

File tree

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -820,11 +820,14 @@ ready to accept data.
820820
For this, it uses an in-memory buffer string to collect all outstanding writes.
821821
This buffer has a soft-limit applied which defines how much data it is willing
822822
to accept before the caller SHOULD stop sending further data.
823-
It currently defaults to 64 KiB and can be controlled through the public
824-
`$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.
825828

826829
```php
827-
$stream->softLimit = 8192;
830+
$stream = new WritableResourceStream(STDOUT, $loop, 8192);
828831
```
829832

830833
See also [`write()`](#write) for more details.
@@ -899,15 +902,22 @@ ready to accept data.
899902
For this, it uses an in-memory buffer string to collect all outstanding writes.
900903
This buffer has a soft-limit applied which defines how much data it is willing
901904
to accept before the caller SHOULD stop sending further data.
902-
It currently defaults to 64 KiB and can be controlled through the public
903-
`$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:
904913

905914
```php
906-
$buffer = $stream->getBuffer();
907-
$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);
908918
```
909919

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

912922
### ThroughStream
913923

src/DuplexResourceStream.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,6 @@ public function handleClose()
196196
}
197197
}
198198

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

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/DuplexResourceStreamTest.php

Lines changed: 3 additions & 4 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
{
@@ -71,8 +72,6 @@ public function testConstructorAcceptsBuffer()
7172
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
7273

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

7877
public function testCloseShouldEmitCloseEvent()
@@ -284,12 +283,12 @@ public function testBufferEventsShouldBubbleUp()
284283
$stream = fopen('php://temp', 'r+');
285284
$loop = $this->createLoopMock();
286285

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

289289
$conn->on('drain', $this->expectCallableOnce());
290290
$conn->on('error', $this->expectCallableOnce());
291291

292-
$buffer = $conn->getBuffer();
293292
$buffer->emit('drain');
294293
$buffer->emit('error', array(new \RuntimeException('Whoops')));
295294
}

tests/WritableStreamResourceTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ public function testWriteReturnsFalseWhenWritableResourceStreamIsFull()
115115
$loop = $this->createWriteableLoopMock();
116116
$loop->preventWrites = true;
117117

118-
$buffer = new WritableResourceStream($stream, $loop);
119-
$buffer->softLimit = 4;
118+
$buffer = new WritableResourceStream($stream, $loop, 4);
120119
$buffer->on('error', $this->expectCallableNever());
121120

122121
$this->assertTrue($buffer->write("foo"));
@@ -132,8 +131,7 @@ public function testWriteReturnsFalseWhenWritableResourceStreamIsExactlyFull()
132131
$stream = fopen('php://temp', 'r+');
133132
$loop = $this->createLoopMock();
134133

135-
$buffer = new WritableResourceStream($stream, $loop);
136-
$buffer->softLimit = 3;
134+
$buffer = new WritableResourceStream($stream, $loop, 3);
137135

138136
$this->assertFalse($buffer->write("foo"));
139137
}
@@ -148,8 +146,7 @@ public function testWriteDetectsWhenOtherSideIsClosed()
148146

149147
$loop = $this->createWriteableLoopMock();
150148

151-
$buffer = new WritableResourceStream($a, $loop);
152-
$buffer->softLimit = 4;
149+
$buffer = new WritableResourceStream($a, $loop, 4);
153150
$buffer->on('error', $this->expectCallableOnce());
154151

155152
fclose($b);
@@ -166,8 +163,7 @@ public function testEmitsDrainAfterWriteWhichExceedsBuffer()
166163
$stream = fopen('php://temp', 'r+');
167164
$loop = $this->createLoopMock();
168165

169-
$buffer = new WritableResourceStream($stream, $loop);
170-
$buffer->softLimit = 2;
166+
$buffer = new WritableResourceStream($stream, $loop, 2);
171167
$buffer->on('error', $this->expectCallableNever());
172168
$buffer->on('drain', $this->expectCallableOnce());
173169

@@ -184,8 +180,7 @@ public function testWriteInDrain()
184180
$stream = fopen('php://temp', 'r+');
185181
$loop = $this->createLoopMock();
186182

187-
$buffer = new WritableResourceStream($stream, $loop);
188-
$buffer->softLimit = 2;
183+
$buffer = new WritableResourceStream($stream, $loop, 2);
189184
$buffer->on('error', $this->expectCallableNever());
190185

191186
$buffer->once('drain', function () use ($buffer) {
@@ -209,8 +204,7 @@ public function testDrainAfterWrite()
209204
$stream = fopen('php://temp', 'r+');
210205
$loop = $this->createLoopMock();
211206

212-
$buffer = new WritableResourceStream($stream, $loop);
213-
$buffer->softLimit = 2;
207+
$buffer = new WritableResourceStream($stream, $loop, 2);
214208

215209
$buffer->on('drain', $this->expectCallableOnce());
216210

@@ -227,8 +221,7 @@ public function testDrainAfterWriteWillRemoveResourceFromLoopWithoutClosing()
227221
$loop = $this->createLoopMock();
228222
$loop->expects($this->once())->method('removeWriteStream')->with($stream);
229223

230-
$buffer = new WritableResourceStream($stream, $loop);
231-
$buffer->softLimit = 2;
224+
$buffer = new WritableResourceStream($stream, $loop, 2);
232225

233226
$buffer->on('drain', $this->expectCallableOnce());
234227

@@ -247,8 +240,7 @@ public function testClosingDuringDrainAfterWriteWillRemoveResourceFromLoopOnceAn
247240
$loop = $this->createLoopMock();
248241
$loop->expects($this->once())->method('removeWriteStream')->with($stream);
249242

250-
$buffer = new WritableResourceStream($stream, $loop);
251-
$buffer->softLimit = 2;
243+
$buffer = new WritableResourceStream($stream, $loop, 2);
252244

253245
$buffer->on('drain', function () use ($buffer) {
254246
$buffer->close();

0 commit comments

Comments
 (0)