EnqueueRequest puts the request into the queue. MaxQueries is user-specific value that specifies how many queriers can this user use (zero or negative = all queriers). It is passed to each EnqueueRequest, because it can change between calls. If request is successfully enqueued, successFn is called
(userID string, req Request, maxQueriers float64, successFn func())
| 89 | // |
| 90 | // If request is successfully enqueued, successFn is called with the lock held, before any querier can receive the request. |
| 91 | func (q *RequestQueue) EnqueueRequest(userID string, req Request, maxQueriers float64, successFn func()) error { |
| 92 | q.mtx.Lock() |
| 93 | defer q.mtx.Unlock() |
| 94 | |
| 95 | if q.stopped { |
| 96 | return ErrStopped |
| 97 | } |
| 98 | |
| 99 | maxQuerierCount := util.DynamicShardSize(maxQueriers, len(q.queues.queriers)) |
| 100 | queue := q.queues.getOrAddQueue(userID, maxQuerierCount) |
| 101 | maxOutstandingRequests := q.queues.limits.MaxOutstandingPerTenant(userID) |
| 102 | priority := strconv.FormatInt(req.Priority(), 10) |
| 103 | |
| 104 | if queue == nil { |
| 105 | // This can only happen if userID is "". |
| 106 | return errors.New("no queue found") |
| 107 | } |
| 108 | |
| 109 | q.totalRequests.WithLabelValues(userID, priority).Inc() |
| 110 | |
| 111 | if queue.length() >= maxOutstandingRequests { |
| 112 | q.discardedRequests.WithLabelValues(userID, priority).Inc() |
| 113 | return ErrTooManyRequests |
| 114 | } |
| 115 | |
| 116 | queue.enqueueRequest(req) |
| 117 | q.cond.Broadcast() |
| 118 | // Call this function while holding a lock. This guarantees that no querier can fetch the request before function returns. |
| 119 | if successFn != nil { |
| 120 | successFn() |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | // GetNextRequestForQuerier find next user queue and takes the next request off of it. Will block if there are no requests. |
| 126 | // By passing user index from previous call of this method, querier guarantees that it iterates over all users fairly. |