I'm developing an MMORPG game server, while this approach is not needed for 80% of the functions within the game server, the 20% of the functions that would use it account for 99% of resource usages.
I'm trying to accomplish a throttled function. Say you call a massive function, or you're searching a massive data structure for a specific criteria. That one call being fired could result in massive CPU usage.
For example, a massive function, when called, utilizes a steady 30% of the CPU over 4 seconds to complete, say we're searching millions of entries in an auction system by criteria. Imagine we have 4 people doing this at once. I want to make that same method to take only 1% CPU or less, taking perhaps more than 15-20 seconds to return the same result without starving hardware.
As of now I'm using this class.
public class AsyncLazy<T> : Lazy<Task<T>>
{
public AsyncLazy(Func<T> valueFactory) :
base(() => Task.Factory.StartNew(valueFactory)) { }
public AsyncLazy(Func<Task<T>> taskFactory) :
base(() => Task.Factory.StartNew(() => taskFactory()).Unwrap()) { }
}
This doesn't block in initialization of the value, and doesn't block when consuming the lazy value. However, during massive operations the executing the operation starves the hardware to complete the task, resulting in 30% CPU usage over 4 seconds. My goal is to throttle calls over a virtually unlimited amount of time so they can complete their task without starving hardware.
Edit: Thanks to Scott Chamberlin for correcting me with proper "lingo". I'm not looking to call a method lazily, but I'm looking to "Throttle" specific method calls.