Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RabbitMQ] Rendre la connexion unique #167

Open
sebprt opened this issue Dec 18, 2024 · 0 comments
Open

[RabbitMQ] Rendre la connexion unique #167

sebprt opened this issue Dec 18, 2024 · 0 comments

Comments

@sebprt
Copy link
Contributor

sebprt commented Dec 18, 2024

Nous avons constaté un problème concernant les rejets. Après plusieurs investigations, on est tombé sur la posisbilité que ce soit le fait que trop de connexion à RabbitMQ soient ouvertes.

image (4)
image (3)

En fait, dans chaque step, nous avons la méthode withAuthentication de l'objet Kiboko\Component\Flow\RabbitMQ\Rejection qui est appelé. Sauf que cette méthode instancie un nouveau client à chaque fois, et appelle la méthode connect a chaque fois.

Kiboko\Component\Flow\RabbitMQ\Rejection::withAuthentication(...)
public static function withAuthentication(
    string $stepUuid,
    string $host,
    string $vhost,
    string $topic,
     ?string $user,
     ?string $password,
     ?string $exchange = null,
     ?int $port = null,
 ): self {
    $connection = new Client([
        'host' => $host,
        'port' => $port,
        'vhost' => $vhost,
        'user' => $user,
        'password' => $password,
    ]);
    $connection->connect();

    return new self($connection, stepUuid: $stepUuid, topic: $topic, exchange: $exchange);
}

Solution :

Dans le Package https://github.com/php-etl/rabbitmq-flow, il faut ajouter une classe qui va gérer les instances du client.

class SharedAuthenticationMiddleware
{
    private static ?\Bunny\Client $instance = null;

    public static function getInstance(
        string $host,
        string $vhost,
        ?string $user,
        ?string $password,
        ?int $port = null,
    ): \Bunny\Client {
        if (null === self::$instance) {
            self::$instance = new \Bunny\Client([
                'host' => $host,
                'port' => $port,
                'vhost' => $vhost,
                'user' => $user,
                'password' => $password,
            ]);

            self::$instance->connect();
        }

        return self::$instance;
    }
}

Dans https://github.com/php-etl/satellite, il faut modifier le code généré pour avoir quelque chose du genre

new \Kiboko\Component\Flow\RabbitMQ\Rejection(\Kiboko\Component\Flow\RabbitMQ\SharedAuthenticationMiddleware::getInstance(host: getenv("RABBITMQ_HOST"), vhost: getenv("RABBITMQ_VHOST"), user: getenv("RABBITMQ_USER"), password: getenv("RABBITMQ_PASSWORD"), port: getenv("RABBITMQ_PORT")), stepUuid: '6760085278df6', topic: 'contacts_1954_eshop')

Et, il faudrait avoir une option shared pour pouvoir utiliser l'ancienne méthode ou la nouvelle facilement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant