1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of JSON RPC Client. |
4
|
|
|
* |
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Configuration; |
12
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidConfigException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Igor Lazarev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ConnectionOptions |
19
|
|
|
{ |
20
|
|
|
public const DEFAULT_ATTEMPT_TIMEOUT = 100000; // 100 ms |
21
|
|
|
public const DEFAULT_TIMEOUT_MULTIPLIER = 2.0; |
22
|
|
|
public const DEFAULT_MAX_ATTEMPTS = 5; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Reconnection attempt timeout in microseconds. Must be greater than zero. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $attemptTimeoutUs; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Used to increase timeout value with growing reconnection attempts. Must be greater than 1.0. |
33
|
|
|
* |
34
|
|
|
* Use 1 for linear scale: |
35
|
|
|
* 1 attempt: timeout = 0 ms |
36
|
|
|
* 2 attempt: timeout = 100 ms |
37
|
|
|
* 3 attempt: timeout = 100 ms |
38
|
|
|
* 4 attempt: timeout = 100 ms |
39
|
|
|
* 5 attempt: timeout = 100 ms |
40
|
|
|
* |
41
|
|
|
* Use 2 for quadratic scale: |
42
|
|
|
* 1 attempt: timeout = 0 ms |
43
|
|
|
* 2 attempt: timeout = 100 ms |
44
|
|
|
* 3 attempt: timeout = 200 ms |
45
|
|
|
* 4 attempt: timeout = 400 ms |
46
|
|
|
* 5 attempt: timeout = 800 ms |
47
|
|
|
* |
48
|
|
|
* @var float |
49
|
|
|
*/ |
50
|
|
|
private $timeoutMultiplier; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Max sequential attempts to reconnect with remote server. Must be greater or equal to 1. |
54
|
|
|
* |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
private $maxAttempts; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws InvalidConfigException |
61
|
|
|
*/ |
62
|
36 |
|
public function __construct( |
63
|
|
|
int $attemptTimeoutUs = self::DEFAULT_ATTEMPT_TIMEOUT, |
64
|
|
|
float $timeoutMultiplier = self::DEFAULT_TIMEOUT_MULTIPLIER, |
65
|
|
|
int $maxAttempts = self::DEFAULT_MAX_ATTEMPTS |
66
|
|
|
) { |
67
|
36 |
|
if ($attemptTimeoutUs <= 0) { |
68
|
1 |
|
throw new InvalidConfigException('Timeout must be greater than 0.'); |
69
|
|
|
} |
70
|
35 |
|
if ($timeoutMultiplier < 1.0) { |
71
|
1 |
|
throw new InvalidConfigException('Timeout multiplier must be greater than or equal to 1.0'); |
72
|
|
|
} |
73
|
34 |
|
if ($maxAttempts <= 0) { |
74
|
1 |
|
throw new InvalidConfigException('Max attempts must be greater or equal to 1'); |
75
|
|
|
} |
76
|
|
|
|
77
|
33 |
|
$this->attemptTimeoutUs = $attemptTimeoutUs; |
78
|
33 |
|
$this->timeoutMultiplier = $timeoutMultiplier; |
79
|
33 |
|
$this->maxAttempts = $maxAttempts; |
80
|
33 |
|
} |
81
|
|
|
|
82
|
21 |
|
public function getAttemptTimeoutUs(): int |
83
|
|
|
{ |
84
|
21 |
|
return $this->attemptTimeoutUs; |
85
|
|
|
} |
86
|
|
|
|
87
|
21 |
|
public function getTimeoutMultiplier(): float |
88
|
|
|
{ |
89
|
21 |
|
return $this->timeoutMultiplier; |
90
|
|
|
} |
91
|
|
|
|
92
|
21 |
|
public function getMaxAttempts(): int |
93
|
|
|
{ |
94
|
21 |
|
return $this->maxAttempts; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws InvalidConfigException |
99
|
|
|
*/ |
100
|
9 |
|
public static function createFromArray(array $options): self |
101
|
|
|
{ |
102
|
9 |
|
return new self( |
103
|
9 |
|
$options['attempt_timeout_us'] ?? self::DEFAULT_ATTEMPT_TIMEOUT, |
104
|
9 |
|
$options['timeout_multiplier'] ?? self::DEFAULT_TIMEOUT_MULTIPLIER, |
105
|
9 |
|
$options['max_attempts'] ?? self::DEFAULT_MAX_ATTEMPTS |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|