-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathToxiproxyManagement.php
75 lines (65 loc) · 2.1 KB
/
ToxiproxyManagement.php
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
<?php
declare(strict_types=1);
namespace League\Flysystem\AdapterTestUtilities;
use GuzzleHttp\Client;
/**
* This class provides a client for the HTTP API provided by the proxy that simulates network issues.
*
* @see https://github.com/shopify/toxiproxy#http-api
*
* @phpstan-type RegisteredProxies 'ftp'|'sftp'|'ftpd'
* @phpstan-type StreamDirection 'upstream'|'downstream'
* @phpstan-type Type 'latency'|'bandwidth'|'slow_close'|'timeout'|'reset_peer'|'slicer'|'limit_data'
* @phpstan-type Attributes array{latency?: int, jitter?: int, rate?: int, delay?: int}
* @phpstan-type Toxic array{name?: string, type: Type, stream?: StreamDirection, toxicity?: float, attributes: Attributes}
*/
final class ToxiproxyManagement
{
/** @var Client */
private $apiClient;
public function __construct(Client $apiClient)
{
$this->apiClient = $apiClient;
}
public static function forServer(string $apiUri = 'http://localhost:8474'): self
{
return new self(
new Client(
[
'base_uri' => $apiUri,
'base_url' => $apiUri, // Compatibility with older versions of Guzzle
]
)
);
}
public function removeAllToxics(): void
{
$this->apiClient->post('/reset');
}
/**
* Simulates a peer reset on the client->server direction.
*
* @param RegisteredProxies $proxyName
*/
public function resetPeerOnRequest(
string $proxyName,
int $timeoutInMilliseconds
): void {
$configuration = [
'type' => 'reset_peer',
'stream' => 'upstream',
'attributes' => ['timeout' => $timeoutInMilliseconds],
];
$this->addToxic($proxyName, $configuration);
}
/**
* Registers a network toxic for the given proxy.
*
* @param RegisteredProxies $proxyName
* @param Toxic $configuration
*/
private function addToxic(string $proxyName, array $configuration): void
{
$this->apiClient->post('/proxies/' . $proxyName . '/toxics', ['json' => $configuration]);
}
}