Skip to content
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

HTTPCLIENT-2343 - Enhance HttpClientContext user token management for improved compatibility #584

Closed
wants to merge 2 commits into from

Conversation

arturobernalg
Copy link
Member

This PR refines the way user tokens are managed within HttpClientContext. Specifically, the getUserToken() method has been enhanced to first check the internal userToken field, and only fall back to context attributes for backward compatibility. A warning log has been added to notify developers when this fallback occurs, strongly encouraging the preferred usage of setUserToken(Object) for setting user tokens.

…or improved compatibility.

Refined the `getUserToken()` method in `HttpClientContext` to prioritize retrieving the internal `userToken` field before falling back to context attributes. This adjustment ensures backward compatibility while encouraging the recommended approach for managing user tokens, laying the groundwork for a smoother transition as legacy methods are phased out.
Copy link
Member

@ok2c ok2c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg I am sorry but this needs to be done differently. Context attributes are mostly "written seldom / read often". Adding a map lookup to #getUserToken is wrong and adds unnecessary overhead. Override #setAttribute instead and update context instance variables along with the map.

@arturobernalg
Copy link
Member Author

setAttribute

Hi @ok2c
When ctxA.setAttribute(HttpClientContext.USER_TOKEN, "custom state") is called directly on an instance of HttpClientContext, it bypasses the overridden setAttribute method in the Delegate class and instead invokes HttpCoreContext#setAttribute. As a result, this does not synchronize with the internal userToken field as intended.

@ok2c
Copy link
Member

ok2c commented Oct 3, 2024

@arturobernalg So what? HttpClientContext.Delegate is being in extreme cases when caller uses incorrect type of context to start with.
Alternatively we should close HTTPCLIENT-2343 as WONTFIX.

@ok2c
Copy link
Member

ok2c commented Oct 3, 2024

@arturobernalg If both test cases pass, it would be good enough.

@SuppressWarnings("deprecation")
class TestHttpClientContext {

    @Test
    void testDeprecatedSetAttributes() {
        final HttpClientContext context1 = HttpClientContext.cast(new BasicHttpContext());
        context1.setAttribute(HttpClientContext.USER_TOKEN, "blah");
        Assertions.assertEquals("blah", context1.getUserToken());

        final HttpClientContext context2 = HttpClientContext.create();
        context2.setAttribute(HttpClientContext.USER_TOKEN, "blah");
        Assertions.assertEquals("blah", context2.getUserToken());
    }

}

…ute set through context attributes and its retrieval via getUserToken(), ensuring that the behavior is consistent across different ways of setting the user token.
@arturobernalg
Copy link
Member Author

SuppressWarnings("deprecation")
class TestHttpClientContext {

@Test
void testDeprecatedSetAttributes() {
    final HttpClientContext context1 = HttpClientContext.cast(new BasicHttpContext());
    context1.setAttribute(HttpClientContext.USER_TOKEN, "blah");
    Assertions.assertEquals("blah", context1.getUserToken());

    final HttpClientContext context2 = HttpClientContext.create();
    context2.setAttribute(HttpClientContext.USER_TOKEN, "blah");
    Assertions.assertEquals("blah", context2.getUserToken());
}

@ok2c both test pass correctly.

@ok2c
Copy link
Member

ok2c commented Oct 3, 2024

@ok2c both test pass correctly.

@arturobernalg Right, but you are still doing a map lookup in #getUserToken, don't you? The whole point of context optimization was to eliminate map lookups for frequently used attributes.

@arturobernalg
Copy link
Member Author

@ok2c both test pass correctly.

@arturobernalg Right, but you are still doing a map lookup in #getUserToken, don't you? The whole point of context optimization was to eliminate map lookups for frequently used attributes.

@ok2c The test failed, confirming that the current approach doesn't resolve the issue effectively.

@arturobernalg
Copy link
Member Author

@ok2c both test pass correctly.

@arturobernalg Right, but you are still doing a map lookup in #getUserToken, don't you? The whole point of context optimization was to eliminate map lookups for frequently used attributes.

@ok2c The test failed, confirming that the current approach doesn't resolve the issue effectively.

@ok2c Given the test results It might be best to consider closing the ticket as "WONTFIX" since the optimization goals aren't met with the current implementation.

@ok2c
Copy link
Member

ok2c commented Oct 3, 2024

@arturobernalg My idea was to move the compensatory code to #setAttribute, which should be less expensive in terms of performance overhead, and as long as the said two tests pass, it would be good enough. Having said that I am fine with WONTFIX as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants