Skip to content

Commit f0a794b

Browse files
author
kamlyli
committed
修复 resource temporarily unavailable
1 parent 23de647 commit f0a794b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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 )

src/Manager.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Kamly\DomainParser;
44

5+
use Kamly\DomainParser\Exceptions\Exception;
6+
use Kamly\DomainParser\Exceptions\HttpException;
57

68
class 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
}

0 commit comments

Comments
 (0)