IdleRecycling::marking()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 3
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Idle conn recycling
4
 * User: moyo
5
 * Date: 09/08/2017
6
 * Time: 5:54 PM
7
 */
8
9
namespace Carno\Pool\Features;
10
11
use Carno\Pool\Chips\CWorker;
12
use Carno\Pool\Chips\ESJudger;
13
use Carno\Pool\Connections;
14
use Carno\Pool\Options;
15
16
class IdleRecycling
17
{
18
    use CWorker, ESJudger;
0 ignored issues
show
introduced by
The trait Carno\Pool\Chips\ESJudger requires some properties which are not provided by Carno\Pool\Features\IdleRecycling: $minIdle, $overall, $initial
Loading history...
19
20
    /**
21
     * @var Options
22
     */
23
    private $options = null;
24
25
    /**
26
     * @var array
27
     */
28
    private $idles = [];
29
30
    /**
31
     * IdleRecycling constructor.
32
     * @param Options $options
33
     * @param Connections $connections
34
     */
35
    public function __construct(Options $options, Connections $connections)
36
    {
37
        $this->options = $options;
38
39
        $this->track($connections);
40
        $this->ticker($options->icInterval, [$this, 'checking']);
41
    }
42
43
    /**
44
     * @param string $cid
45
     */
46
    public function busying(string $cid) : void
47
    {
48
        isset($this->idles[$cid]) && $this->idles[$cid] = 0;
49
    }
50
51
    /**
52
     * @param string $cid
53
     * @return bool
54
     */
55
    public function idling(string $cid) : bool
56
    {
57
        return ($last = $this->idles[$cid] ?? PHP_INT_MAX) ? time() - $last >= $this->options->idleTimeout : false;
58
    }
59
60
    /**
61
     */
62
    public function checking() : void
63
    {
64
        $this->marking();
65
66
        ($targetC = $this->verdict(
67
            $this->options,
68
            $this->conn()->cIdleCount(),
69
            $this->conn()->cBusyCount()
70
        )) ? $this->conn()->resizing($targetC, 'ir-checking') : $this->conn()->exit(false, 'recycling');
71
    }
72
73
    /**
74
     */
75
    private function marking() : void
76
    {
77
        foreach ($this->conn()->getIdles() as $ic) {
78
            ($this->idles[$ic->cid()] ?? 0) === 0 && $this->idles[$ic->cid()] = time();
79
        }
80
    }
81
}
82