Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(Bencode): Remove Bencode Library sandfoxme/bencode
Browse files Browse the repository at this point in the history
The issue #1 point of Memory exhausted problem, which is cause by
https://github.com/sandfoxme/bencode/blob/1.2.0/src/Engine/Decoder.php#L166
Use another Bencode library `OPSnet/bencode-torrent` as `Rid\Bencode\Bencode`,
and change it usage like the old one.

Closes #1
  • Loading branch information
Rhilip committed Feb 2, 2019
1 parent f6f9e6c commit 0e1a6ab
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 10 deletions.
6 changes: 3 additions & 3 deletions apps/config/httpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@
// 开启协程
'enable_coroutine' => false,
// 连接处理线程数
'reactor_num' => 8,
'reactor_num' => 1,
// 工作进程数
'worker_num' => 8,
'worker_num' => 20,
// PID 文件
'pid_file' => '/var/run/rid-httpd.pid',
// 日志文件路径
'log_file' => '/tmp/rid-httpd.log',
// 进程的最大任务数
'max_request' => 10000,
'max_request' => 3000,
// 退出等待时间
'max_wait_time' => 60,
// 异步安全重启
Expand Down
2 changes: 1 addition & 1 deletion apps/controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace apps\controllers;

use Rid\Utils\IpUtils;
use SandFoxMe\Bencode\Bencode;
use Rid\Bencode\Bencode;

use apps\models\User;
use apps\exceptions\TrackerException;
Expand Down
3 changes: 1 addition & 2 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace apps\models;

use SandFoxMe\Bencode\Bencode;

use Rid\Bencode\Bencode;
use Rid\Exceptions\NotFoundException;

class Torrent
Expand Down
5 changes: 2 additions & 3 deletions apps/models/form/TorrentUploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
use apps\models\Torrent;

use Rid\Validators\Validator;

use SandFoxMe\Bencode\Bencode;
use SandFoxMe\Bencode\Exceptions\ParseErrorException;
use Rid\Bencode\Bencode;
use Rid\Bencode\ParseErrorException;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"ext-swoole": "*",
"ext-mbstring": "*",
"ext-simplexml": "*",
"sandfoxme/bencode": "^1.2",
"robthree/twofactorauth": "^1.6",
"swiftmailer/swiftmailer": "^6.1",
"twig/twig": "^2.0",
Expand Down
145 changes: 145 additions & 0 deletions framework/Bencode/Bencode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
*
* Rewrite from : https://github.com/OPSnet/bencode-torrent/blob/master/src/Bencode.php
*
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/2/2
* Time: 20:00
*/

namespace Rid\Bencode;


class Bencode
{
/**
* Decodes a BEncoded string to the following values:
* - Dictionary (starts with d, ends with e)
* - List (starts with l, ends with e
* - Integer (starts with i, ends with e
* - String (starts with number denoting number of characters followed by : and then the string)
*
* @see https://wiki.theory.org/index.php/BitTorrentSpecification
*
* @param string $data
* @param int $pos
* @return mixed
*/
public static function decode(string $data, int &$pos = 0)
{
if ($data[$pos] === 'd') {
$pos++;
$return = [];
while ($data[$pos] !== 'e') {
$key = self::decode($data, $pos);
$value = self::decode($data, $pos);
if ($key === null || $value === null) {
break;
}
if (!is_string($key)) {
throw new ParseErrorException('Invalid key type, must be string: ' . gettype($key));
}
$return[$key] = $value;
}
ksort($return);
$pos++;
} elseif ($data[$pos] === 'l') {
$pos++;
$return = [];
while ($data[$pos] !== 'e') {
$value = self::decode($data, $pos);
$return[] = $value;
}
$pos++;
} elseif ($data[$pos] === 'i') {
$pos++;
$digits = strpos($data, 'e', $pos) - $pos;
$return = substr($data, $pos, $digits);
if ($return === '-0') {
throw new ParseErrorException('Cannot have integer value -0');
}
$multiplier = 1;
if ($return[0] === '-') {
$multiplier = -1;
$return = substr($return, 1);
}
if (!ctype_digit($return)) {
$msg = 'Cannot have non-digit values in integer number: ' . $return;
throw new ParseErrorException($msg);
}
$return = $multiplier * ((int)$return);
$pos += $digits + 1;
} else {
$digits = strpos($data, ':', $pos) - $pos;
$len = (int)substr($data, $pos, $digits);
$pos += ($digits + 1);
$return = substr($data, $pos, $len);
$pos += $len;
}

return $return;
}

/**
* @param mixed $data
* @return string
*/
public static function encode($data): string
{
if (is_array($data)) {
$return = '';
$check = -1;
$list = true;
foreach ($data as $key => $value) {
if ($key !== ++$check) {
$list = false;
break;
}
}
if ($list) {
$return .= 'l';
foreach ($data as $value) {
$return .= self::encode($value);
}
} else {
$return .= 'd';
foreach ($data as $key => $value) {
$return .= self::encode(strval($key));
$return .= self::encode($value);
}
}
$return .= 'e';
} elseif (is_integer($data)) {
$return = 'i' . $data . 'e';
} else {
$return = strlen($data) . ':' . $data;
}
return $return;
}

/**
* Given a path to a file, decode the contents of it
*
* @param string $path
* @return mixed
* @throws ParseErrorException
*/
public static function load(string $path)
{
return self::decode(file_get_contents($path, FILE_BINARY));
}

/**
* Given a path for a file, encode the contents of it
*
* @param string $path
* @param $data
* @return mixed
*/
public static function dump(string $path, $data)
{
return file_put_contents($path, self::encode($data));
}
}
14 changes: 14 additions & 0 deletions framework/Bencode/ParseErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/2/2
* Time: 20:27
*/

namespace Rid\Bencode;


class ParseErrorException extends \RuntimeException
{
}

0 comments on commit 0e1a6ab

Please sign in to comment.