Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PoolWaiter
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 wake
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 await
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SPDX-License-Identifier: EUPL-1.2
5 *
6 * This file is part of icap-flow.
7 *
8 * Licensed under the EUPL, Version 1.2 only (the "Licence");
9 * you may not use this work except in compliance with the Licence.
10 * You may obtain a copy of the Licence at:
11 *
12 *     https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the Licence is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 */
18
19declare(strict_types=1);
20
21namespace Ndrstmr\Icap\Transport;
22
23use Amp\DeferredFuture;
24use Amp\Socket\Socket;
25
26/**
27 * A call to {@see AmpConnectionPool::acquire()} that waits for room. The pool hands it what it was waiting for
28 * ({@see $grant}: an idle socket, already marked as in use, or the room to connect) and wakes it; the call
29 * itself also wakes when its wait runs out, when its caller cancels and when the pool is closed, and then finds
30 * out which of the four it was.
31 *
32 * The wait ends at {@see $deadline}, a point on the pool's monotonic clock, whether or not the timer that wakes
33 * the call has run: an event loop that something held up lets a deadline pass unnoticed, and what is granted
34 * after it is not for this call.
35 *
36 * @internal
37 */
38final class PoolWaiter
39{
40    public Socket|PoolSlot|null $grant = null;
41
42    /** @var DeferredFuture<null> */
43    private DeferredFuture $woken;
44
45    public function __construct(
46        public readonly string $endpoint,
47        public readonly string $bucket,
48        public readonly float $deadline,
49    ) {
50        $this->woken = new DeferredFuture();
51    }
52
53    /**
54     * Wakes the call, once: a second reason to wake it changes nothing.
55     */
56    public function wake(): void
57    {
58        if (!$this->woken->isComplete()) {
59            $this->woken->complete();
60        }
61    }
62
63    public function await(): void
64    {
65        $this->woken->getFuture()->await();
66    }
67}