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

Add context passing for filters #1977

Open
Mis1eader-dev opened this issue Mar 19, 2024 · 2 comments
Open

Add context passing for filters #1977

Mis1eader-dev opened this issue Mar 19, 2024 · 2 comments

Comments

@Mis1eader-dev
Copy link
Member

Is your feature request related to a problem? Please describe.
There are times where individual filters want to work on the same object obtained by one of the previous filters, for example a Logged In filter may guarantee that there is a user object in memory for subsequent filters to make use of.
Now since subsequent filters have no knowledge or context awareness of the previous filters, they have to pull that object from memory somewhere each time, which requires a mutex and a map that contains the desired object.
This creates redundant lookups for the object in each subsequent filter, which was already obtained by the Logged In filter.

Describe the solution you'd like
Add a 4th parameter to filters, std::any& context, which the first filter sets it to the desired object, and subsequent filters will access that 4th parameter to perform their checks on.
The structure of this method may look like the following in the framework:

std::any ctx;
for(auto &filter : filters)
    filter(req, fcb, fccb, ctx);

There's also another way to do this without breaking compatibility with old code using the 3 parameter filters, by adding the context into the request objects themselves, which now will look identical to WebSocketConnection's contexts.

Describe alternatives you've considered
I am aware of drogon::HttpRequest Attribute's existence, but its data type is std::map and involves a lookup anyway.

@afengsoft
Copy link

There is an attributes in req, you can use this

@Mis1eader-dev
Copy link
Member Author

Yeah you're right, my concern is with the lookup, to achieve best performance one has to do req->attributes().insert("", std::move(someObject));
This is to help with the speed of the lookup

Compared to a std::any_cast on a context member it may be debatable which one looks more user friendly, it's not a significant issue

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

No branches or pull requests

2 participants