drogon icon indicating copy to clipboard operation
drogon copied to clipboard

run_as_daemon 之后无法发送http请求

Open yqmm666 opened this issue 11 months ago • 6 comments

前台运行时候正常可以发送httpclient请求(异步) run_as_daemon切到后台后,无法正确发送。 查看trace的log 发现move to queue之后没有got a new task

yqmm666 avatar Feb 06 '25 09:02 yqmm666

你发送http请求是在run as daemon之后吗?最好写个最小demo复现这个问题,我就可以本地调试看看

an-tao avatar Feb 06 '25 09:02 an-tao

drogon::app().loadConfigFile(path_server_root / "config.json");
drogon::app().setUploadPath(path_upload);

if (run_as_daemon)
{ //
    drogon::app().setLogPath(path_log_files);
    drogon::app().enableRunAsDaemon();
}
else
    Create_PidFile(path_server_root);

我发现当我的config.json文件中含有redis_clients的配置时,run_as_daemon后无法发送httpclient,当我删除config.json中的redis相关配置时一切正常。(实际我的程序中没有用到redis,配置中的redis描述是之前项目的残留)

"redis_clients": [ { //name: Name of the client,'default' by default //"name":"", //host: Server IP, 127.0.0.1 by default "host": "127.0.0.1", //port: Server port, 6379 by default "port": 6379, //username: '' by default which means 'default' in redis ACL "username": "", //passwd: '' by default "passwd": "", //db index: 0 by default "db": 0, //is_fast: false by default, if it is true, the client is faster but user can't call //any synchronous interface of it. "is_fast": false, //number_of_connections: 1 by default, if the 'is_fast' is true, the number is the number of
//connections per IO thread, otherwise it is the total number of all connections.
"number_of_connections": 1, //timeout: -1.0 by default, in seconds, the timeout for executing a command. //zero or negative value means no timeout. "timeout": -1.0 } ],

这些信息是否足够?

yqmm666 avatar Feb 06 '25 10:02 yqmm666

发送httpclient是在run_as_daemon之后,在我的server收到一个请求后,我要在这个请求中去httpclient给另外一个server。 并且关键是,我的项目规划确实需要redis。

yqmm666 avatar Feb 06 '25 10:02 yqmm666

按理说,HttpClient和redisClient是没有关系的。如果他们冲突了,一定有潜在的bug。你可以先自己在框架里加些断点或者log尝试解决。我需要完整的demo才好调试,因为触发这个问题跟具体的写法可能密切相关。

an-tao avatar Feb 06 '25 14:02 an-tao

`#include <drogon/drogon.h>

#include #include

#ifdef linux #include <sys/socket.h> #include <netinet/tcp.h> #endif

using namespace drogon;

int nth_resp = 0;

int main(int argc, char const* argv[]) {

bool run_as_daemon{ false };

if (argc == 2 && std::string(argv[1]) == "-d")
{
    LOG_INFO << "enableRunAsDaemon";
    run_as_daemon = true;

}

app().loadConfigFile("config_test.json");
app().setLogPath("./"); 
app().addListener("0.0.0.0", 5888);

if (run_as_daemon)
{ //

    drogon::app().enableRunAsDaemon();
}
trantor::Logger::setLogLevel(trantor::Logger::kDebug);

app().registerHandler(
    "/test",
    [](const HttpRequestPtr&,
        std::function<void(const HttpResponsePtr&)>&& callback) {

            auto client = HttpClient::newHttpClient("http://www.baidu.com");
            auto req = HttpRequest::newHttpRequest();
            req->setMethod(drogon::Get);
            req->setPath("/s");
            req->setParameter("wd", "wx");
            req->setParameter("oq", "wx");
            client->sendRequest(
                req, [callback](ReqResult result, const HttpResponsePtr& response) {
                    if (result != ReqResult::Ok)
                    {
                        LOG_INFO
                            << "error while sending request to server! result: "
                            << result;
                        auto resp = HttpResponse::newHttpResponse();
                        resp->setBody("send error!");
                        callback(resp);
                        return;
                    }

                    LOG_INFO << "receive response!";
                    auto resp = HttpResponse::newHttpResponse();
                    resp->setBody("send ok!");
                    callback(resp);
                });



    },
    { Get });



app().run();

}

config_test.json

` demo我是这样写的,config_test.json里面删掉redies部分就可以正常 run_as_daemon.不知道是不是对框架的理解不够,哪里写的不对,请查看,谢谢~

yqmm666 avatar Feb 07 '25 08:02 yqmm666

你不要用框架自带的 run_as_daemo, 你在自己应用程序开头, 自己手动 直接将它变成守护进程, 然后在运行drogon 的相关功能

nqf avatar Feb 12 '25 04:02 nqf