ricardo93borges/alphavantage-wrapper-ts

[feature] "limit call" mechanism

Closed this issue · 3 comments

Hi,
Because Alphavantage have limitation to call per minute (5 on free version, 75 on first premium account), it will be usefull to add a "limit call" mechanism.

Like this :

import delay from 'delay'

private maxCallPerMinute = 5
private requestCount = 0

public request() {
        // check limit and wait if necessary
        if (this.maxCallPerMinute && this.requestCount >= this.maxCallPerMinute) {
            await delay(60 * 1000)
            this.requestCount = 0
        }

        // make request
        this.requestCount++
        [...]

}

Previous idea was not ideal.
A better way is to track minute change :

import cron from 'node-cron'

private maxCallPerMinute = 5
private requestCount = 0

constructor() {
    cron.schedule('* * * * *', this.cronJob);
}

private cronJob() {
    this.requestCount = 0
}

public request() {
    // check limit and wait if necessary
    if (this.maxCallPerMinute) {
        while (this.requestCount >= this.maxCallPerMinute) {
            await delay(10)
        }
    }

    // make request
    this.requestCount++
    [...]

}

Hi, this package is meant only to provide TypeScript types for the Alphavantage API payloads, although a limit call mechanism is useful, it is something that should be implemented by the application that is using this package.

It would not be practical to implement this on the application side, it will be much more practical at the level of the management of the requests that is to say of the API.

Especially since it is directly linked to the API since this restriction is specific to Alpha vantage.