Skip to content

Commit

Permalink
修复 resource temporarily unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
kamlyli committed Jan 26, 2021
1 parent 23de647 commit f0a794b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
### 2021-1-26 v0.8

修复 大量调用 导致 socket_recvfrom resource temporarily unavailable

处理方案 增加重试机制
### 2019-10-28 v0.7

修复提的建议
### 2019-10-28 v0.6

修复 receive 没有限制次数导致无限循环 (感谢 杨昕 发现的 bug )
Expand Down
23 changes: 20 additions & 3 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Kamly\DomainParser;

use Kamly\DomainParser\Exceptions\Exception;
use Kamly\DomainParser\Exceptions\HttpException;

class Manager
{
protected $socket = '';
protected $RETRY_TIME = 5;

public function __construct()
{
Expand All @@ -16,16 +19,30 @@ public function __construct()
* @param string $domain
* @param array $options
* @return array
* @throws Exceptions\HttpException
* @throws HttpException
*/
public function resolve(string $domain, array $options = [])
public function resolve(string $domain, array $options = [], int $retry_time = 0)
{
// 重试次数超过规定次则跳出
if ($retry_time > $this->RETRY_TIME) {
throw new HttpException('resolve retry over 5 times');
}

$this->socket->setOptions($options)->getSocket(); // 获取 socket

$parser = new DomainTcpParser($domain); // 初始化 DomainTcpParser 类

$this->socket->send($parser->encode()); // 编码发送的内容 + 发送

return $parser->decode($this->socket->receive()); // 接受 + 解码接收到的内容
try {
$msg = $this->socket->receive();
} catch (Exception $e) {
if ($e->getMessage() == "Resource temporarily unavailable") {
return $this->resolve($domain, $options, ++$retry_time);
}
throw new HttpException($e->getMessage());
}

return $parser->decode($msg); // 接受 + 解码接收到的内容
}
}

0 comments on commit f0a794b

Please sign in to comment.