-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Write config version directly in nginx config - Check config version by querying over socket - Add config version check to API - Atomic write (write and rename) for version config
- Loading branch information
1 parent
92730bc
commit aa0f464
Showing
10 changed files
with
315 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,7 +96,7 @@ http { | |
} | ||
{{- end }} | ||
|
||
|
||
include /etc/nginx/config-version.conf; | ||
include /etc/nginx/conf.d/*.conf; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package verify | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
// Client is a client for verifying the config version. | ||
type Client struct { | ||
client *http.Client | ||
} | ||
|
||
// NewClient returns a new client pointed at the config version socket. | ||
func NewClient() *Client { | ||
return &Client{ | ||
client: &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", "/var/run/nginx-config-version.sock") | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// GetConfigVersion get version number that we put in the nginx config to verify that we're using | ||
// the correct config. | ||
func (c *Client) GetConfigVersion() (int, error) { | ||
resp, err := c.client.Get("http://config-version/configVersion") | ||
if err != nil { | ||
return 0, fmt.Errorf("error getting client: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return 0, fmt.Errorf("non-200 response: %v", resp.StatusCode) | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to read the response body: %v", err) | ||
} | ||
v, err := strconv.Atoi(string(body)) | ||
if err != nil { | ||
return 0, fmt.Errorf("error converting string to int: %v", err) | ||
} | ||
return v, nil | ||
} | ||
|
||
// WaitForCorrectVersion calls the config version endpoint until it gets the expectedVersion, | ||
// which ensures that a new worker process has been started for that config version. | ||
func (c *Client) WaitForCorrectVersion(expectedVersion int) error { | ||
// This value needs tuning. | ||
maxRetries := 160 | ||
sleep := 25 * time.Millisecond | ||
for i := 0; i < maxRetries; i++ { | ||
version, err := c.GetConfigVersion() | ||
if err != nil { | ||
return fmt.Errorf("unable to fetch version: %v", err) | ||
} | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package verify | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
type Transport struct { | ||
} | ||
|
||
func (c Transport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: ioutil.NopCloser(bytes.NewBufferString("42")), | ||
Header: make(http.Header), | ||
}, nil | ||
} | ||
|
||
func getTestHTTPClient() *http.Client { | ||
ts := Transport{} | ||
tClient := &http.Client{ | ||
Transport: ts, | ||
} | ||
return tClient | ||
} | ||
|
||
func TestVerifyClient(t *testing.T) { | ||
|
||
c := Client{ | ||
client: getTestHTTPClient(), | ||
} | ||
|
||
configVersion, err := c.GetConfigVersion() | ||
if err != nil { | ||
t.Errorf("error getting config version: %v", err) | ||
} | ||
if configVersion != 42 { | ||
t.Errorf("got bad config version, expected 42 got %v", configVersion) | ||
} | ||
|
||
err = c.WaitForCorrectVersion(43) | ||
if err == nil { | ||
t.Error("expected error from WaitForCorrectVersion ") | ||
} | ||
err = c.WaitForCorrectVersion(42) | ||
if err != nil { | ||
t.Errorf("error waiting for config version: %v", err) | ||
} | ||
} |
Oops, something went wrong.