diff --git a/config/common_config.example.php b/config/common_config.example.php index 02a944f..0b16a7c 100644 --- a/config/common_config.example.php +++ b/config/common_config.example.php @@ -46,6 +46,7 @@ const CONFIG = [ 'exchange' => EXCHANGE, //название биржи 'exchanges' => [EXCHANGE], //список всех бирж для взятие всех данных из memcached + 'expired_orderbook_time' => 5, //сколько по времени в секундах считаем ордербуки актуальными 'min_profit' => [ 'BTC' => 0, 'ETH' => 0, diff --git a/kernel/receive_data.php b/kernel/receive_data.php index 94ae6c3..87508c9 100644 --- a/kernel/receive_data.php +++ b/kernel/receive_data.php @@ -46,6 +46,8 @@ function handler_orderbooks(string $message): void // если event как data, а node как gate if ($data['event'] == 'data' && $data['node'] == 'gate' && $data['action'] == 'orderbook' && isset($data['data'])) { + $data['data']['core_timestamp'] = microtime(true); + // записать в memcached $memcached->set( $data['exchange'] . '_' . $data['action'] . '_' . $data['data']['symbol'], diff --git a/src/Core.php b/src/Core.php index 3455afd..569cd93 100644 --- a/src/Core.php +++ b/src/Core.php @@ -59,6 +59,8 @@ private function getAllMemcachedKeys(): array private function reformatAndSeparateData(array $memcached_data): array { + $microtime = microtime(true); + foreach ($memcached_data as $key => $data) { if (isset($data)) { @@ -70,11 +72,23 @@ private function reformatAndSeparateData(array $memcached_data): array $value = $parts[2] ?? null; if ($action == 'balances') { + $balances[$exchange] = $data; + } elseif ($action == 'orderbook' && $value) { - $orderbooks[$value][$exchange] = $data; + + if ( + ($microtime - $data['core_timestamp']) <= $this->config['expired_orderbook_time'] + ) { + + $orderbooks[$value][$exchange] = $data; + + } + } else { + $undefined[$key] = $data; + } }