tryReacquireForRequest checks whether the given request already has an associated checker. If so, it either returns the checker directly (still held) or reacquires it by claiming a semaphore slot. The caller must provide the appropriate semaphore channel and indicate whether this is a diagnostics re
(requestID string, sem chan<- struct{}, isDiag bool)
| 155 | // normal acquisition — in this case, a semaphore slot has already been claimed. |
| 156 | // Must NOT be called with p.mu held. |
| 157 | func (p *checkerPool) tryReacquireForRequest(requestID string, sem chan<- struct{}, isDiag bool) (*checker.Checker, func(), bool) { |
| 158 | if requestID == "" { |
| 159 | sem <- struct{}{} |
| 160 | return nil, nil, false |
| 161 | } |
| 162 | |
| 163 | p.mu.Lock() |
| 164 | index, ok := p.requestAssociations[requestID] |
| 165 | if !ok { |
| 166 | p.mu.Unlock() |
| 167 | sem <- struct{}{} |
| 168 | return nil, nil, false |
| 169 | } |
| 170 | |
| 171 | // Validate that the associated index matches the expected category. |
| 172 | // Index 0 is for diagnostics; indices 1+ are for queries. |
| 173 | if (isDiag && index != 0) || (!isDiag && index == 0) { |
| 174 | delete(p.requestAssociations, requestID) |
| 175 | p.mu.Unlock() |
| 176 | sem <- struct{}{} |
| 177 | return nil, nil, false |
| 178 | } |
| 179 | |
| 180 | c := p.checkers[index] |
| 181 | if c == nil { |
| 182 | delete(p.requestAssociations, requestID) |
| 183 | p.mu.Unlock() |
| 184 | sem <- struct{}{} |
| 185 | return nil, nil, false |
| 186 | } |
| 187 | |
| 188 | held := p.heldBy[index] |
| 189 | if held == requestID { |
| 190 | // Same request, checker still held — return without claiming a slot. |
| 191 | p.mu.Unlock() |
| 192 | return c, noop, true |
| 193 | } |
| 194 | |
| 195 | if held == "" { |
| 196 | // Same request reacquiring after release — need a semaphore slot. |
| 197 | p.mu.Unlock() |
| 198 | sem <- struct{}{} |
| 199 | p.mu.Lock() |
| 200 | // Re-check: checker may have been disposed while waiting for the slot. |
| 201 | if cc := p.checkers[index]; cc == c && p.heldBy[index] == "" { |
| 202 | p.heldBy[index] = requestID |
| 203 | p.mu.Unlock() |
| 204 | return c, p.createRelease(requestID, index, c), true |
| 205 | } |
| 206 | p.mu.Unlock() |
| 207 | // Checker was replaced/disposed while waiting for the slot. |
| 208 | // The slot is still claimed; the caller will use it for normal acquisition. |
| 209 | return nil, nil, false |
| 210 | } |
| 211 | |
| 212 | // Checker held by another request — claim a slot normally. |
| 213 | p.mu.Unlock() |
| 214 | sem <- struct{}{} |
no test coverage detected