File: ToxiProxy.php

package info (click to toggle)
php-amqplib 3.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,060 kB
  • sloc: php: 13,145; makefile: 77; sh: 27
file content (136 lines) | stat: -rw-r--r-- 3,654 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace PhpAmqpLib\Tests\Functional;

use Httpful\Request;

class ToxiProxy
{
    /** @var string */
    private $name;

    /** @var string */
    private $api;

    /** @var string */
    private $host;

    /** @var int */
    private $listen;

    /** @var bool */
    private $isOpen = false;

    /**
     * @param string $name
     * @param string $host
     * @param int $port
     */
    public function __construct($name, $host, $port = 8474)
    {
        $this->name = $name;
        $this->host = $host;
        $this->api = 'http://' . $host . ':' . $port;
    }

    public function __destruct()
    {
        if ($this->isOpen) {
            $this->close();
        }
    }

    /**
     * Open new proxy connection to $upstream and listen on port $port.
     * @param string $upstream
     * @param int $listen
     */
    public function open($host, $port, $listen)
    {
        $payload = array(
            'name' => $this->name,
            'upstream' => $host . ':' . $port,
            'listen' => ':' . $listen,
        );
        $url = $this->api . '/proxies';
        $request = Request::post($url, json_encode($payload), 'json');
        $request->timeout(1);
        $request->expectsJson();
        $response = $request->send();
        if ($response->code !== 201) {
            throw new \RuntimeException('Cannot create Toxiproxy connection');
        }
        $this->listen = $listen;
        $this->isOpen = true;
    }

    /**
     * Enable proxy $type manipulation.
     * @param string $type One of latency, bandwidth, slow_close, timeout, slicer, limit_data
     * @param array $attributes
     * @param string $direction Either upstream or downstream.
     * @param float $toxicity
     * @see https://github.com/Shopify/toxiproxy#toxics
     */
    public function mode($type, $attributes = array(), $direction = 'upstream', $toxicity = 1.0)
    {
        $payload = [
            'name' => null,
            'stream' => $direction,
            'type' => $type,
            'toxicity' => $toxicity,
            'attributes' => !empty($attributes) ? $attributes : null,
        ];
        $url = sprintf('%s/proxies/%s/toxics', $this->api, $this->name);
        $request = Request::post($url, json_encode($payload), 'json');
        $request->timeout(1);
        $request->expectsJson();
        $response = $request->send();

        if ($response->code !== 200) {
            throw new \RuntimeException('Cannot set Toxiproxy connection mode');
        }
    }

    /**
     * Disable(block) proxy connection so no data can be transferred.
     * @throws \Httpful\Exception\ConnectionErrorException
     */
    public function disable()
    {
        $url = sprintf('%s/proxies/%s', $this->api, $this->name);
        $response = Request::post($url, json_encode(array('enabled' => false)), 'json')->send();
        if ($response->code !== 200) {
            throw new \RuntimeException('Cannot disable Toxiproxy connection');
        }
    }

    /**
     * Completely close connection to upstream.
     * @throws \Httpful\Exception\ConnectionErrorException
     */
    public function close()
    {
        $url = sprintf('%s/proxies/%s', $this->api, $this->name);
        $response = Request::delete($url)->send();
        if ($response->code !== 204 && $response->code !== 404) {
            throw new \RuntimeException('Cannot close Toxiproxy connection');
        }
    }

    /**
     * @return string
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * @return int|null
     */
    public function getPort()
    {
        return $this->listen;
    }
}