美文网首页Node.js
并发请求处理:EventProxy

并发请求处理:EventProxy

作者: 飘渺阿天 | 来源:发表于2017-07-25 15:19 被阅读25次

    || 这个世界上不存在所谓回调函数深度嵌套的问题。 —— Jackson Tian

    || 世界上本没有嵌套回调,写得人多了,也便有了}}}}}}}}}}}}。 —— fengmk2

    API文档: API Documentation
    jscoverage: 97%
    源码注解:注解文档
    

    EventProxy 仅仅是一个很轻量的工具,但是能够带来一种事件式编程的思维变化。有几个特点:

    1. 利用事件机制解耦复杂业务逻辑
    2. 移除被广为诟病的深度callback嵌套问题
    3. 将串行等待变成并行等待,提升多异步协作场景下的执行效率
    4. 友好的Error handling
    5. 无平台依赖,适合前后端,能用于浏览器和Node.js
    6. 兼容CMD,AMD以及CommonJS模块环境
      现在的,无深度嵌套的,并行的

    安装在这里不过多介绍了,直接进去主题

    调用方法

    var ep = new EventProxy();
        ep.after('got_resource_details', events.length, function(list) {
            // parse data before callback
            var idx = 0;
            list.forEach(function(it) {
                // add resource url_prefix
                it.forEach(function(one) {
                    if (!one) {
                        return;
                    }
                    var loc = one.location;
                    if (loc.indexOf("http") !== 0) {
                        one.location = CONFIG.RES_URL_PREFIX + loc;
                    }
                });
                events[idx++].res_list = it;
            });
            // ! RETURN HERE !
            callback(null, events);
        });
        ep.fail(callback);
        var http_prefix = '/' + CONFIG.COLL_NAME_RESOURCE + '/';
        for (var i = 0; i < events.length; i++) {
            epHttpRequestsWithPattern('got_list_resource_details',
                ['name', 'type', 'location', 'duration'],
                events[i].res_list,
                null,  // pattern
                false, // ignore_invalid
                http_prefix,
                module_db_crud_request_headers,
                ep.group('got_resource_details'));
        }
    

    封装方法

    ///////////////////////////////////////////////////////////////////////
    // UTILS: HTTP requests by EventProxy, WITH SEQUENCE.
    //  ep_idname: name of EventProxy
    //  items_need: items need to keep in result, others will be delete from result
    //  objs: objects list
    //  pattern: pattern in objects list which need to be request
    //  ignore_invalid: if invalid result need to be ignored?
    //  http_prefix: http prefix for requesting
    //  callback: callback function
    var epHttpRequestsWithPattern = function(ep_idname, items_need, objs, pattern, ignore_invalid, http_prefix, headers, callback) {
        if (ep_idname == undefined || ep_idname == null || ep_idname == "") {
            return callback(null, []);
        }
        if (items_need == undefined || items_need == null || items_need.constructor != Array || items_need.length == 0) {
            return callback(null, []);
        }
        if (objs == undefined || objs == null || objs.constructor != Array || objs.length == 0) {
            return callback(null, []);
        }
        var ep = new EventProxy();
        ep.after(ep_idname, objs.length, function(list) {
            // parse data before callback
            var idx = 0;
            list.forEach(function(it) {
                if (it.statusCode != 200 && ignore_invalid == true) {return;}
                var jdata;
                if (it.statusCode == 200) {
                    // parse json string
                    try {
                        jdata = JSON.parse(it.body);
                        utils.deleteUselessItem(jdata, items_need);
                    } catch (e) {
                        if (ignore_invalid == true) {return;}
                    }
                }
                if (pattern) {
                    if (jdata) {
                        objs[idx++][pattern] = jdata;
                    } else {
                        objs[idx++][pattern] = null;
                    }
                } else {
                    if (jdata) {
                        objs[idx++] = jdata;
                    } else {
                        objs[idx++] = null;
                    }
                }
            });
            // ! RETURN HERE !
            callback(null, objs);
        });
        ep.fail(callback);
        for (var i = 0; i < objs.length; i++) {
            var uuid;
            if (pattern) {
                uuid = objs[i][pattern];
            } else {
                uuid = objs[i];
            }
            var api_uri = http_prefix + uuid;
            var cur_timestamp = Math.floor((new Date()).getTime() / 1000);
            var token = utils.generateAccessToken(
                headers.ApiCalleeId
                , headers.ApiAccessKey
                , cur_timestamp, api_uri, '', SECRET_KEY
            );
            var request_headers = {
                ApiRequestType: headers.ApiRequestType,
                ApiCalleeId: headers.ApiCalleeId,
                ApiAccessKey: headers.ApiAccessKey,
                ApiTimestamp: cur_timestamp,
                ApiToken: token,
            };
            // send request to query event details
            request({
                uri: CONFIG.API_ADDRESS + request_headers.ApiCalleeId + api_uri,
                method: "GET",
                timeout: 5000,
                followRedirect: false,
                headers: request_headers,
            }, ep.group(ep_idname));
        }
    };
    

    主要解决的问题是在于,针对数组中的每个对象里面存在我们想要的字段。
    需要用到这个字段来进行去请求,才写出以下方法。

    EventProxy原始链接

    EventProxy原始链接

    相关文章

      网友评论

        本文标题:并发请求处理:EventProxy

        本文链接:https://www.haomeiwen.com/subject/yvwwkxtx.html