A function that automatically accepts errors

Author: Zero, Date: 2016-03-08 17:27:56
Tags: Tool

Clicking on this template automatically tries and fails the specified API function, supporting multiple exchanges


// 模板初始化时调用
function init() {
    // 过滤常见错误
    if (EnableErrorFilter) {
        SetErrorFilter("502:|503:|tcp|character|connection|unexpected|network|timeout|WSARecv|Connect|GetAddr|no such|reset|http|received|EOF|reused");
    }
    // 重定义需要容错的函数
    var names = ApiList.split(',');
    _.each(exchanges, function(e) {
        _.each(names, function(name) {
            if (typeof(e[name]) !== 'function') {
                throw "尝试容错 " + name + " 失败, 请确认存在此API并且输入正确.";
            }
            var old = e[name];
            e[name] = function() {
                var r;
                while (!(r = old.apply(this, Array.prototype.slice.call(arguments)))) {
                    if (Debug) {
                        Log(e.GetLabel(), name, "调用失败", RetryInterval, "毫秒后重试...");
                    }
                    Sleep(RetryInterval);
                }
                return r;
            };
        });
    });
    Log("容错机制开启", names);
}

// Test
function main() {
    // 此时GetTicker就不需要重试了
    Log(exchange.GetTicker());
}

Related

More

leilmlHow is this support for multi-threads like exchange.go?

momoxI don't know how to use it, a few questions: 1. Hook ((ApiList.split ((',')); does this method need to be called in its own policy? 2. If the answer to question 1 is YES, does ApiList need an assignment?

ZeroNo need, call in template to run, apiList to assign value, write function to run that needs retry