Skip to content

Commit

Permalink
Support max clients configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gaodunqiao committed Aug 21, 2017
1 parent 9549db6 commit 577c11c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions conf/zgw-example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ server_ip: 0.0.0.0
server_port: 8099
admin_port: 8199
worker_num: 4
max_clients: 8000
enable_gc: no

#yes or no
Expand Down
2 changes: 2 additions & 0 deletions src/zgw_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ZgwConfig::ZgwConfig(std::string path)
daemonize(false),
minloglevel(0),
worker_num(2),
max_clients(5000),
enable_gc(false),
log_path("./log"),
pid_file(kZgwPidFile),
Expand Down Expand Up @@ -49,6 +50,7 @@ int ZgwConfig::LoadConf() {
b_conf->GetConfBool("daemonize", &daemonize);
b_conf->GetConfInt("minloglevel", &minloglevel);
b_conf->GetConfInt("worker_num", &worker_num);
b_conf->GetConfInt("max_clients", &max_clients);
b_conf->GetConfBool("enable_gc", &enable_gc);

b_conf->GetConfStr("log_path", &log_path);
Expand Down
3 changes: 2 additions & 1 deletion src/zgw_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ struct ZgwConfig {
slash::BaseConf *b_conf;

std::vector<std::string> zp_meta_ip_ports;
std::string zp_table_name;
std::string redis_ip_port;
std::string zp_table_name;
std::string redis_passwd;

std::string server_ip;
Expand All @@ -26,6 +26,7 @@ struct ZgwConfig {
int minloglevel;
int cron_interval;
int worker_num;
int max_clients;
bool enable_gc;

std::string log_path;
Expand Down
14 changes: 11 additions & 3 deletions src/zgw_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ static std::string LockName() {
std::to_string(thread_seq_++);
}

int ZgwServerHandle::CreateWorkerSpecificData(void** data) const {
bool ZgwServer::ZgwServerHandle::AccessHandle(std::string& ip) const {
if (zgw_server_->zgw_dispatch_thread_->conn_num() > g_zgw_conf->max_clients) {
return false;
}
return true;
}

int ZgwServer::ZgwServerHandle::CreateWorkerSpecificData(void** data) const {
zgwstore::ZgwStore* store;
Status s = zgwstore::ZgwStore::Open(g_zgw_conf->zp_meta_ip_ports,
g_zgw_conf->redis_ip_port,
Expand All @@ -35,14 +42,15 @@ int ZgwServerHandle::CreateWorkerSpecificData(void** data) const {
return 0;
}

int ZgwServerHandle::DeleteWorkerSpecificData(void* data) const {
int ZgwServer::ZgwServerHandle::DeleteWorkerSpecificData(void* data) const {
delete reinterpret_cast<zgwstore::ZgwStore*>(data);
return 0;
}

ZgwServer::ZgwServer()
: should_exit_(false),
worker_num_(g_zgw_conf->worker_num) {
worker_num_(g_zgw_conf->worker_num),
server_handle_(this) {
if (worker_num_ > kMaxWorkerThread) {
LOG(WARNING) << "Exceed max worker thread num: " << kMaxWorkerThread;
worker_num_ = kMaxWorkerThread;
Expand Down
18 changes: 12 additions & 6 deletions src/zgw_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@

using slash::Status;

class ZgwServerHandle : public pink::ServerHandle {
public:
virtual int CreateWorkerSpecificData(void** data) const override;
virtual int DeleteWorkerSpecificData(void* data) const override;
};

class ZgwServer {
public:
explicit ZgwServer();
Expand All @@ -48,6 +42,18 @@ class ZgwServer {

int worker_num_;

class ZgwServerHandle : public pink::ServerHandle {
public:
ZgwServerHandle(ZgwServer* server) : zgw_server_(server) {}

virtual bool AccessHandle(std::string& ip) const override;
virtual int CreateWorkerSpecificData(void** data) const override;
virtual int DeleteWorkerSpecificData(void* data) const override;

private:
ZgwServer* zgw_server_;
};

ZgwServerHandle server_handle_;
ZgwConnFactory conn_factory_;
pink::ServerThread* zgw_dispatch_thread_;
Expand Down

0 comments on commit 577c11c

Please sign in to comment.