Issues (35)

src/Handlers/TrafficShaping.php (5 issues)

Labels
Severity
1
<?php
2
/**
3
 * Request traffic shaping
4
 * User: moyo
5
 * Date: 17/10/2017
6
 * Time: 5:18 PM
7
 */
8
9
namespace Carno\HRPC\Handlers;
10
11
use Carno\Chain\Layered;
12
use Carno\Coroutine\Context;
13
use Carno\Monitor\Metrics;
0 ignored issues
show
The type Carno\Monitor\Metrics was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Carno\Monitor\Metrics\Gauge;
0 ignored issues
show
The type Carno\Monitor\Metrics\Gauge was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Carno\Monitor\Ticker;
0 ignored issues
show
The type Carno\Monitor\Ticker was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Carno\Promise\Promised;
17
use Carno\RPC\Protocol\Request;
18
use Carno\RPC\Protocol\Response;
19
use Carno\Shaping\Options;
0 ignored issues
show
The type Carno\Shaping\Options was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Carno\Shaping\Shaper;
0 ignored issues
show
The type Carno\Shaping\Shaper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Throwable;
22
23
class TrafficShaping implements Layered
24
{
25
    /**
26
     * @var Shaper
27
     */
28
    private $shaper = null;
29
30
    /**
31
     * TrafficShaping constructor.
32
     * @param Options $options
33
     */
34
    public function __construct(Options $options)
35
    {
36
        $this->shaper = $shaper = new Shaper($options);
37
38
        Ticker::new([
39
            Metrics::gauge()->named('shaper.bucket.tokens'),
40
            Metrics::gauge()->named('shaper.acquire.waits'),
41
        ], static function (Gauge $tokens, Gauge $waits) use ($shaper) {
42
            $tokens->set($shaper->tokens());
43
            $waits->set($shaper->waits());
44
        });
45
    }
46
47
    /**
48
     * @param Request $request
49
     * @param Context $ctx
50
     * @return Promised|Request
51
     */
52
    public function inbound($request, Context $ctx)
53
    {
54
        if ($this->shaper->acquired()) {
55
            return $request;
56
        }
57
58
        return $this->shaper->queued()->then(static function () use ($request) {
59
            return $request;
60
        });
61
    }
62
63
    /**
64
     * @param Response $response
65
     * @param Context $ctx
66
     * @return Response
67
     */
68
    public function outbound($response, Context $ctx)
69
    {
70
        return $response;
71
    }
72
73
    /**
74
     * @param Throwable $e
75
     * @param Context $ctx
76
     * @throws Throwable
77
     */
78
    public function exception(Throwable $e, Context $ctx) : void
79
    {
80
        throw $e;
81
    }
82
}
83