Repository navigation
http: attach a caller context to async client requests - #49113
Conversation
Add send() and startRequest() overloads taking the callbacks as a std::unique_ptr. The request owns them and destroys them in its destructor, after their last call and also when the request is cancelled. If no request is returned, the callbacks were already called inline and destroyed; a request that cannot be created reports onFailure(Reset) first, since the caller keeps nothing to report it through. Use them for the mirror policy stats callbacks, which deleted themselves from onBeforeFinalizeUpstreamSpan() and relied on it being called exactly once per request. Signed-off-by: William Dauchy <[email protected]>
|
/retest |
wbpcode
left a comment
There was a problem hiding this comment.
I have another Idea. That's say, for a pure C async callback, we usually set a void* user data. Then, at the callback, we could cast the user data back to explicit context.
I think we could do similar things there? That's say, set a shared pointer UserData/Context into RequestOptions.
Then, expose a method to the Request to expose the user data like OptRef<Context> context();. By this way, we could get rid of the semantics change of the callback's lifetime.
WDYT?
Replace the send() and startRequest() overloads owning the callbacks with a context set in RequestOptions, as suggested in review: setContext() takes a shared_ptr to an AsyncClient::RequestContext, the request holds it until it is destroyed, and Request::context() returns it, so callbacks can get their per-request data back without changing their lifetime. The shadow writer uses its shared callbacks for all mirrors again. For a policy with stats it sets a ShadowRequestContext that records the outcome, and counts a cancel when it is destroyed without one. A nullptr return is counted as a failure unless the callbacks already ran inline. Signed-off-by: William Dauchy <[email protected]>
Signed-off-by: William Dauchy <[email protected]>
Signed-off-by: William Dauchy <[email protected]>
|
Thanks, I like that better: it keeps the callbacks' lifetime as is and avoids allocating callbacks per mirror. Reworked in 162b75a: |
|
/retest |
wbpcode
left a comment
There was a problem hiding this comment.
Thanks for the update. one last comment!
|
|
||
| /** | ||
| * @return the context set in the options of the request, if any. | ||
| */ | ||
| virtual OptRef<RequestContext> context() const PURE; |
There was a problem hiding this comment.
Add a default implementation.
| /** | |
| * @return the context set in the options of the request, if any. | |
| */ | |
| virtual OptRef<RequestContext> context() const PURE; | |
| /** | |
| * @return the context set in the options of the request, if any. | |
| */ | |
| virtual OptRef<RequestContext> context() const { return {}; } |
Although I guess there should no out-of-tree implementation for this class, but in case.
There was a problem hiding this comment.
Done in 800c3e0, thanks. I also dropped the context() override in the dynatrace test that is no longer needed.
Signed-off-by: William Dauchy <[email protected]>
|
/retest |
Commit Message: http: attach a caller context to async client requests
Add
AsyncClient::RequestContext, set withRequestOptions::setContext()as ashared_ptr. The request holds it until it is destroyed and returns it fromRequest::context(), so shared callbacks can get their per-request data back.Use it for the mirror policy stats, which allocated callbacks per mirror that deleted themselves from
onBeforeFinalizeUpstreamSpan().Additional Description:
Follow-up to #48470, resolving the TODO on
ShadowRequestCallbacks. As @botengyao pointed out, the self-deletion relied ononBeforeFinalizeUpstreamSpan()being called exactly once per request, so a change there could leak the object. The context approach was suggested by @wbpcode and keeps the callbacks' lifetime unchanged.The shadow writer now uses its shared callbacks for all mirrors. For a policy with
stat_prefix, it sets aShadowRequestContextthat records the outcome inonSuccess()/onFailure()and countsrq_cancelledwhen destroyed without one. Whensend()/startRequest()returnsnullptr, a failure is recorded unless the callbacks already ran inline. Policies withoutstat_prefixallocate nothing.Risk Level: Low. New optional field; existing callers are unchanged.
Testing: New
async_client_impl_testcases (context reachable from the callbacks and released with the request on success, reset, cancel, immediate and creation failure); updatedshadow_writer_impl_test;router_testandshadow_policy_integration_testpass.Docs Changes: N/A
Release Notes: N/A
Platform Specific Features: N/A