A
A
Alex2017-10-02 14:30:48
PHP
Alex, 2017-10-02 14:30:48

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 {
    // Вторая часть
  }

But perhaps there is a better way?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2017-10-02
@DmitriyEntelis

It is better to use a queue server such as rabbitmq to distribute the load.

S
Stalker_RED, 2017-10-02
@Stalker_RED

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

X
x67, 2017-10-02
@x67

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 question

Ask a Question

731 491 924 answers to any question