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 | /** |
||
36 | * Maximum length of a string to send |
||
37 | */ |
||
38 | const MAX_SEND_LENGTH = 1024; |
||
39 | |||
40 | const ON_ERROR_ERROR = 'error'; |
||
41 | const ON_ERROR_EXCEPTION = 'exception'; |
||
42 | const ON_ERROR_IGNORE = 'ignore'; |
||
43 | |||
44 | /** @var resource|null */ |
||
45 | protected $socket; |
||
46 | /** @var string */ |
||
47 | private $host; |
||
48 | /** @var int */ |
||
49 | private $port; |
||
50 | /** @var string */ |
||
51 | private $onError; |
||
52 | /** @var float|null */ |
||
53 | private $timeout; |
||
54 | /** @var string */ |
||
55 | private $instance; |
||
56 | /** @var int */ |
||
57 | private $numFails = 0; |
||
58 | /** @var float */ |
||
59 | private $waitTill = 0.0; |
||
60 | |||
61 | /** |
||
62 | * @param string $instance |
||
63 | * @param string $host |
||
64 | * @param int $port |
||
65 | * @param string $onError What to do on connection error |
||
66 | * @param float|null $timeout |
||
67 | */ |
||
68 | 41 | public function __construct( |
|
81 | |||
82 | 4 | public function __destruct() |
|
90 | |||
91 | /** |
||
92 | * @param string $message |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | 41 | public function write($message) |
|
117 | |||
118 | /** |
||
119 | * Ensure that we are currently connected to the socket |
||
120 | */ |
||
121 | 41 | protected function ensureConnection() |
|
127 | |||
128 | /** |
||
129 | * @return bool |
||
130 | */ |
||
131 | 41 | protected function canConnect() |
|
135 | |||
136 | /** |
||
137 | * Attempt to connect to a stream |
||
138 | * |
||
139 | * @return null|resource |
||
140 | */ |
||
141 | 41 | protected function connect() |
|
167 | } |
||
168 |
If you suppress an error, we recommend checking for the error condition explicitly: