ethereum / alexandria

MIT License
6 stars 9 forks source link

Detect when content ingestion is bottleneck #5

Open pipermerriam opened 4 years ago

pipermerriam commented 4 years ago

Need to put limiter in place for content ingestion. Currently it runs concurrently with no limiter.

staccDOTsol commented 1 year ago

To put a limiter in place for content ingestion, you can use various techniques depending on your specific requirements. One common approach is to implement a rate limiter, which allows you to control the number of requests or operations that can be processed within a given time frame.

In Ethereum, you can use libraries like ethers.js or web3.js to interact with the Ethereum network. These libraries provide methods for sending transactions or making API calls to Ethereum nodes. To implement a rate limiter, you can wrap these methods with a rate limiting mechanism.

One popular rate limiting technique is the token bucket algorithm. This algorithm maintains a bucket of tokens, where each token represents a unit of work. When a request is made, a token is consumed from the bucket. If the bucket is empty, the request is delayed until a token becomes available.

You can use existing libraries like async or p-limit in Node.js to easily implement a token bucket rate limiter. These libraries provide functions that allow you to limit the concurrency of asynchronous operations.

Here's an example of how you can use p-limit to implement a rate limiter for content ingestion in Node.js:

const pLimit = require('p-limit');

// Create a rate limiter with a maximum concurrency of 10
const limiter = pLimit(10);

// Wrap your content ingestion function with the rate limiter
const ingestContent = async (content) => {
  await limiter(() => yourContentIngestionFunction(content));
};

// Call the ingestContent function for each content item
ingestContent(content1);
ingestContent(content2);
ingestContent(content3);
// ...

In this example, the ingestContent function is wrapped with the rate limiter, ensuring that no more than 10 content ingestion operations are running concurrently.

Remember to adjust the concurrency limit according to your specific needs and the capacity of your system.