GetNextRequestForQuerier find next user queue and takes the next request off of it. Will block if there are no requests. By passing user index from previous call of this method, querier guarantees that it iterates over all users fairly. If querier finds that request from the user is already expired,
(ctx context.Context, last UserIndex, querierID string)
| 126 | // By passing user index from previous call of this method, querier guarantees that it iterates over all users fairly. |
| 127 | // If querier finds that request from the user is already expired, it can get a request for the same user by using UserIndex.ReuseLastUser. |
| 128 | func (q *RequestQueue) GetNextRequestForQuerier(ctx context.Context, last UserIndex, querierID string) (Request, UserIndex, error) { |
| 129 | q.mtx.Lock() |
| 130 | defer q.mtx.Unlock() |
| 131 | |
| 132 | querierWait := false |
| 133 | |
| 134 | FindQueue: |
| 135 | // We need to wait if there are no users, or no pending requests for given querier. |
| 136 | for (q.queues.len() == 0 || querierWait) && ctx.Err() == nil && !q.stopped { |
| 137 | querierWait = false |
| 138 | q.cond.Wait() |
| 139 | } |
| 140 | |
| 141 | if q.stopped { |
| 142 | return nil, last, ErrStopped |
| 143 | } |
| 144 | |
| 145 | if err := ctx.Err(); err != nil { |
| 146 | return nil, last, err |
| 147 | } |
| 148 | |
| 149 | for { |
| 150 | queue, userID, idx := q.queues.getNextQueueForQuerier(last.last, querierID) |
| 151 | last.last = idx |
| 152 | if queue == nil { |
| 153 | break |
| 154 | } |
| 155 | |
| 156 | // Pick next request from the queue. |
| 157 | for { |
| 158 | minPriority, matchMinPriority := q.getPriorityForQuerier(userID, querierID) |
| 159 | request := queue.dequeueRequest(minPriority, matchMinPriority) |
| 160 | if request == nil { |
| 161 | // The queue does not contain request with the priority, wait for more requests |
| 162 | querierWait = true |
| 163 | goto FindQueue |
| 164 | } |
| 165 | |
| 166 | if queue.length() == 0 { |
| 167 | q.queues.deleteQueue(userID) |
| 168 | } |
| 169 | |
| 170 | // Tell close() we've processed a request. |
| 171 | q.cond.Broadcast() |
| 172 | |
| 173 | return request, last, nil |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // There are no unexpired requests, so we can get back |
| 178 | // and wait for more requests. |
| 179 | querierWait = true |
| 180 | goto FindQueue |
| 181 | } |
| 182 | |
| 183 | func (q *RequestQueue) getPriorityForQuerier(userID string, querierID string) (int64, bool) { |
| 184 | if priority, ok := q.queues.userQueues[userID].reservedQueriers[querierID]; ok { |