Skip to content

Commit

Permalink
Replace numbers with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Sep 7, 2024
1 parent 83a6f91 commit 64657d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
20 changes: 17 additions & 3 deletions src/Connection/TcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
*/
public const STATUS_CLOSED = 8;

/**
* Maximum string length for cache
*
* @var int
*/
public const MAX_CACHE_STRING_LENGTH = 2048;

/**
* Maximum cache size.
*
* @var int
*/
public const MAX_CACHE_SIZE = 512;

/**
* Emitted when socket connection is successfully established.
*
Expand Down Expand Up @@ -635,7 +649,7 @@ public function baseRead($socket, bool $checkEof = true): void
} else {
$this->bytesRead += strlen($buffer);
if ($this->recvBuffer === '') {
if (static::$enableCache && !isset($buffer[512]) && isset($requests[$buffer])) {
if (static::$enableCache && isset($requests[$buffer])) {
++self::$statistics['total_request'];
$request = $requests[$buffer];
if ($request instanceof Request) {
Expand Down Expand Up @@ -710,9 +724,9 @@ public function baseRead($socket, bool $checkEof = true): void
/** @var ProtocolInterface $parser */
$parser = $this->protocol;
$request = $parser::decode($oneRequestBuffer, $this);
if (static::$enableCache && (!is_object($request) || $request instanceof Request) && $one && !isset($oneRequestBuffer[512])) {
if (static::$enableCache && (!is_object($request) || $request instanceof Request) && $one && !isset($oneRequestBuffer[static::MAX_CACHE_STRING_LENGTH])) {
$requests[$oneRequestBuffer] = $request;
if (count($requests) > 512) {
if (count($requests) > static::MAX_CACHE_SIZE) {
unset($requests[key($requests)]);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function enableCache(bool $value)
public static function input(string $buffer, TcpConnection $connection): int
{
static $input = [];
if (!isset($buffer[512]) && isset($input[$buffer])) {
if (isset($input[$buffer])) {
return $input[$buffer];
}
$crlfPos = strpos($buffer, "\r\n\r\n");
Expand Down Expand Up @@ -148,9 +148,9 @@ public static function input(string $buffer, TcpConnection $connection): int
return 0;
}

if (!isset($buffer[512])) {
if (!isset($buffer[TcpConnection::MAX_CACHE_STRING_LENGTH])) {
$input[$buffer] = $length;
if (count($input) > 512) {
if (count($input) > TcpConnection::MAX_CACHE_SIZE) {
unset($input[key($input)]);
}
}
Expand All @@ -168,8 +168,8 @@ public static function input(string $buffer, TcpConnection $connection): int
public static function decode(string $buffer, TcpConnection $connection): Request
{
static $requests = [];
$cacheable = static::$enableCache && !isset($buffer[512]);
if (true === $cacheable && isset($requests[$buffer])) {
$cacheable = static::$enableCache && !isset($buffer[TcpConnection::MAX_CACHE_STRING_LENGTH]);
if (isset($requests[$buffer])) {
$request = clone $requests[$buffer];
$request->connection = $connection;
$connection->request = $request;
Expand All @@ -181,7 +181,7 @@ public static function decode(string $buffer, TcpConnection $connection): Reques
$connection->request = $request;
if (true === $cacheable) {
$requests[$buffer] = $request;
if (count($requests) > 512) {
if (count($requests) > TcpConnection::MAX_CACHE_SIZE) {
unset($requests[key($requests)]);
}
}
Expand Down

0 comments on commit 64657d2

Please sign in to comment.