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
overlord/devicestate: best effort to go to early full retries for registration on the like of DNS no host #4263
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f29e92f
best effort to go for full retry early if having permanet net error l…
pedronis 08369bf
limit number of retries starting from scratch (pre-poll) with request…
pedronis 985fcb2
feedback and drive-by: use .invalid for non existing hosts, see rfc2606
pedronis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
@@ -62,6 +63,7 @@ func deviceAPIBaseURL() string { | |
var ( | ||
keyLength = 4096 | ||
retryInterval = 60 * time.Second | ||
maxTentatives = 15 | ||
deviceAPIBase = deviceAPIBaseURL() | ||
requestIDURL = deviceAPIBase + "request-id" | ||
serialRequestURL = deviceAPIBase + "devices" | ||
|
@@ -113,9 +115,12 @@ type requestIDResp struct { | |
RequestID string `json:"request-id"` | ||
} | ||
|
||
func retryErr(t *state.Task, reason string, a ...interface{}) error { | ||
func retryErr(t *state.Task, nTentatives int, reason string, a ...interface{}) error { | ||
t.State().Lock() | ||
defer t.State().Unlock() | ||
if nTentatives >= maxTentatives { | ||
return fmt.Errorf(reason, a...) | ||
} | ||
t.Errorf(reason, a...) | ||
return &state.Retry{After: retryInterval} | ||
} | ||
|
@@ -125,10 +130,10 @@ type serverError struct { | |
Errors []*serverError `json:"error_list"` | ||
} | ||
|
||
func retryBadStatus(t *state.Task, reason string, resp *http.Response) error { | ||
func retryBadStatus(t *state.Task, nTentatives int, reason string, resp *http.Response) error { | ||
if resp.StatusCode > 500 { | ||
// likely temporary | ||
return retryErr(t, "%s: unexpected status %d", reason, resp.StatusCode) | ||
return retryErr(t, nTentatives, "%s: unexpected status %d", reason, resp.StatusCode) | ||
} | ||
if resp.Header.Get("Content-Type") == "application/json" { | ||
var srvErr serverError | ||
|
@@ -148,6 +153,16 @@ func retryBadStatus(t *state.Task, reason string, resp *http.Response) error { | |
} | ||
|
||
func prepareSerialRequest(t *state.Task, privKey asserts.PrivateKey, device *auth.DeviceState, client *http.Client, cfg *serialRequestConfig) (string, error) { | ||
// limit tentatives starting from scratch before going to | ||
// slower full retries | ||
var nTentatives int | ||
err := t.Get("pre-poll-tentatives", &nTentatives) | ||
if err != nil && err != state.ErrNoState { | ||
return "", err | ||
} | ||
nTentatives++ | ||
t.Set("pre-poll-tentatives", nTentatives) | ||
|
||
st := t.State() | ||
st.Unlock() | ||
defer st.Lock() | ||
|
@@ -161,18 +176,23 @@ func prepareSerialRequest(t *state.Task, privKey asserts.PrivateKey, device *aut | |
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return "", retryErr(t, "cannot retrieve request-id for making a request for a serial: %v", err) | ||
if netErr, ok := err.(net.Error); ok && !netErr.Temporary() { | ||
// a non temporary net error, like a DNS no | ||
// host, error out and do full retries | ||
return "", fmt.Errorf("cannot retrieve request-id for making a request for a serial: %v", err) | ||
} | ||
return "", retryErr(t, nTentatives, "cannot retrieve request-id for making a request for a serial: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != 200 { | ||
return "", retryBadStatus(t, "cannot retrieve request-id for making a request for a serial", resp) | ||
return "", retryBadStatus(t, nTentatives, "cannot retrieve request-id for making a request for a serial", resp) | ||
} | ||
|
||
dec := json.NewDecoder(resp.Body) | ||
var requestID requestIDResp | ||
err = dec.Decode(&requestID) | ||
if err != nil { // assume broken i/o | ||
return "", retryErr(t, "cannot read response with request-id for making a request for a serial: %v", err) | ||
return "", retryErr(t, nTentatives, "cannot read response with request-id for making a request for a serial: %v", err) | ||
} | ||
|
||
encodedPubKey, err := asserts.EncodePublicKey(privKey.PublicKey()) | ||
|
@@ -216,7 +236,7 @@ func submitSerialRequest(t *state.Task, serialRequest string, client *http.Clien | |
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, retryErr(t, "cannot deliver device serial request: %v", err) | ||
return nil, retryErr(t, 0, "cannot deliver device serial request: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding this little helper?
|
||
} | ||
defer resp.Body.Close() | ||
|
||
|
@@ -225,7 +245,7 @@ func submitSerialRequest(t *state.Task, serialRequest string, client *http.Clien | |
case 202: | ||
return nil, errPoll | ||
default: | ||
return nil, retryBadStatus(t, "cannot deliver device serial request", resp) | ||
return nil, retryBadStatus(t, 0, "cannot deliver device serial request", resp) | ||
} | ||
|
||
// TODO: support a stream of assertions instead of just the serial | ||
|
@@ -234,7 +254,7 @@ func submitSerialRequest(t *state.Task, serialRequest string, client *http.Clien | |
dec := asserts.NewDecoder(resp.Body) | ||
got, err := dec.Decode() | ||
if err != nil { // assume broken i/o | ||
return nil, retryErr(t, "cannot read response to request for a serial: %v", err) | ||
return nil, retryErr(t, 0, "cannot read response to request for a serial: %v", err) | ||
} | ||
|
||
serial, ok := got.(*asserts.Serial) | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much nicer than
foo
that we use randomly in other places. Thank you!