File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed
Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change 1+ ### 2021-1-26 v0.8
12
3+ 修复 大量调用 导致 socket_recvfrom resource temporarily unavailable
4+
5+ 处理方案 增加重试机制
6+ ### 2019-10-28 v0.7
7+
8+ 修复提的建议
29### 2019-10-28 v0.6
310
411修复 receive 没有限制次数导致无限循环 (感谢 杨昕 发现的 bug )
Original file line number Diff line number Diff line change 22
33namespace Kamly \DomainParser ;
44
5+ use Kamly \DomainParser \Exceptions \Exception ;
6+ use Kamly \DomainParser \Exceptions \HttpException ;
57
68class Manager
79{
810 protected $ socket = '' ;
11+ protected $ RETRY_TIME = 5 ;
912
1013 public function __construct ()
1114 {
@@ -16,16 +19,30 @@ public function __construct()
1619 * @param string $domain
1720 * @param array $options
1821 * @return array
19- * @throws Exceptions\ HttpException
22+ * @throws HttpException
2023 */
21- public function resolve (string $ domain , array $ options = [])
24+ public function resolve (string $ domain , array $ options = [], int $ retry_time = 0 )
2225 {
26+ // 重试次数超过规定次则跳出
27+ if ($ retry_time > $ this ->RETRY_TIME ) {
28+ throw new HttpException ('resolve retry over 5 times ' );
29+ }
30+
2331 $ this ->socket ->setOptions ($ options )->getSocket (); // 获取 socket
2432
2533 $ parser = new DomainTcpParser ($ domain ); // 初始化 DomainTcpParser 类
2634
2735 $ this ->socket ->send ($ parser ->encode ()); // 编码发送的内容 + 发送
2836
29- return $ parser ->decode ($ this ->socket ->receive ()); // 接受 + 解码接收到的内容
37+ try {
38+ $ msg = $ this ->socket ->receive ();
39+ } catch (Exception $ e ) {
40+ if ($ e ->getMessage () == "Resource temporarily unavailable " ) {
41+ return $ this ->resolve ($ domain , $ options , ++$ retry_time );
42+ }
43+ throw new HttpException ($ e ->getMessage ());
44+ }
45+
46+ return $ parser ->decode ($ msg ); // 接受 + 解码接收到的内容
3047 }
3148}
You can’t perform that action at this time.
0 commit comments