Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Adapt nginx has sizes to the number of ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Aug 22, 2016
1 parent 8a93116 commit e69033c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion ingress/controllers/nginx/nginx/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ func (ngx *Manager) loadTemplate() error {
}

func (ngx *Manager) writeCfg(cfg config.Configuration, ingressCfg IngressConfig) (bool, error) {
var longestName int
var serverNames int
for _, srv := range ingressCfg.Servers {
serverNames += len([]byte(srv.Name))
if longestName < len(srv.Name) {
longestName = len(srv.Name)
}
}

nameHashBucketSize := nextPowerOf2(longestName)
if nameHashBucketSize > cfg.ServerNameHashBucketSize {
glog.V(3).Infof("adjusting ServerNameHashBucketSize variable from %v to %v", cfg.ServerNameHashBucketSize, nameHashBucketSize)
cfg.ServerNameHashBucketSize = nameHashBucketSize
}

serverNameHashMaxSize := nextPowerOf2(serverNames)
if serverNameHashMaxSize > cfg.ServerNameHashMaxSize {
glog.V(3).Infof("adjusting ServerNameHashMaxSize variable from %v to %v", cfg.ServerNameHashMaxSize, serverNameHashMaxSize)
cfg.ServerNameHashMaxSize = serverNameHashMaxSize
}

conf := make(map[string]interface{})
conf["backlogSize"] = sysctlSomaxconn()
conf["upstreams"] = ingressCfg.Upstreams
Expand All @@ -78,7 +99,7 @@ func (ngx *Manager) writeCfg(cfg config.Configuration, ingressCfg IngressConfig)
if glog.V(3) {
b, err := json.Marshal(conf)
if err != nil {
glog.Errorf("unexpected error:", err)
glog.Errorf("unexpected error: %v", err)
}
glog.Infof("NGINX configuration: %v", string(b))
}
Expand Down Expand Up @@ -249,3 +270,17 @@ func buildRateLimit(input interface{}) []string {

return limits
}

// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
// https://play.golang.org/p/TVSyCcdxUh
func nextPowerOf2(v int) int {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++

return v
}

0 comments on commit e69033c

Please sign in to comment.