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
|
|
|
private int $attemptTimeoutUs; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Used to increase timeout value with growing reconnection attempts. Must be greater than 1.0. |
31
|
|
|
* |
32
|
|
|
* Use 1 for linear scale: |
33
|
|
|
* 1 attempt: timeout = 0 ms |
34
|
|
|
* 2 attempt: timeout = 100 ms |
35
|
|
|
* 3 attempt: timeout = 100 ms |
36
|
|
|
* 4 attempt: timeout = 100 ms |
37
|
|
|
* 5 attempt: timeout = 100 ms |
38
|
|
|
* |
39
|
|
|
* Use 2 for quadratic scale: |
40
|
|
|
* 1 attempt: timeout = 0 ms |
41
|
|
|
* 2 attempt: timeout = 100 ms |
42
|
|
|
* 3 attempt: timeout = 200 ms |
43
|
|
|
* 4 attempt: timeout = 400 ms |
44
|
|
|
* 5 attempt: timeout = 800 ms |
45
|
|
|
*/ |
46
|
|
|
private float $timeoutMultiplier; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Max sequential attempts to reconnect with a remote server before fatal exception will be thrown. |
50
|
|
|
* Must be greater than or equal to 1. |
51
|
|
|
*/ |
52
|
|
|
private int $maxAttempts; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws InvalidConfigException |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
int $attemptTimeoutUs = self::DEFAULT_ATTEMPT_TIMEOUT, |
59
|
|
|
float $timeoutMultiplier = self::DEFAULT_TIMEOUT_MULTIPLIER, |
60
|
|
|
int $maxAttempts = self::DEFAULT_MAX_ATTEMPTS |
61
|
|
|
) { |
62
|
|
|
if ($attemptTimeoutUs <= 0) { |
63
|
62 |
|
throw new InvalidConfigException('Timeout must be greater than 0.'); |
64
|
|
|
} |
65
|
|
|
if ($timeoutMultiplier < 1.0) { |
66
|
|
|
throw new InvalidConfigException('Timeout multiplier must be greater than or equal to 1.0'); |
67
|
|
|
} |
68
|
62 |
|
if ($maxAttempts <= 0) { |
69
|
1 |
|
throw new InvalidConfigException('Max attempts must be greater or equal to 1'); |
70
|
|
|
} |
71
|
61 |
|
|
72
|
1 |
|
$this->attemptTimeoutUs = $attemptTimeoutUs; |
73
|
|
|
$this->timeoutMultiplier = $timeoutMultiplier; |
74
|
60 |
|
$this->maxAttempts = $maxAttempts; |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
public function getAttemptTimeoutUs(): int |
78
|
59 |
|
{ |
79
|
59 |
|
return $this->attemptTimeoutUs; |
80
|
59 |
|
} |
81
|
59 |
|
|
82
|
|
|
public function getTimeoutMultiplier(): float |
83
|
22 |
|
{ |
84
|
|
|
return $this->timeoutMultiplier; |
85
|
22 |
|
} |
86
|
|
|
|
87
|
|
|
public function getMaxAttempts(): int |
88
|
22 |
|
{ |
89
|
|
|
return $this->maxAttempts; |
90
|
22 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
22 |
|
* @throws InvalidConfigException |
94
|
|
|
*/ |
95
|
22 |
|
public static function createFromArray(array $options): self |
96
|
|
|
{ |
97
|
|
|
return new self( |
98
|
|
|
$options['attempt_timeout_us'] ?? self::DEFAULT_ATTEMPT_TIMEOUT, |
99
|
|
|
$options['timeout_multiplier'] ?? self::DEFAULT_TIMEOUT_MULTIPLIER, |
100
|
|
|
$options['max_attempts'] ?? self::DEFAULT_MAX_ATTEMPTS |
101
|
35 |
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|