Selector   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 8
c 1
b 0
f 1
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A exception() 0 3 1
A inbound() 0 4 1
A outbound() 0 3 1
A __construct() 0 3 1
1
<?php
2
/**
3
 * Endpoint selector
4
 * User: moyo
5
 * Date: 2018/7/25
6
 * Time: 11:52 AM
7
 */
8
9
namespace Carno\HRPC\Client;
10
11
use Carno\Chain\Layered;
12
use Carno\Coroutine\Context;
13
use Carno\RPC\Contracts\Client\Cluster;
14
use Carno\RPC\Protocol\Request;
15
use Carno\RPC\Protocol\Response;
16
use Throwable;
17
18
class Selector implements Layered
19
{
20
    /**
21
     * rpc extra key for selected client
22
     */
23
    public const CLI = '~v';
24
25
    /**
26
     * @var Cluster
27
     */
28
    private $cluster = null;
29
30
    /**
31
     * Selector constructor.
32
     * @param Cluster $cluster
33
     */
34
    public function __construct(Cluster $cluster)
35
    {
36
        $this->cluster = $cluster;
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @param Context $ctx
42
     * @return Request
43
     */
44
    public function inbound($request, Context $ctx)
45
    {
46
        $request->setExtra(self::CLI, $this->cluster->picking($request->server(), ...$request->getTags()));
47
        return $request;
48
    }
49
50
    /**
51
     * @param Response $response
52
     * @param Context $ctx
53
     * @return Response
54
     */
55
    public function outbound($response, Context $ctx)
56
    {
57
        return $response;
58
    }
59
60
    /**
61
     * @param Throwable $e
62
     * @param Context $ctx
63
     * @throws Throwable
64
     */
65
    public function exception(Throwable $e, Context $ctx)
66
    {
67
        throw $e;
68
    }
69
}
70