2014-11-19 00:25:21 +08:00
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
#include "master_service.h"
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
char *var_cfg_redis_addrs;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_str_tbl var_conf_str_tab[] = {
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{ "redis_addrs", "127.0.0.1:6379", &var_cfg_redis_addrs },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
int var_cfg_use_redis_session;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_bool_tbl var_conf_bool_tab[] = {
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{ "use_redis_session", 1, &var_cfg_use_redis_session },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
int var_cfg_conn_timeout;
|
|
|
|
|
int var_cfg_rw_timeout;
|
|
|
|
|
int var_cfg_max_threads;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_int_tbl var_conf_int_tab[] = {
|
2015-08-09 22:42:51 +08:00
|
|
|
|
{ "rw_timeout", 120, &var_cfg_rw_timeout, 0, 0 },
|
|
|
|
|
{ "ioctl_max_threads", 128, &var_cfg_max_threads, 0, 0 },
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
long long int var_cfg_int64;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
acl::master_int64_tbl var_conf_int64_tab[] = {
|
|
|
|
|
{ "int64", 120, &var_cfg_int64, 0, 0 },
|
|
|
|
|
|
|
|
|
|
{ 0, 0 , 0 , 0, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
master_service::master_service()
|
|
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
|
session_ = NULL;
|
|
|
|
|
cluster_ = NULL;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
master_service::~master_service()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_read(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
http_servlet* servlet = (http_servlet*) conn->get_ctx();
|
|
|
|
|
if (servlet == NULL)
|
|
|
|
|
logger_fatal("servlet null!");
|
|
|
|
|
|
2015-08-09 22:42:51 +08:00
|
|
|
|
if (var_cfg_use_redis_session)
|
|
|
|
|
return servlet->doRun(*session_, conn);
|
|
|
|
|
else
|
|
|
|
|
return servlet->doRun("127.0.0.1:11211", conn);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_accept(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
logger("connect from %s, fd: %d", conn->get_peer(true),
|
|
|
|
|
conn->sock_handle());
|
2015-08-09 22:42:51 +08:00
|
|
|
|
conn->set_rw_timeout(var_cfg_rw_timeout);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
http_servlet* servlet = new http_servlet();
|
|
|
|
|
conn->set_ctx(servlet);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool master_service::thread_on_timeout(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
logger("read timeout from %s, fd: %d", conn->get_peer(),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::thread_on_close(acl::socket_stream* conn)
|
|
|
|
|
{
|
|
|
|
|
logger("disconnect from %s, fd: %d", conn->get_peer(),
|
|
|
|
|
conn->sock_handle());
|
|
|
|
|
|
|
|
|
|
http_servlet* servlet = (http_servlet*) conn->get_ctx();
|
|
|
|
|
if (servlet)
|
|
|
|
|
delete servlet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::thread_on_init()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::thread_on_exit()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::proc_on_init()
|
|
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
|
// create redis cluster for session cluster
|
|
|
|
|
cluster_ = new acl::redis_client_cluster(var_cfg_conn_timeout,
|
|
|
|
|
var_cfg_rw_timeout);
|
|
|
|
|
cluster_->init(NULL, var_cfg_redis_addrs, var_cfg_max_threads);
|
|
|
|
|
|
|
|
|
|
// create session cluster
|
|
|
|
|
session_ = new acl::redis_session(*cluster_, var_cfg_max_threads);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void master_service::proc_on_exit()
|
|
|
|
|
{
|
|
|
|
|
delete session_;
|
|
|
|
|
delete cluster_;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 19:44:37 +08:00
|
|
|
|
bool master_service::proc_exit_timer(size_t nclients, size_t nthreads)
|
|
|
|
|
{
|
|
|
|
|
if (nclients == 0 || nthreads == 0)
|
|
|
|
|
{
|
|
|
|
|
logger("clients count: %d, threads count: %d",
|
|
|
|
|
(int) nclients, (int) nthreads);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|