From 4bc29404c1dfad3365627301a29919834dcffc34 Mon Sep 17 00:00:00 2001 From: kleninmaxim Date: Wed, 25 May 2022 12:04:28 +0300 Subject: [PATCH] Add expired time for orderbook --- config/common_config.example.php | 1 + kernel/receive_data.php | 2 ++ src/Core.php | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) 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; + } }