8000 Merge pull request #113 from valga/gc · reactphp/socket@8c9e1ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c9e1ca

Browse files
authored
Merge pull request #113 from valga/gc
Reduce memory consumption for failed connections
2 parents ee817e3 + cefad72 commit 8c9e1ca

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

src/TcpConnector.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,26 @@ public function connect($uri)
9090

9191
// wait for connection
9292

93-
return $this
94-
->waitForStreamOnce($socket)
95-
->then(array($this, 'checkConnectedSocket'))
96-
->then(array($this, 'handleConnectedSocket'));
93+
return $this->waitForStreamOnce($socket);
9794
}
9895

9996
private function waitForStreamOnce($stream)
10097
{
10198
$loop = $this->loop;
10299

103-
return new Promise\Promise(function ($resolve) use ($loop, $stream) {
104-
$loop->addWriteStream($stream, function ($stream) use ($loop, $resolve) {
100+
return new Promise\Promise(function ($resolve, $reject) use ($loop, $stream) {
101+
$loop->addWriteStream($stream, function ($stream) use ($loop, $resolve, $reject) {
105102
$loop->removeWriteStream($stream);
106103

107-
$resolve($stream);
104+
// The following hack looks like the only way to
105+
// detect connection refused errors with PHP's stream sockets.
106+
if (false === stream_socket_get_name($stream, true)) {
107+
fclose($stream);
108+
109+
$reject(new \RuntimeException('Connection refused'));
110+
} else {
111+
$resolve(new Connection($stream, $loop));
112+
}
108113
});
109114
}, function () use ($loop, $stream) {
110115
$loop->removeWriteStream($stream);
@@ -113,24 +118,4 @@ private function waitForStreamOnce($stream)
113118
throw new \RuntimeException('Cancelled while waiting for TCP/IP connection to be established');
114119
});
115120
}
116-
117-
/** @internal */
118-
public function checkConnectedSocket($socket)
119-
{
120-
// The following hack looks like the only way to
121-
// detect connection refused errors with PHP's stream sockets.
122-
if (false === stream_socket_get_name($socket, true)) {
123-
fclose($socket);
124-
125-
return Promise\reject(new \RuntimeException('Connection refused'));
126-
}
127-
128-
return Promise\resolve($socket);
129-
}
130-
131-
/** @internal */
132-
public function handleConnectedSocket($socket)
133-
{
134-
return new Connection($socket, $this->loop);
135-
}
136121
}

0 commit comments

Comments
 (0)
0