question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I was wondering how you come up with rateLimit values for given exchanges. They are usually > 1000 (as I understand this is in ms), but on exchanges which publish their limits its more like 1000/min and 1,000,000/day which comes to about 87ms rateLimit instead.

I would also like to spark conversation about implementing rateLimit natively into all libs so that we do not have to care about ddos protection in our code.

This is what I use now in my code:


let _EXCHANGES_API_LAST_CALLED = {};

function APICallTimeLog(exchange_id){
    _EXCHANGES_API_LAST_CALLED[exchange_id] = Date.now();
}

async function APISleep(exchange_id){

    let sleep = (ms) => new Promise (resolve => setTimeout (resolve, ms));

    let wait_time = APICallWait(exchange_id);

    if(wait_time)
        await sleep(wait_time);

    APICallTimeLog(exchange_id);

}

function APICallWait(exchange_id){
    if(!_EXCHANGES_API_LAST_CALLED[exchange_id])
        return 0;

    let last_called_time = parseInt(_EXCHANGES_API_LAST_CALLED[exchange_id]);

    let wait = last_called_time + config.exchanges[exchange_id].rateLimit - Date.now();

    if(wait < 0)
        return 0;

    return wait;
}

and then during API communications:


            let promise = (async () => {

                await APISleep(exchange_id);

                return await exchange.fetchTickers();
            })();

would be nice if all this were under the hood implemented. I am willing to look into this if there is interest, thanks for your opinion

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
kroitorcommented, Jan 21, 2018

@michnovka here’s the plan:

  1. check out the entire history, from how it all started to where it is now:
  • initially, we had a naive implementation like yours, that sleeps regular intervals between requests
  • it was slightly different from real rate limits that don’t limit the timing between requests and only limit the amount of requests per a period of time, thus allowing bursts
  • @samholt proposed the more advanced tokenBucket implementation in Python, here https://github.com/ccxt/ccxt/pull/373
  • it was ported to JS, the PHP implementation still remains semi-naive (to be reworked)
  1. check out how current rate-limiter works:

(we are working on the js/base and some files may be moving around in the meantime, don’t panic 😉)

  1. check out related issues and other important info:
  1. you probably already know this: https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md

  2. ask questions if any )

  3. your contributions and pull-requests are welcome 😉

Let me know if it answers your question )

0reactions
kroitorcommented, Mar 3, 2018

@Codewyvern

Does that mean the rate limit is fully automatically handled by the library

Yep.

do I still need to call it manually like here?

Nope. You should either do it manually, or enable and use the built-in rate-limiting in the library, but not both.

The code after 'enableRateLimit': true is:

// fetch trades for all markets one by one sequentially
// while still staying below the rate limit:

;(async () => {
    for (symbol in exchange.markets)
        console.log (await exchange.fetchTrades (symbol)); 
}) ()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Rate limiting - Wikipedia
In computer networks, rate limiting is used to control the rate of requests sent or received by a network interface controller.
Read more >
Rate limits | Stripe Documentation
A rate limiter that limits the number of requests received by the API within any given second. For most APIs, Stripe allows up...
Read more >
Rate Limits - Graph API - Meta for Developers - Facebook
A rate limit is the number of API calls an app or user can make within a given time period. If this limit...
Read more >
Rate limits | Docs | Twitter Developer ... - Twitter Developers
These limits help us provide the reliable and scalable API that our developer community relies on. Each of our APIs use rate limits...
Read more >
Rate Limits - Let's Encrypt
Let's Encrypt provides rate limits to ensure fair usage by as many people ... them so renewing a certificate almost never hits a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found