Connection.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Pool\SimplePool;
  12. use Hyperf\Pool\Connection as AbstractConnection;
  13. use Psr\Container\ContainerInterface;
  14. class Connection extends AbstractConnection
  15. {
  16. /**
  17. * @var callable
  18. */
  19. protected $callback;
  20. protected mixed $connection = null;
  21. public function __construct(ContainerInterface $container, Pool $pool, callable $callback)
  22. {
  23. $this->callback = $callback;
  24. parent::__construct($container, $pool);
  25. }
  26. public function getActiveConnection()
  27. {
  28. if (! $this->connection || ! $this->check()) {
  29. $this->reconnect();
  30. }
  31. return $this->connection;
  32. }
  33. public function reconnect(): bool
  34. {
  35. $this->connection = ($this->callback)();
  36. $this->lastUseTime = microtime(true);
  37. return true;
  38. }
  39. public function close(): bool
  40. {
  41. $this->connection = null;
  42. return true;
  43. }
  44. }