Skip to content

Commit cf3f078

Browse files
David WATTIERslopukhov
authored andcommitted
* send multiple small purge request instead of one big request to avoid reaching the varnish http_req_hdr_len threshold.
1 parent de50d12 commit cf3f078

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

app/code/Magento/CacheInvalidate/Observer/InvalidateVarnishObserver.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ class InvalidateVarnishObserver implements ObserverInterface
2121
*/
2222
protected $purgeCache;
2323

24+
/**
25+
* Batch size of the purge request.
26+
*
27+
* Based on default Varnish 4 http_req_hdr_len size minus a 512 bytes margin for method,
28+
* header name, line feeds etc.
29+
*
30+
* @see http://www.varnish-cache.org/docs/4.0/reference/varnishd.html#http-req-hdr-len
31+
*
32+
* @var int
33+
*/
34+
private $requestSize = 7680;
35+
2436
/**
2537
* Invalidation tags resolver
2638
*
@@ -44,6 +56,8 @@ public function __construct(
4456
* If Varnish caching is enabled it collects array of tags
4557
* of incoming object and asks to clean cache.
4658
*
59+
* For scaling reason the purge request is chopped down to fix batch size.
60+
*
4761
* @param \Magento\Framework\Event\Observer $observer
4862
* @return void
4963
*/
@@ -56,10 +70,21 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5670
if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled()) {
5771
$bareTags = $this->getTagResolver()->getTags($object);
5872

73+
$tagsBatchSize = 0;
5974
$tags = [];
60-
$pattern = "((^|,)%s(,|$))";
75+
$pattern = '((^|,)%s(,|$))';
6176
foreach ($bareTags as $tag) {
62-
$tags[] = sprintf($pattern, $tag);
77+
$formattedTag = sprintf($pattern, $tag);
78+
79+
// Send request if batch size is reached and add the implode with pipe to the computation
80+
if ($tagsBatchSize + strlen($formattedTag) > $this->requestSize - count($tags) - 1) {
81+
$this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
82+
$tags = [];
83+
$tagsBatchSize = 0;
84+
}
85+
86+
$tagsBatchSize += strlen($formattedTag);
87+
$tags[] = $formattedTag;
6388
}
6489
if (!empty($tags)) {
6590
$this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));

0 commit comments

Comments
 (0)