Fixes for high load situations

This commit is contained in:
Pinga 2023-12-02 11:21:08 +02:00
parent 9ce1196c2d
commit 94e0b9942b
2 changed files with 180 additions and 158 deletions

View file

@ -40,3 +40,55 @@ function setupLogger($logFilePath, $channelName = 'app') {
return $log; return $log;
} }
function mapContactToVCard($contactDetails, $role, $c) {
return [
'objectClassName' => 'entity',
'handle' => ['C' . $contactDetails['identifier'] . '-' . $c['roid']],
'roles' => [$role],
'remarks' => [
[
"description" => [
"This object's data has been partially omitted for privacy.",
"Only the registrar managing the record can view personal contact data."
],
"links" => [
[
"href" => "https://namingo.org",
"rel" => "alternate",
"type" => "text/html"
]
],
"title" => "REDACTED FOR PRIVACY",
"type" => "Details are withheld due to privacy restrictions."
],
[
"description" => [
"To obtain contact information for the domain registrant, please refer to the Registrar of Record's RDDS service as indicated in this report."
],
"title" => "EMAIL REDACTED FOR PRIVACY",
"type" => "Details are withheld due to privacy restrictions."
],
],
'vcardArray' => [
"vcard",
[
['version', new stdClass(), 'text', '4.0'],
["fn", new stdClass(), 'text', $contactDetails['name']],
["org", $contactDetails['org']],
["adr", [
"", // Post office box
$contactDetails['street1'], // Extended address
$contactDetails['street2'], // Street address
$contactDetails['city'], // Locality
$contactDetails['sp'], // Region
$contactDetails['pc'], // Postal code
$contactDetails['cc'] // Country name
]],
["tel", $contactDetails['voice'], ["type" => "voice"]],
["tel", $contactDetails['fax'], ["type" => "fax"]],
["email", $contactDetails['email']],
]
],
];
}

View file

@ -4,64 +4,29 @@ if (!extension_loaded('swoole')) {
die('Swoole extension must be installed'); die('Swoole extension must be installed');
} }
use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;
$c = require_once 'config.php';
require_once 'helpers.php'; require_once 'helpers.php';
$logFilePath = '/var/log/namingo/rdap.log'; $logFilePath = '/var/log/namingo/rdap.log';
$log = setupLogger($logFilePath, 'RDAP'); $log = setupLogger($logFilePath, 'RDAP');
function mapContactToVCard($contactDetails, $role, $c) { // Initialize the PDO connection pool
return [ $pool = new Swoole\Database\PDOPool(
'objectClassName' => 'entity', (new Swoole\Database\PDOConfig())
'handle' => ['C' . $contactDetails['identifier'] . '-' . $c['roid']], ->withDriver($c['db_type'])
'roles' => [$role], ->withHost($c['db_host'])
'remarks' => [ ->withPort($c['db_port'])
[ ->withDbName($c['db_database'])
"description" => [ ->withUsername($c['db_username'])
"This object's data has been partially omitted for privacy.", ->withPassword($c['db_password'])
"Only the registrar managing the record can view personal contact data." ->withCharset('utf8mb4')
], );
"links" => [
[
"href" => "https://namingo.org",
"rel" => "alternate",
"type" => "text/html"
]
],
"title" => "REDACTED FOR PRIVACY",
"type" => "Details are withheld due to privacy restrictions."
],
[
"description" => [
"To obtain contact information for the domain registrant, please refer to the Registrar of Record's RDDS service as indicated in this report."
],
"title" => "EMAIL REDACTED FOR PRIVACY",
"type" => "Details are withheld due to privacy restrictions."
],
],
'vcardArray' => [
"vcard",
[
['version', new stdClass(), 'text', '4.0'],
["fn", new stdClass(), 'text', $contactDetails['name']],
["org", $contactDetails['org']],
["adr", [
"", // Post office box
$contactDetails['street1'], // Extended address
$contactDetails['street2'], // Street address
$contactDetails['city'], // Locality
$contactDetails['sp'], // Region
$contactDetails['pc'], // Postal code
$contactDetails['cc'] // Country name
]],
["tel", $contactDetails['voice'], ["type" => "voice"]],
["tel", $contactDetails['fax'], ["type" => "fax"]],
["email", $contactDetails['email']],
]
],
];
}
// Create a Swoole HTTP server // Create a Swoole HTTP server
$http = new Swoole\Http\Server('0.0.0.0', 7500); $http = new Server('0.0.0.0', 7500);
$http->set([ $http->set([
'daemonize' => false, 'daemonize' => false,
'log_file' => '/var/log/namingo/rdap_application.log', 'log_file' => '/var/log/namingo/rdap_application.log',
@ -81,21 +46,12 @@ $http->set([
]); ]);
$log->info('server started.'); $log->info('server started.');
// Connect to the database // Handle incoming HTTP requests
$http->on('request', function ($request, $response) use ($c, $pool, $log) {
// Get a PDO connection from the pool
$pdo = $pool->get();
try { try {
$c = require_once 'config.php';
$pdo = new PDO("{$c['db_type']}:host={$c['db_host']};dbname={$c['db_database']}", $c['db_username'], $c['db_password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$log->error('DB Connection failed: ' . $e->getMessage());
$response->header('Content-Type', 'application/json');
$response->end(json_encode(['error' => 'Error connecting to database']));
return;
}
// Register a callback to handle incoming requests
$http->on('request', function ($request, $response) use ($c, $pdo, $log) {
// Extract the request path // Extract the request path
$requestPath = $request->server['request_uri']; $requestPath = $request->server['request_uri'];
@ -192,9 +148,23 @@ $http->on('request', function ($request, $response) use ($c, $pdo, $log) {
$response->status(404); $response->status(404);
$response->end(json_encode(['errorCode' => 404,'title' => 'Not Found','error' => 'Endpoint not found'])); $response->end(json_encode(['errorCode' => 404,'title' => 'Not Found','error' => 'Endpoint not found']));
} }
} catch (PDOException $e) {
// Handle database exceptions
$log->error('Database error: ' . $e->getMessage());
$response->status(500);
$response->header('Content-Type', 'application/json');
$response->end(json_encode(['Database error:' => $e->getMessage()]));
} catch (Throwable $e) {
// Catch any other exceptions or errors
$log->error('Error: ' . $e->getMessage());
$response->status(500);
$response->header('Content-Type', 'application/json');
$response->end(json_encode(['Error:' => $e->getMessage()]));
} finally {
// Return the connection to the pool
$pool->put($pdo);
}
// Close the connection
$pdo = null;
}); });
// Start the server // Start the server