Skip to content

Commit

Permalink
optimize server code
Browse files Browse the repository at this point in the history
  • Loading branch information
JanHuang committed May 10, 2017
1 parent 25988b6 commit fb8af59
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace FastD\Logger;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger as MonoLogger;

Expand Down Expand Up @@ -37,7 +38,9 @@ protected function convertStatusCodeToLevel($levelCode)
public function addRecord($level, $message, array $context = array())
{
if (empty($this->handlers)) {
$this->pushHandler(new StreamHandler('php://temp'));
$emptyHandler = new StreamHandler('php://temp');
$emptyHandler->setFormatter(new LineFormatter());
$this->pushHandler($emptyHandler);
}

return parent::addRecord($this->convertStatusCodeToLevel($level), $message, $context);
Expand Down
16 changes: 6 additions & 10 deletions src/Servitization/Server/HTTPServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@ public function onRequest(swoole_http_request $swooleRequet, swoole_http_respons
{
$request = SwooleServerRequest::createServerRequestFromSwoole($swooleRequet);

try {
$response = $this->doRequest($request);
foreach ($response->getHeaders() as $key => $header) {
$swooleResponse->header($key, $response->getHeaderLine($key));
}
foreach ($response->getCookieParams() as $key => $cookieParam) {
$swooleResponse->cookie($key, $cookieParam);
}
} catch (Exception $e) {
$response = app()->handleException($e);
$response = $this->doRequest($request);
foreach ($response->getHeaders() as $key => $header) {
$swooleResponse->header($key, $response->getHeaderLine($key));
}
foreach ($response->getCookieParams() as $key => $cookieParam) {
$swooleResponse->cookie($key, $cookieParam);
}

$swooleResponse->status($response->getStatusCode());
Expand Down
6 changes: 1 addition & 5 deletions src/Servitization/Server/TCPServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ public function doWork(swoole_server $server, $fd, $data, $from_id)
$request->withParsedBody($data['args']);
}
}
try {
$response = app()->handleRequest($request);
} catch (\Exception $e) {
$response = app()->handleException($e);
}
$response = app()->handleRequest($request);
$server->send($fd, (string) $response->getBody());
app()->shutdown($request, $response);

Expand Down
6 changes: 1 addition & 5 deletions src/Servitization/Server/UDPServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public function doPacket(swoole_server $server, $data, $clientInfo)
$request->withParsedBody($data['args']);
}
}
try {
$response = app()->handleRequest($request);
} catch (\Exception $e) {
$response = app()->handleException($e);
}
$response = app()->handleRequest($request);
$server->sendto($clientInfo['address'], $clientInfo['port'], (string) $response->getBody());
app()->shutdown($request, $response);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Servitization/Server/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public function doMessage(swoole_server $server, swoole_websocket_frame $frame)
$request->withParsedBody($data['args']);
}
}
try {
$response = app()->handleRequest($request);
} catch (\Exception $e) {
$response = app()->handleException($e);
}
$response = app()->handleRequest($request);
$server->push($frame->fd, (string) $response->getBody());
app()->shutdown($request, $response);

Expand Down

0 comments on commit fb8af59

Please sign in to comment.