Skip to content

Commit 6b42512

Browse files
committed
support flag for varnish 4
1 parent 1b1e400 commit 6b42512

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

doc/varnish-configuration.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ Custom TTL
316316
.. include:: includes/custom-ttl.rst
317317

318318
Subroutines are provided in ``resources/config/varnish-[version]/fos_custom_ttl.vcl``.
319-
To enable support add the following to ``your_varnish.vcl``:
319+
The configuration needs to use inline C, which is disabled by default since
320+
Varnish 4.0. To use the custom TTL feature, you need to start your Varnish with
321+
inline C enabled: ``-p vcc_allow_inline_c=on``. Then add the following to
322+
``your_varnish.vcl``:
320323

321324
.. configuration-block::
322325

resources/config/varnish-3/fos_custom_ttl.vcl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
sub fos_custom_ttl_fetch {
1515
if (beresp.http.X-Reverse-Proxy-TTL) {
1616
/*
17-
* Note that there is a ``beresp.ttl`` field in VCL but unfortunately it can
18-
* only be set to absolute values and not dynamically. Thus we have to revert
19-
* to a C code fragment.
17+
* Note that there is a ``beresp.ttl`` field in VCL but unfortunately
18+
* it can only be set to absolute values and not dynamically. Thus we
19+
* have to resort to an inline C code fragment.
2020
*/
2121
C{
2222
char *ttl;

resources/config/varnish-4/fos_custom_ttl.vcl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
sub fos_custom_ttl_fetch {
1515
if (beresp.http.X-Reverse-Proxy-TTL) {
1616
/*
17-
* Note that there is a ``beresp.ttl`` field in VCL but unfortunately it can
18-
* only be set to absolute values and not dynamically. Thus we have to revert
19-
* to a C code fragment.
17+
* Note that there is a ``beresp.ttl`` field in VCL but unfortunately
18+
* it can only be set to absolute values and not dynamically. Thus we
19+
* have to resort to an inline C code fragment.
20+
*
21+
* As of Varnish 4.0, inline C is disabled by default. To use this
22+
* feature, you need to add `-p vcc_allow_inline_c=on` to your Varnish
23+
* startup command.
2024
*/
2125
C{
2226
char *ttl;

src/Test/Proxy/VarnishProxy.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class VarnishProxy extends AbstractProxy
2323
protected $configFile;
2424
protected $configDir;
2525
protected $cacheDir;
26+
protected $allowInlineC = false;
2627

2728
/**
2829
* Constructor
@@ -40,17 +41,21 @@ public function __construct($configFile)
4041
*/
4142
public function start()
4243
{
43-
$this->runCommand(
44-
$this->getBinary(),
45-
[
46-
'-a', $this->ip . ':' . $this->getPort(),
47-
'-T', $this->ip . ':' . $this->getManagementPort(),
48-
'-f', $this->getConfigFile(),
49-
'-n', $this->getCacheDir(),
50-
'-p', 'vcl_dir=' . $this->getConfigDir(),
51-
'-P', $this->pid,
52-
]
53-
);
44+
$args = [
45+
'-a', $this->ip . ':' . $this->getPort(),
46+
'-T', $this->ip . ':' . $this->getManagementPort(),
47+
'-f', $this->getConfigFile(),
48+
'-n', $this->getCacheDir(),
49+
'-p', 'vcl_dir=' . $this->getConfigDir(),
50+
51+
'-P', $this->pid,
52+
];
53+
if ($this->getAllowInlineC()) {
54+
$args[] = '-p';
55+
$args[] = 'vcc_allow_inline_c=on';
56+
}
57+
58+
$this->runCommand($this->getBinary(), $args);
5459

5560
$this->waitFor($this->ip, $this->getPort(), 2000);
5661
}
@@ -139,4 +144,24 @@ public function getCacheDir()
139144
{
140145
return $this->cacheDir;
141146
}
147+
148+
/**
149+
* Whether the inline C flag should be set.
150+
*
151+
* @return boolean
152+
*/
153+
public function getAllowInlineC()
154+
{
155+
return $this->allowInlineC;
156+
}
157+
158+
/**
159+
* Set whether the inline c flag should be on or off
160+
*
161+
* @param boolean $allowInlineC True for on, false for off
162+
*/
163+
public function setAllowInlineC($allowInlineC)
164+
{
165+
$this->allowInlineC = (boolean) $allowInlineC;
166+
}
142167
}

tests/Functional/Varnish/CustomTtlTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
*/
2020
class CustomTtlTest extends VarnishTestCase
2121
{
22+
public function setUp()
23+
{
24+
if ($this->getVarnishVersion() >= 4) {
25+
$this->getProxy()->setAllowInlineC(true);
26+
}
27+
parent::setUp();
28+
}
29+
2230
public function testCustomTtl()
2331
{
2432
$this->assertMiss($this->getResponse('/custom-ttl.php'));

0 commit comments

Comments
 (0)