我正在使用使用 amphp 的 eventstore 客户端。我需要在我的应用程序中重用许多部分的连接。
所以我创建了一个连接提供者:
public function getConnection(): EventStoreConnection
{
if ($this->connection) {
return $this->connection;
}
$this->connection = $this->createConnection();
wait($this->connection->connectAsync());
return $this->connection;
}
然后我在很多地方使用这个连接:
\Amp\Loop::run(function () use ($eventStoreEvents, $streamName) {
$connection = $this->connectionProvider->getConnection();
// Creation of an event stream
yield $connection->appendToStreamAsync($streamName, ExpectedVersion::ANY, $eventStoreEvents);
// sleep(10); // This sleep does not work, code continue like nothing happend
});
\Amp\Loop::run(function () use ($streamName, $aggregateFqcn, &$aggregateRoot) {
$start = 0;
$count = \Prooph\EventStore\Internal\Consts::MAX_READ_SIZE;
$connection = $this->connectionProvider->getConnection();
do {
$events = [];
/** @var StreamEventsSlice $streamEventsSlice */
$streamEventsSlice = yield $connection
->readStreamEventsForwardAsync(
$streamName,
$start,
$count,
true
);
if (!$streamEventsSlice->status()->equals(SliceReadStatus::success())) {
dump($streamEventsSlice); // Event stream does not exist
// Error here: the event stream doesn't exist at this point.
throw new RuntimeGangxception('Impossible to generate the aggregate');
}
} while (! $streamEventsSlice->isEndOfStream());
});
问题:似乎第一个请求还没有结束,但第二个循环已经开始了。未注释的睡眠没有任何影响!
但是最终创建了事件流,其中包含相关事件,因此第一个请求有效。
如果我启动一个连接然后关闭然后启动一个新连接,它就可以工作。但由于每个新连接的握手开销,它很慢。
我用 Amphp 的 WebSocket 库尝试了一个类似的例子,它成功了。你看有什么不对吗?
这是我对 websocket 的测试:
$connection = \Amp\Promise\wait(connect('ws://localhost:8080'));
Amp\Loop::run(function () use ($connection) {
/** @var Connection $connection */
yield $connection->send("Hello...");
sleep(10); // This sleep works!
});
Amp\Loop::run(function () use ($connection) {
/** @var Connection $connection */
yield $connection->send("... World !");
});
$connection->close();