I have a requirement to fire off web service requests to an online api and I thought that Parallel Extensions would be a good fit for my needs.
The web service in question is designed to be called repeatedly, but has a mechanism that charges you if you got over a certain number of calls per second. I obviously want to minimize my charges and so was wondering if anyone has seen a TaskScheduler that can cope with the following requirements:
- Limit the number of tasks scheduled per timespan. I guess if the number of requests exceeded this limit then it would need to throw away the task or possibly block? (to stop a back log of tasks)
- Detect if the same request is already in the scheduler to be executed but hasn’t been yet and if so not queue the second task but return the first instead.
Do people feel that these are the sorts of responsibilities a task scheduler should be dealing with or am i barking up the wrong tree? If you have alternatives I am open to suggestions.
I agree with others that TPL Dataflow sounds like a good solution for this.
To limit the processing, you could create a
TransformBlockthat doesn’t actually transform the data in any way, it just delays it if it arrived too soon after the previous data:Then create a method that produces the data (for example integers starting from 0):
It’s written asynchronously, so that if the target block isn’t able to process the items right now, it will wait.
Then write a consumer method:
And finally, link it all together and start it up:
Here,
delayBlockwill accept at most one item every 500 ms and theConsumer()method can run multiple times in parallel. To finish processing, calldelayBlock.Complete().If you want to add some caching per your #2, you could create another
TransformBlockdo the work there and link it to the other blocks.