Skip to content

Commit 9ca60d9

Browse files
committed
Merge pull request #126 from FriendsOfSymfony/no-duplicate-proxy-requests
Avoid duplicate requests to the cache proxy
2 parents 9c65814 + a2c253f commit 9ca60d9

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
"extra": {
4444
"branch-alias": {
45-
"dev-master": "1.0.x-dev"
45+
"dev-master": "1.1.x-dev"
4646
}
4747
}
4848
}

src/ProxyClient/AbstractProxyClient.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,26 @@ public function flush()
130130
*/
131131
protected function queueRequest($method, $url, array $headers = array())
132132
{
133-
$this->queue[] = $this->createRequest($method, $url, $headers);
133+
$signature = $this->getSignature($method, $url, $headers);
134+
if (!isset($this->queue[$signature])) {
135+
$this->queue[$signature] = $this->createRequest($method, $url, $headers);
136+
}
137+
}
138+
139+
/**
140+
* Calculate a unique hash for the request, based on all significant information.
141+
*
142+
* @param string $method HTTP method
143+
* @param string $url URL
144+
* @param array $headers HTTP headers
145+
*
146+
* @return string A hash value for this request.
147+
*/
148+
private function getSignature($method, $url, array $headers)
149+
{
150+
ksort($headers);
151+
152+
return md5($method . "\n" . $url . "\n" . var_export($headers, true));
134153
}
135154

136155
/**

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,41 @@ function ($requests) use ($self) {
368368
);
369369
}
370370

371+
public function testEliminateDuplicates()
372+
{
373+
$self = $this;
374+
$client = \Mockery::mock('\Guzzle\Http\Client[send]', array('', null))
375+
->shouldReceive('send')
376+
->once()
377+
->with(
378+
\Mockery::on(
379+
function ($requests) use ($self) {
380+
/** @type Request[] $requests */
381+
$self->assertCount(4, $requests);
382+
foreach ($requests as $request) {
383+
$self->assertEquals('PURGE', $request->getMethod());
384+
}
385+
386+
return true;
387+
}
388+
)
389+
)
390+
->getMock();
391+
392+
$varnish = new Varnish(array('127.0.0.1', '127.0.0.2'), 'fos.lo', $client);
393+
394+
$this->assertEquals(
395+
2,
396+
$varnish
397+
->purge('/c', array('a' => 'b', 'c' => 'd'))
398+
->purge('/c', array('c' => 'd', 'a' => 'b')) // same request (header order is not significant)
399+
->purge('/c') // different request as headers different
400+
->purge('/c')
401+
->flush()
402+
);
403+
}
404+
405+
371406
protected function setUp()
372407
{
373408
$this->mock = new MockPlugin();

0 commit comments

Comments
 (0)