@@ -21,6 +21,18 @@ class InvalidateVarnishObserver implements ObserverInterface
21
21
*/
22
22
protected $ purgeCache ;
23
23
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
+
24
36
/**
25
37
* Invalidation tags resolver
26
38
*
@@ -44,6 +56,8 @@ public function __construct(
44
56
* If Varnish caching is enabled it collects array of tags
45
57
* of incoming object and asks to clean cache.
46
58
*
59
+ * For scaling reason the purge request is chopped down to fix batch size.
60
+ *
47
61
* @param \Magento\Framework\Event\Observer $observer
48
62
* @return void
49
63
*/
@@ -56,10 +70,21 @@ public function execute(\Magento\Framework\Event\Observer $observer)
56
70
if ($ this ->config ->getType () == \Magento \PageCache \Model \Config::VARNISH && $ this ->config ->isEnabled ()) {
57
71
$ bareTags = $ this ->getTagResolver ()->getTags ($ object );
58
72
73
+ $ tagsBatchSize = 0 ;
59
74
$ tags = [];
60
- $ pattern = " ((^|,)%s(,|$)) " ;
75
+ $ pattern = ' ((^|,)%s(,|$)) ' ;
61
76
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 ;
63
88
}
64
89
if (!empty ($ tags )) {
65
90
$ this ->purgeCache ->sendPurgeRequest (implode ('| ' , array_unique ($ tags )));
0 commit comments