Skip to content

Commit

Permalink
support timeout setting
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Feb 24, 2017
1 parent 0e6d3f6 commit 1694863
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 21 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ varnish-generator -c ./examples/config.json -t ./examples/default.vcl

- `version` The vcl version , default is `new Date().toISOString()`

- `timeout` The timeout setting for all directors

- `connect` The connect timeout, default is `2`

- `firstByte` The firstByte timeout, default is `5`

- `betweenBytes` The betweenBytes timeout, default is `2`

- `directors` Director list, Array

- `name` The director's name
Expand All @@ -37,6 +45,13 @@ varnish-generator -c ./examples/config.json -t ./examples/default.vcl

- `type` The algorithm of load balance, it can be 'fallback', 'hash', 'random', 'round_robin'. The default is 'round_robin'

- `timeout` The director timeout setting, if not set , it will be use the global timeout setting
- `connect` The connect timeout

- `firstByte` The firstByte timeout

- `betweenBytes` The betweenBytes timeout

- `backends` The backend list, Array

- `ip` The ip of backend
Expand Down
10 changes: 10 additions & 0 deletions examples/varnish4/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
"name": "varnish-test",
"stale": 2,
"varnish": "4",
"timeout": {
"connect": 1,
"firstByte": 2,
"betweenBytes": 2
},
"directors": [
{
"name": "timtam",
"prefix": "/timtam",
"type": "fallback",
"timeout": {
"connect": 1,
"firstByte": 1,
"betweenBytes": 1
},
"backends": [
{
"ip": "127.0.0.1",
Expand Down
8 changes: 8 additions & 0 deletions examples/varnish4/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
name: varnish-test
stale: 2
varnish: "4"
timeout:
connect: 1
firstByte: 2
betweenBytes: 2
directors:
-
name: timtam
prefix: /timtam
type: fallback
timeout:
connect: 1
firstByte: 1
betweenBytes: 1
backends:
-
ip: 127.0.0.1
Expand Down
34 changes: 17 additions & 17 deletions examples/varnish4/default.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import directors;
backend dcharts0 {
.host = "127.0.0.1";
.port = "3020";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.connect_timeout = 1s;
.first_byte_timeout = 2s;
.between_bytes_timeout = 2s;
.probe = {
.url = "/ping";
Expand All @@ -22,8 +22,8 @@ backend dcharts0 {
backend dcharts1 {
.host = "127.0.0.1";
.port = "3030";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.connect_timeout = 1s;
.first_byte_timeout = 2s;
.between_bytes_timeout = 2s;
.probe = {
.url = "/ping";
Expand All @@ -37,8 +37,8 @@ backend dcharts1 {
backend vicanso0 {
.host = "127.0.0.1";
.port = "3040";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.connect_timeout = 1s;
.first_byte_timeout = 2s;
.between_bytes_timeout = 2s;
.probe = {
.url = "/ping";
Expand All @@ -52,8 +52,8 @@ backend vicanso0 {
backend vicanso1 {
.host = "127.0.0.1";
.port = "3050";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.connect_timeout = 1s;
.first_byte_timeout = 2s;
.between_bytes_timeout = 2s;
.probe = {
.url = "/ping";
Expand All @@ -67,9 +67,9 @@ backend vicanso1 {
backend timtam0 {
.host = "127.0.0.1";
.port = "3000";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
.connect_timeout = 1s;
.first_byte_timeout = 1s;
.between_bytes_timeout = 1s;
.probe = {
.url = "/ping";
.interval = 3s;
Expand All @@ -82,9 +82,9 @@ backend timtam0 {
backend timtam1 {
.host = "127.0.0.1";
.port = "3010";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
.connect_timeout = 1s;
.first_byte_timeout = 1s;
.between_bytes_timeout = 1s;
.probe = {
.url = "/ping";
.interval = 3s;
Expand All @@ -97,8 +97,8 @@ backend timtam1 {
backend aslant0 {
.host = "127.0.0.1";
.port = "8000";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.connect_timeout = 1s;
.first_byte_timeout = 2s;
.between_bytes_timeout = 2s;
.probe = {
.url = "/ping";
Expand Down Expand Up @@ -318,7 +318,7 @@ sub vcl_synth {
if(resp.status == 701){
synthetic("pong");
} elsif(resp.status == 702){
synthetic("2017-02-23T09:38:18.059Z");
synthetic("2017-02-24T06:33:52.995Z");
}
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
set resp.status = 200;
Expand Down
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const path = require('path');
const fs = require('fs');
const yaml = require('js-yaml');

const defaultTimeout = {
connect: 2,
firstByte: 5,
betweenBytes: 2,
};

/**
* [readFilePromise description]
* @param {[type]} file [description]
Expand Down Expand Up @@ -60,7 +66,9 @@ function getBackendConfig(directors) {
const arr = [];
_.forEach(sortedDirectors, (director) => {
_.forEach(director.backends, (backend, i) => {
const tmp = _.extend({}, backend);
const tmp = _.extend({
timeout: _.extend({}, defaultTimeout, director.timeout),
}, backend);
tmp.name = `${_.camelCase(director.name)}${i}`;
try {
arr.push(template(tmp));
Expand Down Expand Up @@ -186,6 +194,7 @@ function getConfig(directors) {
function getVcl(conf) {
const config = _.extend({
version: new Date().toISOString(),
timeout: defaultTimeout,
}, conf);
/* istanbul ignore if */
if (!config.directors || !config.name) {
Expand All @@ -195,6 +204,10 @@ function getVcl(conf) {
throw new Error('directors can not be empty');
}
_.forEach(config.directors, (item) => {
if (!item.timeout) {
/* eslint no-param-reassign:0 */
item.timeout = config.timeout;
}
if (item.type === 'shard') {
throw new Error('shard director is not support');
}
Expand Down
6 changes: 3 additions & 3 deletions template/backend.tpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
backend <%= name %> {
.host = "<%= ip %>";
.port = "<%= port %>";
.connect_timeout = 2s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
.connect_timeout = <%= timeout.connect %>s;
.first_byte_timeout = <%= timeout.firstByte %>s;
.between_bytes_timeout = <%= timeout.betweenBytes %>s;
.probe = {
.url = "/ping";
.interval = 3s;
Expand Down

0 comments on commit 1694863

Please sign in to comment.