This commit is contained in:
as2252258@163.com
2021-07-11 02:05:56 +08:00
parent ec37380175
commit 712e6b8f2a
4 changed files with 127 additions and 156 deletions
+4 -56
View File
@@ -263,17 +263,7 @@ class HttpParams
*/ */
public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int
{ {
$int = $this->required($name, $isNeed); return (int)$this->required($name, $isNeed);
if (is_null($int)) return null;
if (is_array($min)) {
list($min, $max) = $min;
}
$length = strlen((string)$int);
if (!is_numeric($int) || intval($int) != $int) {
throw new RequestException("The request parameter $name must integer.");
}
$this->between($length, $min, $max);
return (int)$int;
} }
/** /**
@@ -285,15 +275,7 @@ class HttpParams
*/ */
public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float
{ {
$int = $this->required($name, $isNeed); return (float)$this->required($name, $isNeed);
if ($int === null) {
return null;
}
if ($round > 0) {
return round(floatval($int), $round);
} else {
return floatval($int);
}
} }
/** /**
@@ -306,37 +288,7 @@ class HttpParams
*/ */
public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string
{ {
$string = $this->required($name, $isNeed); return (string)$this->required($name, $isNeed);
if ($string === null || $length === null) {
return $string;
}
if (is_numeric($length)) {
if ($length !== mb_strlen((string)$string)) {
throw new RequestException("The minimum value cannot be lower than $length, has length $length");
}
return $string;
} else {
return $this->between($string, $length[0], $length[1] ?? $length[0]);
}
}
/**
* @param $string
* @param $min
* @param $max
* @return mixed
* @throws RequestException
*/
private function between($string, $min, $max): mixed
{
$_length = mb_strlen((string)$string);
if ($min !== NULL && $_length < $min) {
throw new RequestException("The minimum value cannot be lower than $min, has length $_length");
}
if ($max !== NULL && $_length > $max) {
throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
}
return $string;
} }
/** /**
@@ -368,11 +320,7 @@ class HttpParams
*/ */
public function bool(string $name, bool $isNeed = FALSE): bool public function bool(string $name, bool $isNeed = FALSE): bool
{ {
$email = $this->required($name, $isNeed); return (boolean)$this->required($name, $isNeed);
if ($email === null) {
return false;
}
return (bool)$email;
} }
/** /**
+17 -21
View File
@@ -304,10 +304,10 @@ class Server extends HttpService
return; return;
} }
foreach ($config['events'] as $name => $_event) { foreach ($config['events'] as $name => $_event) {
if ($name === Event::SERVER_CLIENT_CLOSE) { if ($name !== Event::SERVER_CLIENT_CLOSE) {
Event::on($name, $_event);
}else{
Event::on('listen ' . $config['port'] . ' ' . $name, $_event); Event::on('listen ' . $config['port'] . ' ' . $name, $_event);
} else {
Event::on($name, $_event);
} }
} }
} }
@@ -342,10 +342,8 @@ class Server extends HttpService
exit($this->addError(sprintf('Listen %s::%d fail.', $config['host'], $config['port']))); exit($this->addError(sprintf('Listen %s::%d fail.', $config['host'], $config['port'])));
} }
if (isset($config['settings']) && is_array($config['settings'])) { $newListener->set($config['settings'] ?? []);
$newListener->set($config['settings']); $this->onListenerBind($newListener, $config);
}
$this->onListenerBind($config);
return $this->swoole; return $this->swoole;
} }
@@ -405,9 +403,9 @@ class Server extends HttpService
* @return Packet|Websocket|Receive|Http|null * @return Packet|Websocket|Receive|Http|null
* @throws Exception * @throws Exception
*/ */
private function onListenerBind($config): Packet|Websocket|Receive|Http|null private function onListenerBind($server, $config): Packet|Websocket|Receive|Http|null
{ {
$this->bindServerEvent($config['type']); $this->bindServerEvent($server, $config['type']);
$this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port'])); $this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port']));
@@ -419,18 +417,16 @@ class Server extends HttpService
* @param string $type * @param string $type
* @throws Exception * @throws Exception
*/ */
private function bindServerEvent($type = self::TCP) private function bindServerEvent($server, $type = self::TCP)
{ {
if (in_array($type, [self::PACKAGE, self::TCP])) { if (self::PACKAGE == $type) {
$this->onBindCallback('connect', [make(OnConnect::class), 'onHandler']); $this->onBindCallback($server, 'packet', [make(OnPacket::class), 'onHandler']);
$this->onBindCallback('close', [make(OnClose::class), 'onHandler']);
if ($type == self::PACKAGE) {
$this->onBindCallback('packet', [make(OnPacket::class), 'onHandler']);
} else if ($type == self::TCP) { } else if ($type == self::TCP) {
$this->onBindCallback('receive', [make(OnReceive::class), 'onHandler']); $this->onBindCallback($server, 'connect', [make(OnConnect::class), 'onHandler']);
} $this->onBindCallback($server, 'close', [make(OnClose::class), 'onHandler']);
$this->onBindCallback($server, 'receive', [make(OnReceive::class), 'onHandler']);
} else if ($type === self::HTTP) { } else if ($type === self::HTTP) {
$this->onBindCallback('request', [make(OnRequest::class), 'onHandler']); $this->onBindCallback($server, 'request', [make(OnRequest::class), 'onHandler']);
} else { } else {
throw new Exception('Unknown server type(' . $type . ').'); throw new Exception('Unknown server type(' . $type . ').');
} }
@@ -442,12 +438,12 @@ class Server extends HttpService
* @param $callback * @param $callback
* @throws Exception * @throws Exception
*/ */
public function onBindCallback($name, $callback) public function onBindCallback($server, $name, $callback)
{ {
if ($this->swoole->getCallback($name) !== null) { if ($server->getCallback($name) !== null) {
return; return;
} }
$this->swoole->on($name, $callback); $server->on($name, $callback);
} }
+18 -16
View File
@@ -32,6 +32,14 @@ use Snowflake\Snowflake;
class Service extends Component class Service extends Component
{ {
const defaultConfig = [
'open_tcp_keepalive' => true,
'tcp_keepidle' => 30,
'tcp_keepinterval' => 10,
'tcp_keepcount' => 10,
'open_http_protocol' => false,
'open_websocket_protocol' => false,
];
/** /**
* @param Packet|Websocket|Receive|Http|null $server * @param Packet|Websocket|Receive|Http|null $server
@@ -51,17 +59,13 @@ class Service extends Component
} }
$this->debug(Snowflake::listen($service)); $this->debug(Snowflake::listen($service));
$this->addCallback($mode); if (!isset($service['setting'])) {
$service['setting'] = self::defaultConfig;
}
$rpcServer = $server->addlistener($service['host'], $service['port'], $mode); $rpcServer = $server->addlistener($service['host'], $service['port'], $mode);
$rpcServer->set($service['setting'] ?? [ $rpcServer->set($service['setting']);
'open_tcp_keepalive' => true, $this->addCallback($rpcServer, $mode);
'tcp_keepidle' => 30,
'tcp_keepinterval' => 10,
'tcp_keepcount' => 10,
'open_http_protocol' => false,
'open_websocket_protocol' => false,
]);
} }
@@ -79,18 +83,16 @@ class Service extends Component
* @param $mode * @param $mode
* @throws Exception * @throws Exception
*/ */
private function addCallback($mode) private function addCallback($rpcServer, $mode)
{ {
$tcp = [SWOOLE_SOCK_TCP, SWOOLE_TCP, SWOOLE_TCP6, SWOOLE_SOCK_TCP6]; $tcp = [SWOOLE_SOCK_TCP, SWOOLE_TCP, SWOOLE_TCP6, SWOOLE_SOCK_TCP6];
$server = Snowflake::app()->getServer(); $server = Snowflake::app()->getServer();
$server->onBindCallback('connect', [make(OnConnect::class), 'onHandler']);
$server->onBindCallback('close', [make(OnClose::class), 'onHandler']);
if (in_array($mode, $tcp)) { if (in_array($mode, $tcp)) {
$server->onBindCallback('receive', [make(OnReceive::class), 'onHandler']); $server->onBindCallback($rpcServer, 'connect', [make(OnConnect::class), 'onHandler']);
$server->onBindCallback($rpcServer, 'close', [make(OnClose::class), 'onHandler']);
$server->onBindCallback($rpcServer, 'receive', [make(OnReceive::class), 'onHandler']);
} else { } else {
$server->onBindCallback('packet', [make(OnReceive::class), 'onHandler']); $server->onBindCallback($rpcServer, 'packet', [make(OnReceive::class), 'onHandler']);
} }
} }
+38 -13
View File
@@ -31,9 +31,14 @@ class Logger extends Component
private array $logs = []; private array $logs = [];
private array $sources = [];
public function init() public function init()
{ {
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']); Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']);
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'closeSource']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'closeSource']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']); Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']);
} }
@@ -167,26 +172,46 @@ class Logger extends Component
*/ */
public function write(string $messages, string $method = 'app') public function write(string $messages, string $method = 'app')
{ {
return;
if (empty($messages)) { if (empty($messages)) {
return;
} }
$fileName = 'server-' . date('Y-m-d') . '.log'; $dirName = 'log/' . ($method ?? 'app');
$dirName = 'log/' . (empty($method) ? 'app' : $method);
$logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL;
Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND);
$files = glob(storage(null, $dirName) . '/*'); $fileName = storage('server-' . date('Y-m-d') . '.log', $dirName);
if (count($files) >= 15) { if (!isset($this->sources[$fileName])) {
$this->sources[$fileName] = fopen(storage($fileName, $dirName), 'rw');
}
fwrite($this->sources[$fileName], '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL);
$this->clearHistoryFile($dirName);
}
/**
* 清理文件资源
*/
public function closeSource()
{
foreach ($this->sources as $source) {
fclose($source);
}
$this->sources = [];
}
/**
* @param string $dirName
* @throws \Exception
*/
private function clearHistoryFile(string $dirName)
{
$command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;'; $command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;';
if (Coroutine::getCid() !== -1) {
Coroutine\System::exec($command); Coroutine::getCid() !== -1 ? Coroutine\System::exec($command) : \shell_exec($command);
} else {
\shell_exec($command);
}
}
} }
/** /**
* @param $logFile * @param $logFile
* @return string * @return string