I have a requirement for which I am trying to write an algorithm and struggling a bit conceptually with. This seems like it should be a common-ish type problem so I imagine there is a name/standard solution for it but can't seem to find any material...
Basically say I have X consumers (lets say 4 for the example), and I have a target interval I want some operation to occur at (lets say every minute for the example), I need to split the interval so that all 4 of the consumers take turns hitting the interval. First part is simple - we set them all to do the operation in (Interval * # of Consumers) time, i.e. 4 minutes in this case. But now I need to stagger them, so that 1st consumers fires on 1 minute, 2nd on minute 2, 3rd on minute 3 and 4th on minute 4 - the cycle will then repeat.
I had a go at doing this and it works fine at start-up but I am struggling to deal with the case where a consumer is added or removed. The existing consumers should then 're-balance' themselves in this case. by waiting or speeding up to fit their new time-slice. How can I calculate the time they should delay themselves in order to fall into the new correct pattern.
I feel like this is a relatively simple synchronisation problem and should have been tackled before / written about but not sure what it would be called to research it. Anyone heard of this / done anything similar before?