Adapt circuit-breaker component to support php8. (#3694)

This commit is contained in:
李铭昕 2021-06-11 11:01:20 +08:00 committed by GitHub
parent 2a7f69fd8d
commit 08f9a78db8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 23 deletions

View File

@ -20,6 +20,7 @@
- [#3651](https://github.com/hyperf/hyperf/pull/3651) Removed `roave/better-reflection` from LazyLoader.
- [#3654](https://github.com/hyperf/hyperf/pull/3654) Removed `roave/better-reflection` from other components.
- [#3676](https://github.com/hyperf/hyperf/pull/3676) Use `promphp/prometheus_client_php` instead of `endclothing/prometheus_client_php`.
- [#3694](https://github.com/hyperf/hyperf/pull/3694) Changed `Hyperf\CircuitBreaker\CircuitBreakerInterface` to support php8.
## Deprecated

View File

@ -12,7 +12,6 @@ parameters:
- %currentWorkingDirectory%/src/*/tests/*
- %currentWorkingDirectory%/src/*/publish/*
- %currentWorkingDirectory%/src/di/src/Resolver/FactoryResolver.php
- %currentWorkingDirectory%/src/circuit-breaker/*
- %currentWorkingDirectory%/src/config-zookeeper/*
- %currentWorkingDirectory%/src/contract/*
- %currentWorkingDirectory%/src/crontab/*

View File

@ -18,7 +18,7 @@ use Hyperf\Di\Annotation\AbstractAnnotation;
/**
* @Annotation
* @Target({"METHOD"})
* @property $timeout
* @property float $timeout
*/
class CircuitBreaker extends AbstractAnnotation
{

View File

@ -37,7 +37,7 @@ class BreakerAnnotationAspect extends AbstractAspect
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$metadata = $proceedingJoinPoint->getAnnotationMetadata();
/** @var CircuitBreaker $annotation */
/** @var null|CircuitBreaker $annotation */
$annotation = $metadata->method[CircuitBreaker::class] ?? null;
if (! $annotation) {

View File

@ -67,19 +67,19 @@ class CircuitBreaker implements CircuitBreakerInterface
return $attempt->attempt();
}
public function open()
public function open(): void
{
$this->init();
$this->state->open();
}
public function close()
public function close(): void
{
$this->init();
$this->state->close();
}
public function halfOpen()
public function halfOpen(): void
{
$this->init();
$this->state->halfOpen();
@ -105,12 +105,12 @@ class CircuitBreaker implements CircuitBreakerInterface
return $this->timestamp;
}
public function incFailCounter(): int
public function incrFailCounter(): int
{
return ++$this->failCounter;
}
public function incSuccessCounter(): int
public function incrSuccessCounter(): int
{
return ++$this->successCounter;
}

View File

@ -16,4 +16,20 @@ interface CircuitBreakerInterface
public function state(): State;
public function attempt(): bool;
public function open(): void;
public function close(): void;
public function halfOpen(): void;
public function getDuration(): float;
public function getFailCounter(): int;
public function getSuccessCounter(): int;
public function incrSuccessCounter(): int;
public function incrFailCounter(): int;
}

View File

@ -13,7 +13,7 @@ namespace Hyperf\CircuitBreaker\Exception;
class TimeoutException extends CircuitBreakerException
{
public function __construct(string $message = '', $result)
public function __construct(string $message, $result)
{
parent::__construct($message);
$this->result = $result;

View File

@ -72,7 +72,7 @@ abstract class AbstractHandler implements HandlerInterface
return sprintf('%s::%s', $proceedingJoinPoint->className, $proceedingJoinPoint->methodName);
}
protected function switch(CircuitBreaker $breaker, Annotation $annotation, bool $status)
protected function switch(CircuitBreakerInterface $breaker, Annotation $annotation, bool $status): void
{
$state = $breaker->state();
if ($state->isClose()) {
@ -84,7 +84,8 @@ abstract class AbstractHandler implements HandlerInterface
$annotation->duration
);
$this->logger->debug($info);
return $breaker->close();
$breaker->close();
return;
}
if (! $status && $breaker->getFailCounter() >= $annotation->failCounter) {
@ -94,7 +95,8 @@ abstract class AbstractHandler implements HandlerInterface
$annotation->failCounter
);
$this->logger->debug($info);
return $breaker->open();
$breaker->open();
return;
}
return;
@ -109,7 +111,8 @@ abstract class AbstractHandler implements HandlerInterface
$annotation->failCounter
);
$this->logger->debug($info);
return $breaker->open();
$breaker->open();
return;
}
if ($status && $breaker->getSuccessCounter() >= $annotation->successCounter) {
@ -119,7 +122,8 @@ abstract class AbstractHandler implements HandlerInterface
$annotation->successCounter
);
$this->logger->debug($info);
return $breaker->close();
$breaker->close();
return;
}
return;
@ -134,19 +138,20 @@ abstract class AbstractHandler implements HandlerInterface
$annotation->duration
);
$this->logger->debug($info);
return $breaker->halfOpen();
$breaker->halfOpen();
return;
}
return;
}
}
protected function call(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreaker $breaker, Annotation $annotation)
protected function call(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreakerInterface $breaker, Annotation $annotation)
{
try {
$result = $this->process($proceedingJoinPoint, $breaker, $annotation);
$breaker->incSuccessCounter();
$breaker->incrSuccessCounter();
$this->switch($breaker, $annotation, true);
} catch (\Throwable $exception) {
if (! $exception instanceof CircuitBreakerException) {
@ -157,14 +162,14 @@ abstract class AbstractHandler implements HandlerInterface
$msg = sprintf('%s::%s %s.', $proceedingJoinPoint->className, $proceedingJoinPoint->methodName, $exception->getMessage());
$this->logger->debug($msg);
$breaker->incFailCounter();
$breaker->incrFailCounter();
$this->switch($breaker, $annotation, false);
}
return $result;
}
protected function attemptCall(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreaker $breaker, Annotation $annotation)
protected function attemptCall(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreakerInterface $breaker, Annotation $annotation)
{
if ($breaker->attempt()) {
return $this->call($proceedingJoinPoint, $breaker, $annotation);
@ -173,7 +178,7 @@ abstract class AbstractHandler implements HandlerInterface
return $this->fallback($proceedingJoinPoint, $breaker, $annotation);
}
protected function fallback(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreaker $breaker, Annotation $annotation)
protected function fallback(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreakerInterface $breaker, Annotation $annotation)
{
[$className, $methodName] = $this->prepareHandler($annotation->fallback);
@ -187,7 +192,7 @@ abstract class AbstractHandler implements HandlerInterface
return $class->{$methodName}(...$argument);
}
abstract protected function process(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreaker $breaker, Annotation $annotation);
abstract protected function process(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreakerInterface $breaker, Annotation $annotation);
protected function prepareHandler(string $fallback): array
{

View File

@ -12,7 +12,7 @@ declare(strict_types=1);
namespace Hyperf\CircuitBreaker\Handler;
use Hyperf\CircuitBreaker\Annotation\CircuitBreaker as Annotation;
use Hyperf\CircuitBreaker\CircuitBreaker;
use Hyperf\CircuitBreaker\CircuitBreakerInterface;
use Hyperf\CircuitBreaker\Exception\TimeoutException;
use Hyperf\Di\Aop\ProceedingJoinPoint;
@ -20,7 +20,7 @@ class TimeoutHandler extends AbstractHandler
{
public const DEFAULT_TIMEOUT = 5;
protected function process(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreaker $breaker, Annotation $annotation)
protected function process(ProceedingJoinPoint $proceedingJoinPoint, CircuitBreakerInterface $breaker, Annotation $annotation)
{
$timeout = $annotation->value['timeout'] ?? self::DEFAULT_TIMEOUT;
$time = microtime(true);