drogon icon indicating copy to clipboard operation
drogon copied to clipboard

Add coroutines support for HttpFilter

Open omarmohamedkh opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe. Executing queries in HttpFilter specially for redis becomes messy because of the callbacks and dependency between the queries:

Describe the solution you'd like Add coroutines support for HttpFilter::doFilter method.

Additional context Example:

auto redisClient = drogon::app().getFastRedisClient();
    redisClient->execCommandAsync(
            [fcb, redisClient, username](const drogon::nosql::RedisResult &result) {
                redisClient->execCommandAsync(
                        [fcb, redisClient, username](const drogon::nosql::RedisResult &result) {
                            redisClient->execCommandAsync(
                                    [fcb, redisClient](const drogon::nosql::RedisResult &result) {
                                        redisClient->execCommandAsync(
                                                [](const drogon::nosql::RedisResult &result) {
                                                    for (const auto &r : result.asArray())
                                                        LOG_DEBUG << r.asInteger();
                                                    },
                                                [fcb](const drogon::nosql::RedisException &exception) {
                                                    returnUnauthorized(exception.what(), fcb);
                                                    },
                                                "EXEC");
                                    },
                                    [fcb](const drogon::nosql::RedisException &exception) {
                                        returnUnauthorized(exception.what(), fcb);
                                        },
                                    "EXPIRE qbook:%s.actions %s", username.c_str(), std::to_string(SESSION_EXPIRE_SEC).c_str());
                        },
                        [fcb](const drogon::nosql::RedisException &exception) {
                            returnUnauthorized(exception.what(), fcb);
                            },
                        "EXPIRE qbook:%s.session_id %s", username.c_str(), std::to_string(SESSION_EXPIRE_SEC).c_str());
            },
            [fcb](const drogon::nosql::RedisException &exception) {
                returnUnauthorized(exception.what(), fcb);
                },
            "MULTI");

omarmohamedkh avatar Apr 23 '22 11:04 omarmohamedkh

Does the filter support async?

auto redisClientPtr = app().getRedisClient();
transPtr->execCommandAsync(
		[fccb](const nosql::RedisResult &r) { 
                   // successed
                   return fccb();
                 },
		[](const std::exception &err) { /* this command failed */ },
		"del %s", auth.data());

I have tested in my project and it seems that only synchronous methods can be used.

1111mp avatar Jun 23 '22 09:06 1111mp

Does the filter support async?

auto redisClientPtr = app().getRedisClient();
transPtr->execCommandAsync(
		[fccb](const nosql::RedisResult &r) { 
                   // successed
                   return fccb();
                 },
		[](const std::exception &err) { /* this command failed */ },
		"del %s", auth.data());

I have tested in my project and it seems that only synchronous methods can be used.

@1111mp It does, but the filter chain callback must be running on the IO threads due to some implementation details. So you can only use fast RedisClient or fast DbClient here.

hwc0919 avatar Aug 15 '22 15:08 hwc0919