代码之家  ›  专栏  ›  技术社区  ›  ChrisP

如何在Node应用程序中解决套接字挂起问题

  •  0
  • ChrisP  · 技术社区  · 6 年前

    所以,我有一个节点应用程序在2台服务器上运行。当从浏览器或Postman对应用程序进行调用时,负载均衡器会将调用定向到这2台服务器之一。我没有设置负载均衡器,所以我目前不知道它是如何设置的。

    Node应用程序基本上会收到一个包含一些数据的调用,从Redis中提取一个配置文件,然后将所需的数据发送到Google Analytics。然后,如果成功,它将返回一个1x1像素的gif作为响应,如果调用有问题,例如缺少数据或Redis中不存在配置文件键,则返回一个400状态和一条消息。我使用Express来处理调用,并使用Redis模块来处理应用程序和Redis之间的通信。

    偶尔,我们会在浏览器(Chrome和Firefox)中看到失败的呼叫(无响应),但它们会在Postman中成功。当浏览器中的呼叫失败时,我能够从应用程序中查看我的所有日志,并且没有错误。这些热门歌曲有时也会像我们预期的那样进入GA。

    有时,我可以在Postman中看到一个失败的呼叫,错误显示“套接字挂断”。

    我的问题,真的是,我应该在哪里寻找导致这种情况的原因?我不是一个网络人,对此我有点困惑。

    • 我检查了一下,以确保我的回答是正确的。我是 在我所有的调用中使用res.send()。
    • 我已经在本地用邮递员和 浏览器,没有问题。
    • 我在Postman中的超时设置为0 无限,我关闭了ssl验证,我没有使用 代理。
    • 我的应用程序中没有出现错误
    • 我们还有一个ruby应用程序,这个应用程序正在取代它,它没有表现出这些行为,我不确定这是由于ruby处理事情的方式与Express不同,还是我的编程不好。

    有没有可能是应用程序和Redis之间的通信变慢并导致套接字挂起错误?或者有人认为这更像是一个网络问题吗?

    我显然不希望有人能为我解决这个问题,我只是想学习并找出我需要在哪里寻找导致这些问题的原因。

    以下是一个我打电话的例子,以防我处理电话的方式把事情搞砸了:

    如何设置Redis连接:

    var redis = require('redis');
    var client = redis.createClient();
    const { promisify } = require('util');
    const hgetallAsync = promisify(client.hgetall).bind(client);
    const hsetAsync = promisify(client.hmset).bind(client);
    const existsAsync = promisify(client.exists).bind(client);
    const deleteAsync = promisify(client.del).bind(client);
    

    以及我是如何用这些打电话的。这有点复杂,但原因是redis模块进行异步调用,我试图让它们“同步”工作:

    /**
     * Record a pageview in GA
     * 
     * @var {bool} exists - This returns a value of 1 if the profile exists in Redis and 0 if it does not
     * @var {*} getall - This returns the profile, from Redis associated with the profile id
     * @var {*} reqobj - Contains referrer, user_language, and user_agent
     * @var {*} params - This is the query string from the url.  Example query string: ?a=1&b=2&c=3
     * @var {array} pageViewArray - [profile_id, pageView]
     */
    router.get('/example.gif', function (req, res, next) {
      var clientId = Before(req, res);
      if (DatasetCheck(req, res)) {
        var profileId = GetProfileIdFromCookie(req);
        var json = {};
        var params = req.query;
        if (profileId) {
          var exists = existsAsync(profileId);
          var getall = hgetallAsync(profileId);
          var validate = validation.ValidExampleRequest(clientId, req.query);
          if (validate == true) {
            var reqobj = RequestEnvironment(req);
            var mergedobj = { ...reqobj, ...params };
            var pageViewArray = PageView(mergedobj);
            if (pageViewArray != false) {
              exists.then(function (exists) {
                if (exists > 0) {
                  getall.then(async function (getall) {
                    reqobj = RequestEnvironment(req);
                    var mergedobj = { ...reqobj, ...params };
                    PageViewPart2(mergedobj, getall, pageArray[1]);
                  }).then(function () {
                    //res.send(result);
                    RespondWithPixel(res);
                  });
                } else {
                  logger.error("get example.gif ==> Profile does not exist");
                  json.status = "get example.gif ==> Profile does not exist";
                  res.status(400).send(JSON.stringify(json));
                }
              });
            } else {
              logger.error("get example.gif ==> Invalid hit");
              json.status = "get example.gif ==> Invalid hit";
              res.status(400).send(JSON.stringify(json));
            }
          } else {
            logger.error("get example.gif ==> Invalid hit request");
            json.status = "get example.gif ==> called with an empty hostname: " + params.hostname + " or path: " + params.path;
            res.status(400).send(JSON.stringify(json));
          }
        } else {
          logger.info("get example.gif ==> profileId is invalid")
          json.status = "get example.gif ==> profileId is invalid or missing";
          res.status(400).send(JSON.stringify(json));
        }
      }
    });
    
    0 回复  |  直到 6 年前