Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Added check on correlation_id
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRow committed Jul 3, 2020
1 parent d95f5f1 commit daa1e16
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
15 changes: 11 additions & 4 deletions src/NanoIPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public function __construct(string $transport_type, array $params)
}

$this->transportType = $transport_type;
$this->preamble = 'N' . chr(4) . chr(0) . chr(0);
$this->encoding = 4;
$this->preamble = 'N' . chr($this->encoding) . chr(0) . chr(0);
}


Expand Down Expand Up @@ -175,9 +176,9 @@ public function __call($method, array $params)
// 4
} elseif ($this->encoding == 4) {
$request = [
'id' => $this->id,
'message_type' => $method,
'message' => $arguments
'correlation_id' => (string) $this->id,
'message_type' => $method,
'message' => $arguments
];

// Nano auth type
Expand Down Expand Up @@ -251,6 +252,12 @@ public function __call($method, array $params)
$this->responseTime = (int) $this->response['time'];
}

if (isset($this->response['correlation_id'])) {
if ((int) $this->response['correlation_id'] != $this->id) {
$this->error = 'Correlation ID doesn\'t match';
}
}

if ($this->response['message_type'] == 'Error') {
$this->error = $this->response['message'];
$this->errorCode = (int) $this->response['message']['code'];
Expand Down
14 changes: 10 additions & 4 deletions src/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(string $hostname = 'localhost', int $port = 7076, st
$this->port = $port;
$this->url = $url;
$this->proto = 'http';
$this->API = 1;
$this->API = 1;
}


Expand Down Expand Up @@ -176,9 +176,9 @@ public function __call($method, array $params)
// v2
} elseif ($this->API == 2) {
$request = [
'id' => $this->id,
'message_type' => $method,
'message' => $arguments
'correlation_id' => (string) $this->id,
'message_type' => $method,
'message' => $arguments
];

// Nano auth type
Expand Down Expand Up @@ -268,6 +268,12 @@ public function __call($method, array $params)
$this->responseTime = (int) $this->response['time'];
}

if (isset($this->response['correlation_id'])) {
if ((int) $this->response['correlation_id'] != $this->id) {
$this->error = 'Correlation ID doesn\'t match';
}
}

if ($this->response['message_type'] == 'Error') {
$this->error = $this->response['message'];
$this->errorCode = (int) $this->response['message']['code'];
Expand Down
16 changes: 8 additions & 8 deletions test/NanoIPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

$t0 = microtime(true);

$return = $nanoipc_unix->account_weight(['account' => $account]);
$nanoipc_unix->account_weight(['account' => $account]);

echo 'Time unix 2: ' . (microtime(true) - $t0) . PHP_EOL;

print_r($return);
var_dump($nanoipc_unix);


// # Unix domain socket encoding 4
Expand All @@ -28,11 +28,11 @@

$t0 = microtime(true);

$return = $nanoipc_unix->AccountWeight(['account' => $account]);
$nanoipc_unix->AccountWeight(['account' => $account]);

echo 'Time unix 4: ' . (microtime(true) - $t0) . PHP_EOL;

print_r($return);
var_dump($nanoipc_unix);


// # TCP encoding 2
Expand All @@ -45,11 +45,11 @@

$t0 = microtime(true);

$return = $nanoipc_tcp->account_weight(['account' => $account]);
$nanoipc_tcp->account_weight(['account' => $account]);

echo 'Time TCP 2: ' . (microtime(true) - $t0) . PHP_EOL;

print_r($return);
var_dump($nanoipc_tcp);


// # TCP encoding 4
Expand All @@ -62,8 +62,8 @@

$t0 = microtime(true);

$return = $nanoipc_tcp->AccountWeight(['account' => $account]);
$nanoipc_tcp->AccountWeight(['account' => $account]);

echo 'Time TCP 4: ' . (microtime(true) - $t0) . PHP_EOL;

print_r($return);
var_dump($nanoipc_tcp);
8 changes: 4 additions & 4 deletions test/NanoRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

$t0 = microtime(true);

$return = $nanorpc->account_weight(['account' => $account]);
$nanorpc->account_weight(['account' => $account]);

echo 'Time v1: ' . (microtime(true) - $t0) . PHP_EOL;

print_r($return);
var_dump($nanorpc);


// # API v2
Expand All @@ -26,8 +26,8 @@

$t0 = microtime(true);

$return = $nanorpc->AccountWeight(['account' => $account]);
$nanorpc->AccountWeight(['account' => $account]);

echo 'Time v2: ' . (microtime(true) - $t0) . PHP_EOL;

print_r($return);
var_dump($nanorpc);

0 comments on commit daa1e16

Please sign in to comment.