1
\$\begingroup\$

I produced below code and soon it became outdated due to the blocking nature of it's loops which I can't see how to avoid that.

Work-flow summery

  1. Client send a request to "/" to get a list of JS objects via JSON format. Few hundred thousand records of them. Array A

  2. First script should retrieve translation documents for above records, translations are also obviously in few hundred thousand set. In this stage it does a network call and retrieves those. Array B

  3. Then needed Array A should be retrieved via Mysql DB.

  4. Then sorts Array A and Array B and bind those together in a very long loop. (Blocks code)

  5. Then submits the reformatted result to client.

router.get("/", async function (req, res) {
  CustomModel.count({}) // this is database call via sequelize-hierarchy library
    .then(async (count) => {
      await common
        .getLanguages(config.urls.languages_cat_endpoint) // this is a outside API call which retreives few hundred thousand records of JSON objects
        .then((langs) => {
          common.afterFindHook(CustomModel, langs.data); // This code registers a hook and it will get triggers when line findAll calls, and it pass above set of JSON objects as an arguments
        })
        .catch((e) => {
          console.log(e);
          common.sendResponse(res, 500, "Internal Server Error");
          return;
        });

      CustomModel.findAll({
        // BLOCKING code due to above 4th step. Another call via sequelize-hierarchy library, it will trigger above afterFindHook and sort->find->bind each language JSON object to each row. Few hundred thousand records here
        where: { status: true },
        hierarchy: true,
        order: [["position", "DESC"]],
      })
        .then(function (result) {
          res.send(result); // This is where the real result submitted to client.
          res.end();
        })
        .catch((error) => {
          common.sendResponse(res, 500, error);
        });
    })
    .catch((e) => {
      console.log(e);
      common.sendResponse(res, 500, "Internal Server Error");
      return;
    });
});

I'm aware there are lot of optimizations can be done to speed up the request, or reduce the time to prepare the request however I'm not interested on that.

My question here is how to remove and refactor only event-loop blocking code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.