C/C++ - 如何在 Apache HTTP Server 中创建单例连接模块?
C/C++ - How to make a singleton connection module in Apache HTTP Server?
假设我的 apache
模块有以下代码:
#include <iostream>
#include <string>
#include <httpd.h>
#include <http_core.h>
#include <http_protocol.h>
#include <http_request.h>
#include <apr_strings.h>
int count = 0;
static void my_child_init(apr_pool_t *p, server_rec *s)
{
count = 1000; //starts up with this number!
}
static int my_handler(request_rec *r)
{
count++; //Increments here
ap_rputs(std::to_string(count).c_str(), r);
return OK;
}
static void register_hooks(apr_pool_t *pool)
{
ap_hook_child_init(my_child_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(my_handler, NULL, NULL, APR_HOOK_LAST);
}
module AP_MODULE_DECLARE_DATA myserver_module =
{
STANDARD20_MODULE_STUFF,
NULL, // Per-directory configuration handler
NULL, // Merge handler for per-directory configurations
NULL, // Per-server configuration handler
NULL, // Merge handler for per-server configurations
NULL, // Any directives we may have for httpd
register_hooks // Our hook registering function
};
现在,如果我打开浏览器并转到 localhost/my_server
,每次刷新页面时我都会看到我的 count
递增,从而创建一个到 Apache
的新 HTTP 请求。
1001 //from connection 1
1002 //from connection 1
1003 //from connection 1
1004 //from connection 1
...
我原以为每次刷新时,我都会看到 count
递增。但有时我看到 apache
可能创建了另一个连接并且模块再次实例化..我现在有两个相等的连接 运行:
1151 //from connection 1
1152 //from connection 1
1001 // from connection 2
1153 //from connection 1
1002 // from connection 2
1003 // from connection 2
1154 //from connection 1
...
有没有办法阻止 apache 重新加载同一个模块?
大多数 Apache MPM/常见配置都会创建多个子进程。您可以将它们配置为使用具有多个线程的单个进程,或者为您的计数器使用共享内存。
以可移植方式使用共享内存的最简单方法是依赖 "slotmem" 和 "slotmem_shm" 模块。 mod_proxy_balancer 使用这个。另一种方法是 server/scoreboard.c 如何直接使用共享内存。
假设我的 apache
模块有以下代码:
#include <iostream>
#include <string>
#include <httpd.h>
#include <http_core.h>
#include <http_protocol.h>
#include <http_request.h>
#include <apr_strings.h>
int count = 0;
static void my_child_init(apr_pool_t *p, server_rec *s)
{
count = 1000; //starts up with this number!
}
static int my_handler(request_rec *r)
{
count++; //Increments here
ap_rputs(std::to_string(count).c_str(), r);
return OK;
}
static void register_hooks(apr_pool_t *pool)
{
ap_hook_child_init(my_child_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_handler(my_handler, NULL, NULL, APR_HOOK_LAST);
}
module AP_MODULE_DECLARE_DATA myserver_module =
{
STANDARD20_MODULE_STUFF,
NULL, // Per-directory configuration handler
NULL, // Merge handler for per-directory configurations
NULL, // Per-server configuration handler
NULL, // Merge handler for per-server configurations
NULL, // Any directives we may have for httpd
register_hooks // Our hook registering function
};
现在,如果我打开浏览器并转到 localhost/my_server
,每次刷新页面时我都会看到我的 count
递增,从而创建一个到 Apache
的新 HTTP 请求。
1001 //from connection 1
1002 //from connection 1
1003 //from connection 1
1004 //from connection 1
...
我原以为每次刷新时,我都会看到 count
递增。但有时我看到 apache
可能创建了另一个连接并且模块再次实例化..我现在有两个相等的连接 运行:
1151 //from connection 1
1152 //from connection 1
1001 // from connection 2
1153 //from connection 1
1002 // from connection 2
1003 // from connection 2
1154 //from connection 1
...
有没有办法阻止 apache 重新加载同一个模块?
大多数 Apache MPM/常见配置都会创建多个子进程。您可以将它们配置为使用具有多个线程的单个进程,或者为您的计数器使用共享内存。
以可移植方式使用共享内存的最简单方法是依赖 "slotmem" 和 "slotmem_shm" 模块。 mod_proxy_balancer 使用这个。另一种方法是 server/scoreboard.c 如何直接使用共享内存。