如何限制返回输出的typescript函数

时间:2018-01-04 16:28:50

标签: javascript node.js typescript throttling delayed-execution

我正在使用typescript编写node.js应用程序。我的应用程序将有几个服务相互通信。有些服务需要调用外部API。此API对可执行的每秒调用次数有限制。因此,我想创建一个包装外部API调用的服务(让我们称之为ApiService)。其他服务将调用此服务,它将在队列中收集它们的请求并按顺序执行它们 - 每秒N次请求(为简单起见,我们假设每秒1次)。当服务A调用ApiService的方法时 - 它期望接收输出(接收Promise是好的)。

现在我的问题是 - 如何在ApiService中排队这些API调用,以便每1秒执行一次队列中的下一次调用,并将该API调用的输出返回给ApiService的调用者?

以下是一个示例服务:

export class ServiceA {
   apiService: ApiService;

   public constructor(_apiService: ApiService) {
      apiService = _apiService;
   }

   public async DoWork() {
      // Do some stuff
      const output: number = await apiService.RetrieveA(param1, param2);
      // Do something with the output
   }
}

ApiService:

export class ApiService {
   queue: (() => Promise<any>)[] = [];

   public async RetrieveA(param1, param2): Promise<number> {
      const func = async () => {
         return this.CallApi(param1, param2);
      };

      this.queue.push(func);
      return func();
   }

   public async RunQueue() {
      while(true) {
         const func = this.queue.shift();
         if (!func) { continue; }
         // Call the function after 1 second
         await setTimeout(() => { func(); }, 1000);
      }
   }

   private async CallApi(param1, param2): Promise<number> {
      // Call the external API, process its output and return
   }
}

协调整个事情的主要方法:

var CronJob = require('cron').CronJob;

const apiService = new ApiService();
const service = new ServiceA(apiService);

new CronJob('* * * * * *', function() {
   service.DoWork();
}, null, true);

apiService.RunQueue();

我面临的问题是当RetrieveA方法返回func()时 - 该函数被执行。我需要返回一个Promise,但实际的函数执行需要在RunQueue()方法中发生。有没有办法实现这个目标?我可以在没有立即执行函数的情况下返回一个promise并在等待此承诺时 - 在RunQueue方法中调用函数时接收输出吗?

或者是否有不同的方法来解决我的限制返回输出的API调用问题?

我是Node.js / Typescript / JavaScript世界的新手,所以感谢任何帮助:)

2 个答案:

答案 0 :(得分:0)

我确实找到了一个有效的解决方案。我对JavaScript中的整个Promise和async概念不太熟悉,所以这可能不是最好的解决方案,但它可以完成我的具体案例。以下是其他任何希望实现类似功能的代码:

示例ServiceA保持与上述相同:

export class ServiceA {
   apiService: ApiService;

   public constructor(_apiService: ApiService) {
      apiService = _apiService;
   }

   public async DoWork() {
      // Do some stuff
      const output: number = await apiService.RetrieveA(param1, param2);
      // Do something with the output
   }
}

这里是修改后的ApiService,它返回输出的promises并限制实际的函数执行:

export class ApiService {
   // We keep the functions that need to be executed in this queue
   // and process them sequentially
   queue: (() => void)[] = [];

   public async RetrieveA(param1, param2): Promise<number> {
      // This resolver serves two purposes - it will be called when the
      // function is executed (to set the output), but will also be part
      // of the Promise that will be returned to the caller (so that the
      // caller can await for the result).
      let resolver: (value: number) => void;

      // This function will be executed by the RunQueue method when its
      // turn has come. It makes a call to the external API and when that
      // call succeeds - the resolver is called to return the result through
      // the Promise.
      const func = async () => {
         return this.CallApi(param1, param2).then(resolver);
      };

      this.queue.push(func);

      // This is the promise that we return to the caller, so that he
      // can await for the result. 
      const promise = new Promise<number>((resolve, reject) => {
         resolver = resolve;
      });

      return promise;
   }

   public async Run() {
      this.RunQueue(this.queue);
   }

   private async RunQueue(funcQueue: (() => void)[]) {
      // Get the first element of the queue
      const func = funcQueue.shift();

      // If the queue is empty - this method will continue to run
      // until a new element is added to the queue
      if (func) {
         await func();
      }

      // Recursively call the function again after 1 second
      // This will process the next element in the queue
      setTimeout(() => {
         this.RunQueue(funcQueue);
      }, 1000);
   }

   private async CallApi(param1, param2): Promise<number> {
      // Call the external API, process its output and return
   }
}

我希望代码中的注释能说清楚我想要实现的目标(以及如何实现)。

答案 1 :(得分:0)

如果您想将对RetreiveA的调用限制为每秒2次,那么所有这些都可以简单得多:

//lib is here: https://github.com/amsterdamharu/lib/blob/master/src/index.js
import * as lib from '../../src/index'
const twoPerSecond = lib.throttlePeriod(2,1000);
export class ApiService {
  public RetrieveA(param1, param2): Promise<number> {
    //removed the resolver part, according to the typescript signature
    //  it should return a promise of number but resolver actually takes
    //  that number and returns void (undefined?)
    return twoPerSecond(this.CallApi.bind(this))([param1, param2]);
  }
  //change the signature of this function to take one parameter
  //  but deconsruct the array to param1 and param2
  private async CallApi([param1, param2]): Promise<number> {
    // Call the external API, process its output and return
  }
}

只有在此类中只有一个实例时,您的方法才有效。如果您要创建多个实例并在这些实例上调用RetrieveA,则不再将请求限制为callApi