Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ service CounterService {
}
```

The server implementation now iterates up to the requested count, streaming each word back to the client before closing the stream:
The server implementation now iterates up to the requested count, streaming each word back to the client:

```php
use Amp\Cancellation;
Expand All @@ -572,13 +572,11 @@ use Thesis\Grpc\Server;
final readonly class CounterServer implements CounterServiceServer
{
#[\Override]
public function count(Info $request, Server\ServerStreamChannel $stream, Metadata $md, Cancellation $cancellation): void
public function count(Info $request, Metadata $md, Cancellation $cancellation): iterable
{
for ($i = 0; $i < $request->count; ++$i) {
$stream->send(new Word(random_bytes(10)));
yield new Word(random_bytes(10));
}

$stream->close();
}
}
```
Expand Down
10 changes: 8 additions & 2 deletions src/Server/ServerStreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
final readonly class ServerStreamHandler implements Handler
{
/**
* @param \Closure(TRequest, ServerStreamChannel<TRequest, TResponse>, Metadata, Cancellation): void $handler
* @param \Closure(TRequest, Metadata, Cancellation): iterable<array-key, TResponse> $handler
*/
public function __construct(
private \Closure $handler,
Expand All @@ -27,6 +27,12 @@ public function __construct(
public function handle(ServerStream $stream, Metadata $md, Cancellation $cancellation): void
{
$request = $stream->receive();
($this->handler)($request, new ServerStreamChannel($stream), $md, $cancellation);
$messages = ($this->handler)($request, $md, $cancellation);

foreach ($messages as $message) {
$stream->send($message);
}

$stream->close();
}
}
9 changes: 2 additions & 7 deletions tests/ServerStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ public function testServerStream(): void
#[\Override]
public function subscribe(
SubscribeRequest $request,
Server\ServerStreamChannel $stream,
Metadata $md,
Cancellation $cancellation,
): void {
): iterable {
$events = [
'payments' => [
new Event('payment_finished', '{"id": 1}', new Timestamp(new Number(1_771_782_096))),
Expand All @@ -100,10 +99,6 @@ public function subscribe(
],
];

foreach ($events[$request->topic] ?? throw new InvokeError(Code::FAILED_PRECONDITION, 'Unknown topic "' . $request->topic . '"') as $it) {
$stream->send($it);
}

$stream->close();
yield from $events[$request->topic] ?? throw new InvokeError(Code::FAILED_PRECONDITION, 'Unknown topic "' . $request->topic . '"');
}
}
2 changes: 1 addition & 1 deletion tests/genproto/Chat/Api/V1/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/chat_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Chat/Api/V1/MessengerServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/chat_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Chat/Api/V1/MessengerServiceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/chat_v1.proto
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/chat_v1.proto
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/chat_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Chat/Api/V1/autoload.metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
*/

Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Echos/Api/V1/EchoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/echo_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Echos/Api/V1/EchoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/echo_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Echos/Api/V1/EchoServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/echo_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Echos/Api/V1/EchoServiceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/echo_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Echos/Api/V1/EchoServiceServerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/echo_v1.proto
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/echo_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Echos/Api/V1/autoload.metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
*/

Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/File/Api/V1/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/file_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/File/Api/V1/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/file_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/File/Api/V1/FileServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/file_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/File/Api/V1/FileServiceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/file_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/File/Api/V1/FileServiceServerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/file_v1.proto
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/file_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/File/Api/V1/autoload.metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
*/

Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Topic/Api/V1/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/topic_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Topic/Api/V1/SubscribeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/topic_v1.proto
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/topic_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Topic/Api/V1/TopicServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/topic_v1.proto
*/
Expand Down
8 changes: 3 additions & 5 deletions tests/genproto/Topic/Api/V1/TopicServiceServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/topic_v1.proto
*/
Expand All @@ -14,20 +14,18 @@

use Amp\Cancellation;
use Thesis\Grpc\Metadata;
use Thesis\Grpc\Server;

/**
* @api
*/
interface TopicServiceServer
{
/**
* @param Server\ServerStreamChannel<\Topic\Api\V1\SubscribeRequest, \Topic\Api\V1\Event> $stream
* @return iterable<array-key, \Topic\Api\V1\Event>
*/
public function subscribe(
\Topic\Api\V1\SubscribeRequest $request,
Server\ServerStreamChannel $stream,
Metadata $md,
Cancellation $cancellation,
): void;
): iterable;
}
2 changes: 1 addition & 1 deletion tests/genproto/Topic/Api/V1/TopicServiceServerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
* Source: tests/protos/topic_v1.proto
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/genproto/Topic/Api/V1/autoload.metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Code generated by thesis/protoc-plugin. DO NOT EDIT.
* Versions:
* thesis/protoc-plugin — v0.1.10
* thesis/protoc-plugin — v0.1.11
* protoc — v6.33.5
*/

Expand Down