Skip to content

Commit

Permalink
Improve NGINX start
Browse files Browse the repository at this point in the history
For NGINX and NGINX Plus:
- After the IC starts NGINX, check that NGINX is running by
querying the config version via the unix socket -- the same check
that is performed after each configuration reload.
- Modify the check to always sleep before the first check.

For NGINX Plus:
- Remove no longer required 0.5s timeout during the IC start.
  • Loading branch information
pleshakov committed Oct 25, 2018
1 parent 372abc4 commit 5e50496
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
1 change: 0 additions & 1 deletion cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func main() {

var nginxAPI *plus.NginxAPIController
if *nginxPlus {
time.Sleep(500 * time.Millisecond)
httpClient := getSocketClient()
nginxAPI, err = plus.NewNginxAPIController(&httpClient, "http://nginx-plus-api/api", local)
if err != nil {
Expand Down
32 changes: 20 additions & 12 deletions internal/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,26 @@ func (nginx *Controller) Reload() error {

// Start starts NGINX
func (nginx *Controller) Start(done chan error) {
if !nginx.local {
cmd := exec.Command("nginx")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
glog.Fatalf("Failed to start nginx: %v", err)
}
go func() {
done <- cmd.Wait()
}()
} else {
glog.V(3).Info("Starting nginx")
glog.V(3).Info("Starting nginx")

if nginx.local {
return
}

cmd := exec.Command("nginx")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
glog.Fatalf("Failed to start nginx: %v", err)
}

go func() {
done <- cmd.Wait()
}()

err := nginx.verifyClient.WaitForCorrectVersion(nginx.configVersion)
if err != nil {
glog.Fatalf("Could not get newest config version: %v", err)
}
}

Expand Down
8 changes: 5 additions & 3 deletions internal/nginx/verify/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ func (c *Client) WaitForCorrectVersion(expectedVersion int) error {
// This value needs tuning.
maxRetries := 160
sleep := 25 * time.Millisecond
for i := 0; i < maxRetries; i++ {
for i := 1; i <= maxRetries; i++ {
time.Sleep(sleep)

version, err := c.GetConfigVersion()
if err != nil {
return fmt.Errorf("unable to fetch version: %v", err)
glog.V(3).Infof("Unable to fetch version: %v", err)
continue
}
if version == expectedVersion {
glog.V(3).Infof("success, version %v ensured. iterations: %v. took: %v", expectedVersion, i, time.Duration(i)*sleep)
return nil
}
time.Sleep(sleep)
}
return fmt.Errorf("could not get expected version: %v", expectedVersion)
}

0 comments on commit 5e50496

Please sign in to comment.