Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit a583522

Browse files
remove redundacy code
1 parent 63360a0 commit a583522

File tree

7 files changed

+127
-117
lines changed

7 files changed

+127
-117
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
FROM php:7.3-cli
1+
FROM php:7.4-cli
22

33
RUN apt-get update -qq \
4-
&& apt-get install -qq --no-install-recommends \
5-
git \
6-
zip \
7-
unzip \
4+
&& apt-get install -qq --no-install-recommends git zip unzip \
85
&& apt-get clean
96

107
RUN apt-get install -y libz-dev libmemcached-dev && \
118
pecl install memcached && \
129
docker-php-ext-enable memcached
1310

14-
# Install Composer
1511
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
1612

17-
ARG UID=1000
18-
ARG GID=1000
19-
20-
RUN groupmod -g ${GID} www-data \
21-
&& usermod -u ${UID} -g www-data www-data \
22-
&& chown -hR www-data:www-data \
23-
/var/www \
24-
/usr/local/src
13+
RUN groupmod -g 1000 www-data \
14+
&& usermod -u 1000 -g www-data www-data \
15+
&& chown -hR www-data:www-data /var/www /usr/local/src
2516

2617
USER www-data:www-data
2718
WORKDIR /usr/local/src/app
19+
2820
ENV PATH=$PATH:/var/www/.composer/vendor/bin
21+
22+
CMD ["php", "./bin/circuit-breaker"]

bin/circuit-breaker

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
use Ackintosh\Ganesha\Builder;
6+
use Ackintosh\Ganesha\Exception\RejectedException;
7+
use Ackintosh\Ganesha\GuzzleMiddleware;
8+
use Ackintosh\Ganesha\Storage\Adapter\Memcached as Adapter;
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\HandlerStack;
12+
use GuzzleHttp\TransferStats;
13+
14+
require __DIR__ . '/../vendor/autoload.php';
15+
16+
$memcached = new Memcached();
17+
$memcached->addServer("memcached", 11211);
18+
$adapter = new Adapter($memcached);
19+
20+
$ganesha = Builder::withCountStrategy()
21+
->adapter(new Ackintosh\Ganesha\Storage\Adapter\Memcached($memcached))
22+
->failureCountThreshold(1)
23+
->intervalToHalfOpen(5)
24+
->build();
25+
26+
$handlers = HandlerStack::create();
27+
$handlers->push(new GuzzleMiddleware($ganesha));
28+
29+
$client = new Client([
30+
'base_uri' => 'http://demo4446522.mockable.i',
31+
'handler' => $handlers,
32+
]);
33+
34+
$table = new LucidFrame\Console\ConsoleTable();
35+
$table->setHeaders(['Tentativa', 'Transfer Time', 'Status']);
36+
37+
for ($i = 1; $i <= 15; $i++) {
38+
$table->addRow();
39+
$table->addColumn($i);
40+
41+
try {
42+
$response = $client->get('/bar', [
43+
'on_stats' => static function (TransferStats $stats) use ($table): void {
44+
$table->addColumn($stats->getTransferTime() ?? 0);
45+
}
46+
]);
47+
} catch (RequestException $guzzleException) {
48+
$table->addColumn('Hit');
49+
}catch (RejectedException $rejectedException) {
50+
$table->addColumn(0);
51+
$table->addColumn('Rejected');
52+
} finally {
53+
sleep(1);
54+
}
55+
}
56+
57+
$table->display();
58+

composer.json

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
{
2-
"name": "root/html",
2+
"name": "gabrielemidiomoreira/php-circuit-breaker",
3+
"description": "This project is a proof of concept with circuit breaker pattern.",
34
"type": "project",
4-
"require-dev": {
5-
"phpunit/phpunit": "^8.3"
6-
},
5+
"license": "MIT",
76
"authors": [
87
{
98
"name": "Gabriel Emidio Moreira",
109
"email": "[email protected]"
1110
}
1211
],
1312
"require": {
13+
"php": ">=7.4.0",
14+
"ext-memcached": "*",
15+
"guzzlehttp/guzzle": "6.4",
1416
"ackintosh/ganesha": "^1.0",
15-
"guzzlehttp/guzzle": "6.4"
17+
"phplucidframe/console-table": "^1.2"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^8.3"
1621
}
1722
}

composer.lock

+50-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
version: "3.7"
2-
32
services:
4-
53
php:
6-
build: docker/php
4+
build: .
75
depends_on:
86
- memcached
97
volumes:

index.php

-68
This file was deleted.

object.php

-24
This file was deleted.

0 commit comments

Comments
 (0)