Merge pull request #645 from jangie/add-backend-features-to-consul-catalog

enable consul catalog to use maxconn
This commit is contained in:
Emile Vauge 2016-09-15 18:46:30 +02:00 committed by GitHub
commit b376da1829
4 changed files with 31 additions and 6 deletions

View file

@ -861,6 +861,8 @@ Additional settings can be defined using Consul Catalog tags:
- `traefik.backend.weight=10`: assign this weight to the container - `traefik.backend.weight=10`: assign this weight to the container
- `traefik.backend.circuitbreaker=NetworkErrorRatio() > 0.5` - `traefik.backend.circuitbreaker=NetworkErrorRatio() > 0.5`
- `traefik.backend.loadbalancer=drr`: override the default load balancing mode - `traefik.backend.loadbalancer=drr`: override the default load balancing mode
- `traefik.backend.maxconn.amount=10`: set a maximum number of connections to the backend. Must be used in conjunction with the below label to take effect.
- `traefik.backend.maxconn.extractorfunc=client.ip`: set the function to be used against the request to determine what to limit maximum connections to the backend by. Must be used in conjunction with the above label to take effect.
- `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}`). - `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}`).
- `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend. - `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend.
- `traefik.frontend.priority=10`: override default frontend priority - `traefik.frontend.priority=10`: override default frontend priority

View file

@ -215,6 +215,7 @@ func (provider *ConsulCatalog) buildConfig(catalog []catalogUpdate) *types.Confi
"getBackendAddress": provider.getBackendAddress, "getBackendAddress": provider.getBackendAddress,
"getAttribute": provider.getAttribute, "getAttribute": provider.getAttribute,
"getEntryPoints": provider.getEntryPoints, "getEntryPoints": provider.getEntryPoints,
"hasMaxconnAttributes": provider.hasMaxconnAttributes,
} }
allNodes := []*api.ServiceEntry{} allNodes := []*api.ServiceEntry{}
@ -249,6 +250,15 @@ func (provider *ConsulCatalog) buildConfig(catalog []catalogUpdate) *types.Confi
return configuration return configuration
} }
func (provider *ConsulCatalog) hasMaxconnAttributes(attributes []string) bool {
amount := provider.getAttribute("backend.maxconn.amount", attributes, "")
extractorfunc := provider.getAttribute("backend.maxconn.extractorfunc", attributes, "")
if amount != "" && extractorfunc != "" {
return true
}
return false
}
func (provider *ConsulCatalog) getNodes(index map[string][]string) ([]catalogUpdate, error) { func (provider *ConsulCatalog) getNodes(index map[string][]string) ([]catalogUpdate, error) {
visited := make(map[string]bool) visited := make(map[string]bool)

View file

@ -212,6 +212,8 @@ func TestConsulCatalogBuildConfig(t *testing.T) {
"traefik.backend.loadbalancer=drr", "traefik.backend.loadbalancer=drr",
"traefik.backend.circuitbreaker=NetworkErrorRatio() > 0.5", "traefik.backend.circuitbreaker=NetworkErrorRatio() > 0.5",
"random.foo=bar", "random.foo=bar",
"traefik.backend.maxconn.amount=1000",
"traefik.backend.maxconn.extractorfunc=client.ip",
}, },
}, },
Nodes: []*api.ServiceEntry{ Nodes: []*api.ServiceEntry{
@ -260,6 +262,10 @@ func TestConsulCatalogBuildConfig(t *testing.T) {
LoadBalancer: &types.LoadBalancer{ LoadBalancer: &types.LoadBalancer{
Method: "drr", Method: "drr",
}, },
MaxConn: &types.MaxConn{
Amount: 1000,
ExtractorFunc: "client.ip",
},
}, },
}, },
}, },

View file

@ -23,6 +23,13 @@
[backends."backend-{{$service}}".loadbalancer] [backends."backend-{{$service}}".loadbalancer]
method = "{{$loadBalancer}}" method = "{{$loadBalancer}}"
{{end}} {{end}}
{{if hasMaxconnAttributes .Attributes}}
[backends."backend-{{$service}}".maxconn]
amount = {{getAttribute "backend.maxconn.amount" .Attributes "" }}
extractorfunc = "{{getAttribute "backend.maxconn.extractorfunc" .Attributes "" }}"
{{end}}
{{end}} {{end}}
[frontends] [frontends]