Commit f22ca9a7 by ken

减少延时

parent ad26dcf5
const os = require('os');
function getLocalIpv4s() {
var ifaces = os.networkInterfaces();
var ips = [];
var func = function(details) {
if (details.family === 'IPv6' || details.internal) {
return;
}
ips.push(details.address);
};
for (var dev in ifaces) {
ifaces[dev].forEach(func);
}
return ips;
};
const ips = getLocalIpv4s();
exports.allIps = ips;
\ No newline at end of file
...@@ -3,6 +3,7 @@ const stream = 'wss://api.zb.com:9999/websocket'; ...@@ -3,6 +3,7 @@ const stream = 'wss://api.zb.com:9999/websocket';
const WebSocket = require('ws'); const WebSocket = require('ws');
const request = require('request'); const request = require('request');
const crypto = require('ezcrypto').Crypto; const crypto = require('ezcrypto').Crypto;
const ips = require("./util").allIps;
const options = {}; const options = {};
const observerSymbol = ["btcusdt", 'ethusdt', 'ethbtc', "eosbtc", "eosusdt", 'bccusdt', "bccbtc"]; const observerSymbol = ["btcusdt", 'ethusdt', 'ethbtc', "eosbtc", "eosusdt", 'bccusdt', "bccbtc"];
const redis = require('redis'); const redis = require('redis');
...@@ -14,8 +15,14 @@ const APISECRET = "001d5649-65f7-4c88-9d31-c429da39989d"; ...@@ -14,8 +15,14 @@ const APISECRET = "001d5649-65f7-4c88-9d31-c429da39989d";
const lastEventTimeMap = {}; const lastEventTimeMap = {};
let balanceByRestOk = true; let balanceByRestOk = true;
let balanceByWebsocketOk = true; let balanceByWebsocketOk = true;
let currentIpIndex = 0;
function selectIp() {
currentIpIndex = (currentIpIndex + 1) % ips.length;
return ips[currentIpIndex];
}
function signParams2Str(params) { function signParams2Str(params) {
// const accountReq = {accesskey: APIKEY,channel: accountInfoChannel,event: "addChannel", sign: "a5f785d4627ae21f4b4baf9c1baf1a60"}; // const accountReq = {accesskey: APIKEY,channel: accountInfoChannel,event: "addChannel", sign: "a5f785d4627ae21f4b4baf9c1baf1a60"};
let paramsSort = sortDict(params); let paramsSort = sortDict(params);
...@@ -131,6 +138,7 @@ function subscribe() { ...@@ -131,6 +138,7 @@ function subscribe() {
} }
function _request(method, url, callback) { function _request(method, url, callback) {
const ip = selectIp();
let opt = { let opt = {
url, url,
method, method,
...@@ -138,6 +146,9 @@ function _request(method, url, callback) { ...@@ -138,6 +146,9 @@ function _request(method, url, callback) {
Connection: "Keep-Alive" Connection: "Keep-Alive"
} }
}; };
if (ip) {
opt.localAddress = ip;
}
request(opt, (error, response, body) => { request(opt, (error, response, body) => {
if ( !callback ) return; if ( !callback ) return;
...@@ -207,11 +218,12 @@ function getBalanceByRest() { ...@@ -207,11 +218,12 @@ function getBalanceByRest() {
function run() { function run() {
subscribe(); subscribe();
const depthInterval = Math.max(60 * 1000 / (850 * ips.length / observerSymbol.length), 300);
setInterval(() => { setInterval(() => {
for (const symbol of observerSymbol) { for (const symbol of observerSymbol) {
depthByRest(symbol); depthByRest(symbol);
} }
}, 300); }, depthInterval);
setInterval(() => { setInterval(() => {
getBalanceByRest(); getBalanceByRest();
}, 6000); }, 6000);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment