Axios获取请求超时被吞噬(Promise)

我一直在研究这个数据收集模块,这个模块应该是从不同的比特币市场获得数据,并将所有接收到的数据标准化,以便将其插入到mongodb数据库中供以后使用。 (该模块是在节点4.3.x中编写的)

我遇到的问题是我需要在数据库中表示数据的一致性。 所以,每当一个请求超时,而不是捕获获取请求并logging一个错误,我想parsing一个'0'。 此外,收到的数据包含需要按顺序切割的交易。 这需要发生,以便交易可以正确地切割,所以数据不会被写两次。 为此,我实施了两个队列:

1:TimestampQueue – 保存时间戳。 下一个预期的响应是[0]中的时间戳

2:objectQueue – 保存收到的响应

=>只要objectQueue [0]中的对象等于timestampQueue [0] =>中的时间戳,就执行数据操作并插入到数据库中。

问题在于,应该捕获超时的axios.get请求并不一致。

它发生在随机的时间框架之后,但平均而言,队列在2小时之后就会卡住了。

为了使这里更清楚一些重要的代码片段:httpclient使axios请求:

get(url) { return this.instance.get(url) //instance just defined a timeout. Nothing special .then(response => { return response.data; }) .catch(error => { throw error; //THIS SEEMINGLY DOESN'T GET EXECUTED IN THE DESCRIBED CASE }); } 

现在解决请求的市场处理者:

 getMethodFromMarket(method, market, timestamp){ if(this.markets[market]){ if(this.markets[market].methods[method]) { var url = this.markets[market].methods[method]; let result = {}; result[method] = {}; result[method][market] = {}; return this.client.get(url) .then(data => { result[method][market] = data; log.debug("Successfully received " + method + " for " + market + " : " + timestamp); return result; }) .catch(err => { result[method][market] = 0; log.error(new Error("Failed to get " + method + " for " + market + ": " + timestamp)); log.error(err); return result; }); } else{ return Promise.reject(new Error(method + " not available for " + market)); } } else { return Promise.reject(new Error("Market not specified in config")); } } 

使所有定义市场的请求(对于一种方法)的代码并将它们join到一个对象中:

 //returns promise to get *method* from all markets specified in //config.json getAllMarkets(method, timestamp){ let getMarketsPromises = []; let result = {}; result[method] = {}; Object.keys(this.markets).forEach(market => { result[method][market] = {}; getMarketsPromises.push(this.getMethodFromMarket(method, market, timestamp)); }); return Promise.all(getMarketsPromises) .then(results => { for(let i = 0; i < results.length; i++){ let market = Object.keys(results[i][method])[0]; result[method][market] = results[i][method][market]; } log.debug("Got all markets for " + method + " for " + timestamp); return result; }) } 

这个代码使所有的方法和市场的请求,并join到最终的对象,从不同的模块操作,并插入到数据库中:

 //returns promise to get trades and depths from markets specified in //config.json getEverything(timestamp){ let getMethodPromises = []; let result = {timestamp}; this.methods.forEach(method => { result[method] = {}; getMethodPromises.push(this.getAllMarkets(method, timestamp)) }); return Promise.all(getMethodPromises) .then(results =>{ for(let i = 0; i < results.length; i++){ let method = Object.keys(results[i])[0]; result[method] = results[i][method]; } log.debug("Got everything for " + timestamp); return result; }) } 

我已经testing了整个过程,没有任何数据操作。 只有这些函数并将其插入到数据库中。

2队列的实现:

 //handles the incoming responses from markets and sorts //them according to their timestamp queueResponse(marketInfo){ this.marketInfoQueue.push(marketInfo); this.marketInfoQueue.sort(function(a, b){ return a.timestamp - b.timestamp; }) } //returns queued Responses in order of timestamps. getQueuedResponses(){ var i = 0; var results = []; log.debug("TimestampQueue: "+ this.timestampQueue[0] + " | objectQueue: " + this.marketInfoQueue[0].timestamp); while(this.marketInfoQueue[i] && this.timestampQueue[i] == this.marketInfoQueue[i].timestamp){ results.push(this.marketInfoQueue.shift()); this.timestampQueue.shift(); i++; } return results; } //pushes new timestamp into timestampQueue to keep //incoming responses in order queueTimestamp(timestamp){ this.timestampQueue.push(timestamp); } 

我一直试图解决这个问题超过3个星期了,我绝对无能为力。

TLDR:Axios获取请求不会解决或拒绝。 即使在httpClient模块中使用的实例中定义了超时值5000ms。