Skip to content

Commit

Permalink
解决传null报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
reatang committed Jul 27, 2023
1 parent d8dde22 commit 71a2cf3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/Middlewares/GrpcLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class GrpcLogger extends Interceptor
*/
protected $logger;

public function __construct(LoggerInterface $logger) {
public function __construct(LoggerInterface $logger)
{
$this->setLogger($logger);
}

Expand Down
22 changes: 3 additions & 19 deletions src/Middlewares/GrpcRetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Grpc\UnaryCall;
use Psr\Log\LoggerInterface;
use Reatang\GrpcPHPAbstract\Call\ResponseCall;
use Reatang\GrpcPHPAbstract\Utils\LoggerTrait;
use const Grpc\STATUS_UNAVAILABLE;
use const Grpc\STATUS_ABORTED;

Expand All @@ -17,6 +18,8 @@
*/
class GrpcRetry extends Interceptor
{
use LoggerTrait;

/** @var int 最大重试次数 */
protected $maxAttempts;

Expand All @@ -25,13 +28,6 @@ class GrpcRetry extends Interceptor

protected $retryableStatusCodes = [];

/**
* 日志记录
*
* @var LoggerInterface
*/
protected $logger;

public function __construct(
$maxAttempts = 3,
$delay = 300,
Expand Down Expand Up @@ -84,16 +80,4 @@ public function interceptUnaryUnary(

return new ResponseCall(null, $status);
}

/**
* @param LoggerInterface $logger
*
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;

return $this;
}
}
9 changes: 6 additions & 3 deletions src/Utils/LoggerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ trait LoggerTrait
protected $logger = null;

/**
* @param LoggerInterface $logger
* @param LoggerInterface|null $logger
*
* @return self
* @return $this
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(?LoggerInterface $logger)
{
$this->logger = $logger;

return $this;
}

/**
* @return LoggerInterface|null
*/
protected function getLogger(): ?LoggerInterface
{
return $this->logger;
Expand Down

0 comments on commit 71a2cf3

Please sign in to comment.