back中文文档|back js中文教程|解析

npm npmdoc 2年前 (2022-01-02) 1786次浏览

back中文文档|back js中文教程|解析

安装命令:npm i back

back

构建状态

新产品管理

一个简单的模块,用于创建指数加权的退避尝试。最初提取自Primus

注意
如果您是 1.0.0 之前的
back用户,API 已更改为以下内容。如果您不喜欢这种略有不同的抽象,而更喜欢前者稍微简单的 API,那么它仍然可以通过require('back/reconnect').

由于@Raynos的贡献,API 更改
使事情变得更简单,因为您不必自己管理选项对象的复制来处理重复的退避情况。

例子

var http = require('http');
var back = require('back');
//
// Options to use for backoff
//
// Remark: This object is modified so it should be cloned if you are dealing
// with independent backoff attempts and want to use these values as a base.
//
var options = {
  retries: 3,
  minDelay: 1000, // Defaults to 500ms
  maxDelay: 10000, // Defaults to infinity
  // The following option is shown with its default value but you will most
  // likely never define it as it creates the exponential curve.
  factor: 2,
};
 
// Where we will store the backoff instance during a particular backoff attempt
var attempt;
 
function retry(err) {
  var back = attempt || (attempt = new Back(options));
  return back.backoff(function (fail) {
    if (fail) {
      // Oh noez we never reconnect :(
      console.error('Retry failed with ' + err.message);
      process.exit(1);
    }
    //
    // Remark: .attempt and .timeout are added to this object internally
    //
    console.log('Retry attempt # ' + back.settings.attempt +
                ' being made after ' + back.settings.timeout + 'ms');
  request();
  });
}
 
function request() {
  http.get('http://localhost:9000', function (res) {
    console.log('Successful Response that will not happen!');
    //
    // If we succeeded, we would set the current to null so the next error
    // generates a new instance.
    //
    attempt = null;
  }).on('error', retry);
}
 
request();

应用程序接口

var back = new Back(backoffOpts);

Back构造函数需要你的补偿方案,并将其保存为
settings在内部状态的back对象。

back.backoff(callback)

back实例有一个backoff方法,方法在 a callback之后执行setTimeout超时当然是基于指数退避的!它会根据您传递给 back 实例的退避选项重复所有这些回调,直到它用尽它的努力。当它用尽它的尝试时,它将返回一个错误作为回调的第一个参数。

back.close()

如果您想在callback执行之前处理实例,请清除退避计时器

项目issue数量: 0

项目贡献人员列表:


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:back中文文档|back js中文教程|解析
喜欢 (0)
.excerpt .focus {display:none}