Skip to content

Commit

Permalink
Add support ijson SRV record
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPattell committed Sep 18, 2020
1 parent bdddd60 commit b604961
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kakadu-dev/php-ijson-microservices",
"version": "1.1.4",
"version": "1.2.0",
"description": "Package for create microservice architecture based on PHP.",
"homepage": "https://github.com/kakadu-dev/php-ijson-microservices",
"type": "library",
Expand Down
54 changes: 41 additions & 13 deletions src/Microservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ protected function __construct(string $name, array $options = [], $logDriver = f
$this->name = $name;
$this->options = array_merge($this->options, array_filter($options));

// Detect SRV host
$this->expandSrv();

MicroserviceException::$service = $name;

if ($logDriver instanceof ILogDriver) {
Expand All @@ -74,23 +77,29 @@ protected function __construct(string $name, array $options = [], $logDriver = f
$this->_httpClient = $this->createHttpClient();
}

protected function __clone()
{
}

public function __wakeup()
{
throw new \Exception("Cannot unserialize a microservice.");
}

/**
* Get microservice instance
* Detect SRV record and get dns record
*
* @return Microservice
* @return void
*/
public static function getInstance(): Microservice
private function expandSrv(): void
{
return self::$_instance;
if (substr($this->options['ijson'], -4) !== '.srv') {
return;
}

$part = explode('://', $this->options['ijson']);
$srvRecords = dns_get_record($part[1], DNS_SRV);

// Sort
$priority = array_column($srvRecords, 'pri');
$weight = array_column($srvRecords, 'weight');
array_multisort($priority, SORT_ASC, $weight, SORT_ASC, $srvRecords);

$host = $srvRecords[0]['target'] ?? null;
$port = $srvRecords[0]['port'] ?? null;

$this->options['ijson'] = "$part[0]://$host:$port";
}

/**
Expand All @@ -107,6 +116,25 @@ private function createHttpClient(): HttpClient
]);
}

protected function __clone()
{
}

public function __wakeup()
{
throw new \Exception("Cannot unserialize a microservice.");
}

/**
* Get microservice instance
*
* @return Microservice
*/
public static function getInstance(): Microservice
{
return self::$_instance;
}

/**
* Create microservice
*
Expand Down

0 comments on commit b604961

Please sign in to comment.