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

关于hmain与hlog文件的一些问题疑惑与解决方式 #535

Closed
House-Men opened this issue Apr 25, 2024 · 3 comments
Closed

关于hmain与hlog文件的一些问题疑惑与解决方式 #535

House-Men opened this issue Apr 25, 2024 · 3 comments

Comments

@House-Men
Copy link

House-Men commented Apr 25, 2024

使用的libhv版本:1.3.2

问题1:在 main_ctx_init 处理中Win32判定旧进程是否结束,我遇到最近已终止的pid存在同样能够OpenProcess成功造成误判现象
最后解决方式如下。

HANDLE hproc = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, FALSE, g_main_ctx.oldpid);
DWORD exitCode = 0;
if (hproc) {
   GetExitCodeProcess(hproc, &exitCode);
    CloseHandle(hproc);
}
if(exitCode != STILL_ACTIVE) g_main_ctx.oldpid = -1;

问题2:在Win32环境kill_proc采用的TerminateProcess强制终止导致并不触发atexit常规收尾delete_pidfile结束,而代码中也有s_hEventTerm处理的方式,可为什么相关的代码都被注释了,是存在什么问题吗?我是否可以调整为Win32Event处理方式。

问题3:在 main_ctx_init 处理中强制conf/pid/log需读写权限的文件在run_dir运行目录下,导致业务应用在不同的场景启动时运行目录混乱造成错误以及程序在通过exe包admin权限安装后或deb包root权限安装后运行目录并无写入权限造成错误,之后通过调整如下代码支持可以由业务应用优先决定设置run_dir为Win32的%localAppData%或linux的用户home做为数据目录解决。

if(!hv_exists(g_main_ctx.run_dir)) get_executable_dir(g_main_ctx.run_dir, sizeof(g_main_ctx.run_dir));

问题4:在 signal_handle中 处理 "status" 时对已经停止的提示为"%s stop/waiting\n"有语义错误,应该调整为"%s is already stopped\n"

问题5:在业务中使用 signal_handle 感到不利于业务自定义交互形式,因为内部许多直接exit的处理,并且无法得知具体的处理状态,最后通过删除exit并增加具体处理状态的返回值解决。

问题6:在hmain文件中有许多printf做为关键信息提示,但在我的业务中有对不同系统平台自定义的一套信息提示方案,最后是增加一个具体消息用意类型参数并将这些printf调整为printf_callback导出函数指针并默认处理过程为printf直接输出,这样同时也就支持修改为业务所需的自定义处理过程解决。

问题7:某些人或许并不期望自己的业务Release版存在debug输出与源代码信息,是否可以增加编译选项开启使用如下日志形式

#ifdef NDEBUG
	#define hlogd(fmt, ...) 
	#define hlogi(fmt, ...) logger_print(hlog, LOG_LEVEL_INFO,  fmt, ## __VA_ARGS__)
	#define hlogw(fmt, ...) logger_print(hlog, LOG_LEVEL_WARN,  fmt, ## __VA_ARGS__)
	#define hloge(fmt, ...) logger_print(hlog, LOG_LEVEL_ERROR, fmt, ## __VA_ARGS__)
	#define hlogf(fmt, ...) logger_print(hlog, LOG_LEVEL_FATAL, fmt, ## __VA_ARGS__)
#else
	#define hlogd(fmt, ...) logger_print(hlog, LOG_LEVEL_DEBUG, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILENAME__, __LINE__, __FUNCTION__)
	#define hlogi(fmt, ...) logger_print(hlog, LOG_LEVEL_INFO,  fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILENAME__, __LINE__, __FUNCTION__)
	#define hlogw(fmt, ...) logger_print(hlog, LOG_LEVEL_WARN,  fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILENAME__, __LINE__, __FUNCTION__)
	#define hloge(fmt, ...) logger_print(hlog, LOG_LEVEL_ERROR, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILENAME__, __LINE__, __FUNCTION__)
	#define hlogf(fmt, ...) logger_print(hlog, LOG_LEVEL_FATAL, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILENAME__, __LINE__, __FUNCTION__)
#endif
@House-Men
Copy link
Author

补充一个问题:在使用WebSocketServer时虽然能够通过onopen与onclose实现监控ws会话数,但无法监控父类HttpServer的实际当前tcp连接数,目前还在理解源代码看从哪里着手增加这个监控连接数的功能,也期望作者后面能支持下这个需求。

@House-Men
Copy link
Author

http服务监控TCP连接断开事件已解决 测试正常 大致处理如下

//HttpServer.h的http_server_s中增加两个回调指针
std::function<bool(hio_t* io)> onAccept;
std::function<void(hio_t* io)> onClose;

//HttpHandler.h的HttpHandler中增加一个指针存储
void                    *user_data;

//HttpServer.cpp的on_close中增加关闭回调触发
http_server_t* server = (http_server_t*)handler->user_data;
...
if (loop) {
    if (server->onClose) {
        server->onClose(io);
    }
    --loop->connectionNum;
}

//HttpServer.cpp的on_accept中增加连接回调触发和保存指针
if (server->onAccept) {
    if (!server->onAccept(io)) {
        hio_close(io);
        return;
    }
}
++loop->connectionNum;
...
handler->user_data = server;

@ithewei
Copy link
Owner

ithewei commented Apr 26, 2024

首先,感谢你的反馈以及对libhv的认可!
你上面的大多问题中是对hmain和hlog的定制化改造,如果不满足你的需要的话,可以fork后自行修改。
问题1:这个你可以提个PR
问题2:可以调整为Win32Event处理方式,只是需要sleep
问题3:可以在main_ctx_init后自行再重新设置conf/pid/log等路径
问题4:这里是调用了waitpid循环等待所有子进程退出

关于HttpServer提供出onAccept和onClose回调,这个后面可以考虑加上。

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