-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 the async probing function to the prober library. #3986
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,31 +20,98 @@ import ( | |
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/knative/serving/pkg/network" | ||
) | ||
|
||
// Do sends a single probe to given target, e.g. `http://revision.default.svc.cluster.local:81`. | ||
// headerValue is the value for the `k-network-probe` header. | ||
// Do returns the status code, response body, and the request error, if any. | ||
func Do(ctx context.Context, target, headerValue string) (int, string, error) { | ||
// Do returns whether the probe was successful or not, or there was an error probing. | ||
func Do(ctx context.Context, target, headerValue string) (bool, error) { | ||
req, err := http.NewRequest(http.MethodGet, target, nil) | ||
if err != nil { | ||
return 0, "", errors.Wrapf(err, "%s is not a valid URL", target) | ||
return false, errors.Wrapf(err, "%s is not a valid URL", target) | ||
} | ||
|
||
req.Header.Set(http.CanonicalHeaderKey(network.ProbeHeaderName), headerValue) | ||
req = req.WithContext(ctx) | ||
resp, err := network.AutoTransport.RoundTrip(req) | ||
if err != nil { | ||
return 0, "", err | ||
return false, errors.Wrapf(err, "error roundtripping %s", target) | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, "", err | ||
return false, errors.Wrap(err, "error reading body") | ||
} | ||
return resp.StatusCode, string(body), nil | ||
return resp.StatusCode == http.StatusOK && string(body) == headerValue, nil | ||
} | ||
|
||
// Done is a callback that is executed when the async probe has finished. | ||
// `arg` is given by the caller at the offering time, while `success` and `err` | ||
// are the return values of the `Do` call. | ||
// It is assumed that the opaque arg is consistent for a given target and | ||
// we will coalesce concurrent Offer invocations on target. | ||
type Done func(arg interface{}, success bool, err error) | ||
|
||
// Manager manages async probes and makes sure we run concurrently only a single | ||
// probe for the same key. | ||
type Manager struct { | ||
cb Done | ||
|
||
// mu guards keys. | ||
mu sync.Mutex | ||
keys sets.String | ||
} | ||
|
||
// New creates a new Manager, that will invoke the given callback when | ||
// async probing is finished. | ||
func New(cb Done) *Manager { | ||
return &Manager{ | ||
keys: sets.NewString(), | ||
cb: cb, | ||
} | ||
} | ||
|
||
// Offer executes asynchronous probe using `target` as the key. | ||
// If a probe with the same key already exists, Offer will return false and the | ||
// call is discarded. If the request is accepted, Offer returns true. | ||
// Otherwise Offer starts a goroutine that periodically executes | ||
// `Do`, until timeout is reached, the probe succeeds, or fails with an error. | ||
// In the end the callback is invoked with the provided `arg` and probing results. | ||
func (m *Manager) Offer(ctx context.Context, target, headerValue string, arg interface{}, period, timeout time.Duration) bool { | ||
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. Why "Offer"? 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. we offer system to make a probe, since the system can reject doing it -- it's not |
||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
if m.keys.Has(target) { | ||
return false | ||
} | ||
m.keys.Insert(target) | ||
m.doAsync(ctx, target, headerValue, arg, period, timeout) | ||
return true | ||
} | ||
|
||
// doAsync starts a go routine that probes the target with given period. | ||
func (m *Manager) doAsync(ctx context.Context, target, headerValue string, arg interface{}, period, timeout time.Duration) { | ||
go func() { | ||
defer func() { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.keys.Delete(target) | ||
}() | ||
var ( | ||
result bool | ||
err error | ||
) | ||
err = wait.PollImmediate(period, timeout, func() (bool, error) { | ||
result, err = Do(ctx, target, headerValue) | ||
return result, err | ||
}) | ||
m.cb(arg, result, err) | ||
}() | ||
vagababov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
Oops, something went wrong.
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.
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.
I've added to the
Done
definition.