Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
NullConnectionPool
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
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
 acquire
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 release
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 close
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\Cancellation;
24use Amp\Socket\Socket as SocketInterface;
25use Closure;
26use Ndrstmr\Icap\Config;
27
28/**
29 * No-op connection pool: every {@see acquire()} opens a fresh TCP/TLS
30 * connection, every {@see release()} closes it immediately. No idle
31 * sockets are ever kept.
32 *
33 * Use this when connection reuse is explicitly unwanted (e.g. testing,
34 * debugging, or ICAP servers that close the connection after every
35 * request anyway).
36 *
37 * It does not limit connections: with this pool, as without one, the
38 * application limits its own concurrency. {@see AmpConnectionPool} does.
39 *
40 * @see AmpConnectionPool for the keep-alive variant
41 */
42final class NullConnectionPool implements ConnectionPoolInterface
43{
44    /** @var Closure(Config, ?Cancellation): SocketInterface */
45    private Closure $connector;
46
47    /**
48     * @param (Closure(Config, ?Cancellation): SocketInterface)|null $connector optional override for testing
49     */
50    public function __construct(?Closure $connector = null)
51    {
52        $this->connector = $connector ?? SocketOpener::open(...);
53    }
54
55    #[\Override]
56    public function acquire(Config $config, ?Cancellation $cancellation = null): SocketInterface
57    {
58        return ($this->connector)($config, $cancellation);
59    }
60
61    #[\Override]
62    public function release(Config $config, SocketInterface $socket): void
63    {
64        $socket->close();
65    }
66
67    #[\Override]
68    public function close(): void
69    {
70        // Nothing to do — no idle sockets are ever stored.
71    }
72}