Refactor peer resolution logic and add sync interval setting

This commit is contained in:
netkas 2025-01-15 15:49:53 -05:00
parent ab03439e04
commit 9dd8c39ce1
4 changed files with 88 additions and 31 deletions

View file

@ -153,6 +153,10 @@
// answer within the time-frame that the captcha was generated
// If expired; client is expected to request for a new captcha which will generate a new random answer.
$config->setDefault('policies.image_captcha_expires', 300);
// The amount of time in seconds it takes before a peer's external address is resolved again
// When a peer's external address is resolved, it is cached for this amount of time before resolving again.
// This reduces the amount of times a resolution request is made to the external server.
$config->setDefault('policies.peer_sync_interval', 3600);
// Storage configuration
$config->setDefault('storage.path', '/etc/socialbox'); // The main path for file storage

View file

@ -7,12 +7,14 @@
private int $maxSigningKeys;
private int $sessionInactivityExpires;
private int $imageCaptchaExpires;
private int $peerSyncInterval;
public function __construct(array $data)
{
$this->maxSigningKeys = $data['max_signing_keys'];
$this->sessionInactivityExpires = $data['session_inactivity_expires'];
$this->imageCaptchaExpires = $data['image_captcha_expires'];
$this->peerSyncInterval = $data['peer_sync_interval'];
}
/**
@ -46,4 +48,14 @@
{
return $this->imageCaptchaExpires;
}
/**
* Returns the maximum amount of seconds before the external peer resolve cache is considered expired
*
* @return int
*/
public function getPeerSyncInterval(): int
{
return $this->peerSyncInterval;
}
}

View file

@ -41,32 +41,10 @@
throw new StandardException('Peer Address Error: ' . $e->getMessage(), StandardError::RPC_INVALID_ARGUMENTS, $e);
}
// If the requested peer resides in the server, resolve the peer internally.
if($peerAddress->getDomain() === Configuration::getInstanceConfiguration()->getDomain())
{
try
{
$registeredPeer = RegisteredPeerManager::getPeerByAddress($peerAddress);
}
catch (DatabaseOperationException $e)
{
throw new StandardException('There was an unexpected error while trying to resolve the peer internally', StandardError::INTERNAL_SERVER_ERROR, $e);
}
// Return not found if the returned record is null or if the registered peer isn't enabled
if($registeredPeer === null || !$registeredPeer->isEnabled())
{
return $rpcRequest->produceError(StandardError::PEER_NOT_FOUND, sprintf('Peer %s not found', $peerAddress->getAddress()));
}
// Return standard peer representation
return $rpcRequest->produceResponse($registeredPeer->toStandardPeer());
}
// Otherwise, resolve the peer from the remote server
// Resolve the peer using the server's peer resolver, this will resolve both internal peers and external peers
try
{
return $rpcRequest->produceResponse(Socialbox::resolveExternalPeer($peerAddress));
return $rpcRequest->produceResponse(Socialbox::resolvePeer($peerAddress));
}
catch(Exception $e)
{