1 | <?php |
||
23 | class StreamWriter implements WriterInterface |
||
24 | { |
||
25 | /** |
||
26 | * Seconds to wait (as a base) for exponential back-off on connection |
||
27 | * |
||
28 | * minDelay = RETRY_INTERVAL * (2 ^ num_failed_attempts) |
||
29 | * |
||
30 | * e.g. |
||
31 | * 0, 0.1 0.2 0.4 0.8 1.6 3.2 6.4 12.8 25.6 51.2 102.4 etc... |
||
32 | */ |
||
33 | const RETRY_INTERVAL = 0.1; |
||
34 | |||
35 | const ON_ERROR_ERROR = 'error'; |
||
36 | const ON_ERROR_EXCEPTION = 'exception'; |
||
37 | const ON_ERROR_IGNORE = 'ignore'; |
||
38 | |||
39 | /** @var resource|null */ |
||
40 | protected $socket; |
||
41 | /** @var string */ |
||
42 | private $host; |
||
43 | /** @var int */ |
||
44 | private $port; |
||
45 | /** @var string */ |
||
46 | private $onError; |
||
47 | /** @var float|null */ |
||
48 | private $timeout; |
||
49 | /** @var string */ |
||
50 | private $instance; |
||
51 | /** @var int */ |
||
52 | private $numFails = 0; |
||
53 | /** @var float */ |
||
54 | private $waitTill = 0.0; |
||
55 | |||
56 | /** |
||
57 | * @param string $instance |
||
58 | * @param string $host |
||
59 | * @param int $port |
||
60 | * @param string $onError What to do on connection error |
||
61 | * @param float|null $timeout |
||
62 | */ |
||
63 | 39 | public function __construct( |
|
76 | |||
77 | 3 | public function __destruct() |
|
83 | |||
84 | /** |
||
85 | * @param string $message |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | 39 | public function write($message) |
|
105 | |||
106 | /** |
||
107 | * Ensure that we are currently connected to the socket |
||
108 | */ |
||
109 | 39 | protected function ensureConnection() |
|
115 | |||
116 | /** |
||
117 | * @return bool |
||
118 | */ |
||
119 | 39 | protected function canConnect() |
|
123 | |||
124 | /** |
||
125 | * Attempt to connect to a stream |
||
126 | * |
||
127 | * @return null|resource |
||
128 | */ |
||
129 | 39 | protected function connect() |
|
155 | } |
||
156 |
If you suppress an error, we recommend checking for the error condition explicitly: