Skip to content

Commit

Permalink
feat:grpc:context
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Jul 14, 2021
1 parent 3edafcc commit d67c1f8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ Swoole\Coroutine\run(function () use ($grpc) {

```php
$client = new Mix\Grpc\Client('127.0.0.1', 9595);
$ctx = $client->context();

$say = new Php\Micro\Grpc\Greeter\SayClient($client);

$request = new Php\Micro\Grpc\Greeter\Request();
$request->setName('xiaoming');
$ctx = new Mix\Grpc\Context();
$response = $say->Hello($ctx, $request);

var_dump($response->getMsg());
Expand Down
4 changes: 2 additions & 2 deletions protoc-gen-mix/php/template1.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class {{ .Service.Name | client }} extends Grpc\Client\AbstractClient
*
* @throws Grpc\Exception\InvokeException
*/
public function {{ $m.Name }}(Context $context, {{ name $ns $m.InputType }} $request, array $options = []): {{ name $ns $m.OutputType }}
public function {{ $m.Name }}(Context $context, {{ name $ns $m.InputType }} $request): {{ name $ns $m.OutputType }}
{
return $this->_simpleRequest('/{{ $.File.Package }}.{{ $.Service.Name }}/{{ $m.Name }}', $context, $request, new {{ name $ns $m.OutputType }}(), $options);
return $this->_simpleRequest('/{{ $.File.Package }}.{{ $.Service.Name }}/{{ $m.Name }}', $context, $request, new {{ name $ns $m.OutputType }}());
}
{{end -}}
}
Expand Down
4 changes: 2 additions & 2 deletions protos/Php/Micro/Grpc/Greeter/SayClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class SayClient extends Grpc\Client\AbstractClient
*
* @throws Grpc\Exception\RuntimeException
*/
public function Hello(Context $context, Request $request, array $options = []): Response
public function Hello(Context $context, Request $request): Response
{
return $this->_simpleRequest('/php.micro.grpc.greeter.Say/Hello', $context, $request, new Response(), $options);
return $this->_simpleRequest('/php.micro.grpc.greeter.Say/Hello', $context, $request, new Response());
}
}
6 changes: 3 additions & 3 deletions src/Client/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public function __construct(\Mix\Grpc\Client $connection)
* @return Message
* @throws RuntimeException
*/
protected function _simpleRequest(string $path, Context $context, Message $request, Message $response, array $options): Message
protected function _simpleRequest(string $path, Context $context, Message $request, Message $response): Message
{
$conn = $this->connection;
$headers = $options['headers'] ?? [];
$headers = $context->getHeaders();
$headers += [
'Content-Type' => 'application/grpc+proto',
];
$body = GrpcHelper::serialize($request);
$timeout = $options['timeout'] ?? 5.0;
$timeout = $context->getTimeout();
$resp = $conn->request('POST', $path, $headers, $body, $timeout);
if ($resp->statusCode != 200) {
throw new RuntimeException(sprintf('Response Error: status-code: %d, grpc-status %s, grpc-message: %s', $resp->statusCode, $resp->headers['grpc-status'] ?? '', $resp->headers['grpc-message'] ?? ''));
Expand Down
44 changes: 35 additions & 9 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Coroutine\Http2\Client;

class Context
{
Expand All @@ -20,9 +19,14 @@ class Context
public $response;

/**
* @var Client
* @var array
*/
public $client;
protected $headers = [];

/**
* @var float
*/
protected $timeout = 5.0;

/**
* @param Request $request
Expand All @@ -38,14 +42,36 @@ public static function fromSwoole(Request $request, Response $response): Context
}

/**
* @param Client $client
* @return Context
* @param string $key
* @param string $value
*/
public static function fromClient(Client $client): Context
public function setHeader(string $key, string $value): void
{
$ctx = new self();
$ctx->client = $client;
return $ctx;
$this->headers[$key] = $value;
}

/**
* @param float $timeout
*/
public function setTimeout(float $timeout): void
{
$this->timeout = $timeout;
}

/**
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}

/**
* @return float
*/
public function getTimeout(): float
{
return $this->timeout;
}

}

0 comments on commit d67c1f8

Please sign in to comment.