Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 1.74 KB

函数自动容错模板.md

File metadata and controls

71 lines (53 loc) · 1.74 KB

Name

函数自动容错模板

Author

Zero

Strategy Description

打勾调用此模板后会自动对指定的Api函数进行重试容错, 支持多交易所

Strategy Arguments

Argument Default Description
RetryInterval 500 容错重试间隔(毫秒)
Debug true 显示重试记录
EnableErrorFilter false 屏蔽常见网络错误信息
ApiList GetAccount,GetDepth,GetTicker,GetRecords,GetTrades,GetOrders,SetContractType 容错API列表

Source (javascript)

// 模板初始化时调用
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());
}

Detail

https://www.fmz.com/strategy/11609

Last Modified

2016-04-03 17:17:41