Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debounce to nginx reload #1

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 57 additions & 12 deletions nginx-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
//"math/rand"

"github.com/golang/glog"
"sync"
"time"
)

const dhparamFilename = "dhparam.pem"
Expand All @@ -21,6 +23,12 @@ type NginxController struct {
nginxCertsPath string
local bool
healthStatus bool

reloadLock sync.Mutex
reloading bool
reloadResult error
reloadDebounce chan struct{}
reloadDone chan struct{}
}

// IngressNginxConfig describes an NGINX configuration
Expand Down Expand Up @@ -231,24 +239,61 @@ func (nginx *NginxController) templateIt(config IngressNginxConfig, filename str
// Reload reloads NGINX
func (nginx *NginxController) Reload() error {
if !nginx.local {
if err := shellOut("nginx -t"); err != nil {
return fmt.Errorf("Invalid nginx configuration detected, not reloading: %s", err)
}

// Instead of using nginx -s reload, we use this approach: https://www.digitalocean.com/community/tutorials/how-to-upgrade-nginx-in-place-without-dropping-client-connections
//rand.Seed(time.Now().Unix())
//time.Sleep( time.Duration(rand.Intn(300)) * time.Millisecond )

if err := shellOut("nginx -s reload"); err != nil {
return fmt.Errorf("Reloading NGINX failed: %s", err)
}

return <- nginx._reload()
} else {
glog.V(3).Info("Reloading nginx")
}
return nil
}

func (nginx *NginxController) _reload() <-chan error {
nginx.reloadLock.Lock()
defer nginx.reloadLock.Unlock()

if nginx.reloading {
nginx.reloadDebounce <- struct{}{}
} else {
nginx.reloading = true
nginx.reloadDebounce = make(chan struct{})
nginx.reloadDone = make(chan struct{})
interval := 200 * time.Millisecond

go func() {
defer close(nginx.reloadDone)
defer nginx.reloadLock.Unlock()
for {
select {
case <-nginx.reloadDebounce:
case <-time.After(interval):
nginx.reloadResult = nil

if err := shellOut("nginx -t"); err != nil {
nginx.reloadResult = fmt.Errorf("Invalid nginx configuration detected, not reloading: %s", err)
}

// Instead of using nginx -s reload, we use this approach: https://www.digitalocean.com/community/tutorials/how-to-upgrade-nginx-in-place-without-dropping-client-connections
//rand.Seed(time.Now().Unix())
//time.Sleep( time.Duration(rand.Intn(300)) * time.Millisecond )
if err := shellOut("nginx -s reload"); err != nil {
nginx.reloadResult = fmt.Errorf("Reloading NGINX failed: %s", err)
}

nginx.reloadLock.Lock()
nginx.reloading = false
nginx.reloadDone <- struct{}{}
return
}
}
}()
}
result := make(chan error)
go func() {
<-nginx.reloadDone
result <- nginx.reloadResult
}()
return result
}

// Start starts NGINX
func (nginx *NginxController) Start() {
if !nginx.local {
Expand Down