Answer the question
In order to leave comments, you need to log in
How to split an array into certain parts?
There is a server that accepts POST requests with records. The server does not know how many records it has already accepted and how many are to be accepted for the current session. How can you divide all requests into two parts equally?
At the moment I'm using random numbers:
$portion = intval(get_option('portion')); // В каких долях делить запросы. В процентах
if ($portion === 0 || rand(0, 100) > $portion ) {
// Одна часть
} else {
// Вторая часть
}
Answer the question
In order to leave comments, you need to log in
It is better to use a queue server such as rabbitmq to distribute the load.
It is possible to toggle on each request its semaphore state , for example.
But if the purpose of this is load balancing, then it might be worth looking towards ready-made solutions.
Load Balancing
Random numbers are good on large samples, on small ones the distribution can be a little unexpected. Use a counter and divide by even/odd requests. When increasing the number of beats, read the remainder of dividing the current counter value by the number of beats. If you need load balancing, then such methods (including PRNG) are good for requests that are approximately equal in complexity. If requests can be of varying complexity, evaluate by the state of shares or by query parameters. Why you should not use PRNG, because in the limit you are wasting energy on generating random numbers, which, in fact, are not needed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question