This article was originally published at: github.com/bigo-fronte… Welcome to follow and reprint.

background

Bigo front-end began to promote BFF. Hello Farm, as the first BFF landing project, completed the landing practice from 0-1 in two months.

【 Node Combat Series 】 According to the small module split, from the developer’s point of view, how to carry out BFF high availability coding.

This series of articles is based on the EggJS framework and uses TS syntax. To improve your reading experience, it is recommended that you learn about EggJS first.

series

  • Write a retry decorator
  • 【 Node Combat Series 】 Self-realization of application cache
  • 【 Node Combat series 】 Asynchronous concurrency, custom Promise. AllSettled
  • 【 Node Actual Series 】 RPC and HTTP protocol communication
  • Use reqId to track the whole process log
  • 【 Node Combat series 】 Validate the input parameter
  • 【 Node Combat Series 】 Abnormal interruption
  • 【 Node Combat Series 】 Base64 encoding
  • 【 Node Combat Series 】 Service discovery
  • 【 Node Combat Series 】 Coding and convention
  • 【 Node Combat Series 】 Monitor alarms
  • 【 Node Combat Series 】 Unit test
  • 【 Node Combat Series 】 pressure test
  • 【 Node Combat Series 】 grayscale
  • 【 Node Combat Series 】 document
  • 【 Node Combat series 】 series summary

Please follow our Github blog for continuous updates. Github.com/bigo-fronte…

The request of concurrent

Asynchronous programming is made easy by the nodeJS event loop implementation.

We can make asynchronous calls directly using the Promise object. A core scenario of BFF is interface aggregation, which naturally requires concurrent requests from multiple interfaces.

Request concurrency allows us to reduce the request time to the current maximum request time.

For example, interfaces A, B, and C need to be aggregated, and the response time of the corresponding interfaces is 100ms, 150ms, and 200ms. For sequential requests, the aggregation takes 450ms, but for concurrent requests, the aggregation takes 200ms, and the benefits are very obvious.

Promise.allSettled

Promise.all: short circuit feature, there is a request error, go directly catch, no longer go then. Promise.allsettled: Whether the request is settled correctly or wrongly, an array object will be returned to then and the returned data will mark the difference between wrong and right data. However, it is a new proposal and requires NodeJS >=12.9.0. Our test environment also uses V10 version, so we need to manually encapsulate it.

Custom Promise. AllSettled

implementation

// Request concurrency, equivalent to promise.allSettled
allSettled(promises) { / / custom https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
  return new Promise(resolve= > {
    const data: any = [], len = promises.length;
    let count = len;
    for (let i = 0; i < len; i += 1) {
      const promise = promises[i];
      promise.then(res= > {
        data[i] = res;
      }, error= > {
        data[i] = error;
      }).finally(() = > { // promise has been settled
        if(! --count) { resolve(data); }}); }}); }Copy the code

Service Usage Example

const resList = await ctx.helper.allSettled([
  // 1. My farm information
  service.accounts.getAccountInfoByUid.request(),
  // 2. User information
  service.accounts.getUserExtraInfo.requestByUid(),
  // 3. Whether it is a major customer
  service.accounts.getBigClient.request(),
  // 4. Check whether the red dot is displayed
  service.gift.checkAppTabIsNeedShowRedPoint.request(),
  // 5. Check in today
  service.task.signHistory.isSignedToday(),
  // 6
  service.accounts.getAccountAppearanceInfo.getAppearanceList(),
  // 7. My image
  service.accounts.getAccountAppearanceInfo.appearanceInfo(),
  // 8. Package list
  service.gift.getGiftList.request(),
  // 9. Task red dot
  service.task.listCommonTask.isShowTaskRedPoint()
]);
Copy the code

summary

To sum up, a simple and practical request concurrency function is achieved.

Welcome everyone to leave a message to discuss, wish smooth work, happy life!

I’m bigO front. See you next time.