/
config.hpp
166 lines (136 loc) · 5.1 KB
/
config.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
//#include <sys/sysinfo.h>
#include <cstdint>
#include <exception>
#include <algorithm>
#include <string>
#include <tuple>
#include <vector>
#include <locale>
#include "yaml-cpp/yaml.h"
#include "log/log.hpp"
namespace snow {
template<typename T = int>
class config {
public:
config(const std::string &conf_file_name)
: m_proc_num(2/*::get_nprocs()*/),
m_connection_timeout(DEFAULT_CONNECTION_TIMEOUT),
m_max_connecction(DEFAULT_MAX_CONNECTION),
m_max_request_per_second(DEFAULT_MAX_REQUEST_PER_SECOND),
m_log_level(DEFAULT_LOG_LEVEL),
m_log_format(DEFAULT_LOG_FORMATE) {
init(conf_file_name);
}
int get_proc_num() const {
return m_proc_num;
}
int get_connection_timeout() const {
return m_connection_timeout;
}
int get_max_connection() const {
return m_max_connecction;
}
int get_max_request_per_second() const {
return m_max_request_per_second;
}
int get_log_level() const {
return m_log_level;
}
const std::string &get_log_format() const {
return m_log_format;
}
const std::vector<std::string> &get_endpoints() const {
return m_endpoints;
}
private:
config(const config &) = delete;
void operator=(const config &) = delete;
bool is_log_format_valid(const std::string &log_format) const {
return true;
}
int init(const std::string &conf_file_name) {
try {
YAML::Node config = YAML::LoadFile(conf_file_name);
if (config["interface"] && config["interface"].IsSequence()) {
for (auto it = config["interface"].begin(); it != config["interface"].end(); ++it) {
m_endpoints.emplace_back(it->as<std::string>());
}
}
if (config["service"] && config["service"].IsMap()) {
if (config["service"]["procnum"]) {
m_proc_num = std::max(m_proc_num, config["service"]["procnum"].as<int>());
}
if (config["service"]["log"] && config["service"]["log"].IsMap()) {
if (config["service"]["log"]["level"]) {
m_log_level = std::max(config::MIN_LOG_LEVEL, config["service"]["log"]["level"].as<int>());
m_log_level = std::min(m_log_level, config["service"]["log"]["level"].as<int>());
}
if (config["service"]["log"]["format"] &&
is_log_format_valid(config["service"]["log"]["format"].as<std::string>())) {
m_log_format = config["service"]["log"]["format"].as<std::string>();
}
}
}
if (config["limits"] && config["limits"].IsMap()) {
if (config["limits"]["max_connection"]) {
m_max_connecction = std::max(MIN_CONNECTION, config["limits"]["max_connection"].as<int>());
}
if (config["limits"]["connection_timeout"]) {
m_connection_timeout = std::max(MIN_CONNECTION_TIMEOUT,
config["limits"]["connection_timeout"].as<int>());
}
if (config["limits"]["tcp_send_buf"]) {
std::cout << config["limits"]["tcp_send_buf"].as<int>() << std::endl;
}
if (config["limits"]["tcp_recv_buf"]) {
std::cout << config["limits"]["tcp_recv_buf"].as<int>() << std::endl;
}
}
return 0;
} catch (std::exception &e) {
SNOW_LOG_FATAL("config init failed : {}", e.what());
return -1;
}
}
static const int MIN_PROC_NUM;
static const int DEFAULT_CONNECTION_TIMEOUT;
static const int MIN_CONNECTION_TIMEOUT;
static const int DEFAULT_MAX_CONNECTION;
static const int MIN_CONNECTION;
static const int DEFAULT_MAX_REQUEST_PER_SECOND;
static const int DEFAULT_LOG_LEVEL;
static const int MIN_LOG_LEVEL;
static const int MAX_LOG_LEVEL;
static const char *const DEFAULT_LOG_FORMATE;
private:
int m_proc_num;
int m_connection_timeout; //ms
int m_max_connecction;
int m_max_request_per_second;
int m_log_level;
std::string m_log_format;
std::vector<std::string> m_endpoints;
};
template<typename T>
const int config<T>::MIN_PROC_NUM = 1;
template<typename T>
const int config<T>::DEFAULT_CONNECTION_TIMEOUT = 60 * 1000; //1min
template<typename T>
const int config<T>::MIN_CONNECTION_TIMEOUT = 1000; //1s
template<typename T>
const int config<T>::DEFAULT_MAX_CONNECTION = 100;
template<typename T>
const int config<T>::MIN_CONNECTION = 1;
template<typename T>
const int config<T>::DEFAULT_MAX_REQUEST_PER_SECOND = 1000;
template<typename T>
const int config<T>::DEFAULT_LOG_LEVEL = 3;
template<typename T>
const int config<T>::MIN_LOG_LEVEL = 0;
template<typename T>
const int config<T>::MAX_LOG_LEVEL = 6;
template<typename T>
const char *const config<T>::DEFAULT_LOG_FORMATE = "test";
using Config = config<void>;
}