config_simple.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <fstream>
  2. #include "config.h"
  3. #include "workflow/WFGlobal.h"
  4. #include "workflow/UpstreamManager.h"
  5. #include "workflow/UpstreamPolicies.h"
  6. using namespace srpc;
  7. // default upstream_route_t
  8. static unsigned int default_consistent_hash(const char *path, const char *query,
  9. const char *fragment)
  10. {
  11. return 0;
  12. }
  13. static unsigned int default_select_route(const char *path, const char *query,
  14. const char *fragment)
  15. {
  16. return 0;
  17. }
  18. static void set_endpoint_params(const wfrest::Json& data,
  19. struct EndpointParams *params)
  20. {
  21. *params = ENDPOINT_PARAMS_DEFAULT;
  22. for (const auto& it : data)
  23. {
  24. if (it.key() == "max_connections")
  25. params->max_connections = data["max_connections"];
  26. else if (it.key() == "connect_timeout")
  27. params->connect_timeout = data["connect_timeout"];
  28. else if (it.key() == "response_timeout")
  29. params->response_timeout = data["response_timeout"];
  30. else if (it.key() == "ssl_connect_timeout")
  31. params->ssl_connect_timeout = data["ssl_connect_timeout"];
  32. else if (it.key() == "use_tls_sni")
  33. params->use_tls_sni = data["use_tls_sni"];
  34. else
  35. {
  36. printf("[INFO][set_endpoint_params] Unknown key : %s\n",
  37. it.key().c_str());
  38. }
  39. }
  40. }
  41. static void load_global(const wfrest::Json& data)
  42. {
  43. struct WFGlobalSettings settings = GLOBAL_SETTINGS_DEFAULT;
  44. std::string resolv_conf_path;
  45. std::string hosts_path;
  46. for (const auto& it : data)
  47. {
  48. if (it.key() == "endpoint_params")
  49. {
  50. set_endpoint_params(data["endpoint_params"],
  51. &settings.endpoint_params);
  52. }
  53. else if (it.key() == "dns_server_params")
  54. {
  55. set_endpoint_params(data["dns_server_params"],
  56. &settings.dns_server_params);
  57. }
  58. else if (it.key() == "dns_ttl_default")
  59. settings.dns_ttl_default = data["dns_ttl_default"];
  60. else if (it.key() == "dns_ttl_min")
  61. settings.dns_ttl_min = data["dns_ttl_min"];
  62. else if (it.key() == "dns_threads")
  63. settings.dns_threads = data["dns_threads"];
  64. else if (it.key() == "poller_threads")
  65. settings.poller_threads = data["poller_threads"];
  66. else if (it.key() == "handler_threads")
  67. settings.handler_threads = data["handler_threads"];
  68. else if (it.key() == "compute_threads")
  69. settings.compute_threads = data["compute_threads"];
  70. else if (it.key() == "resolv_conf_path")
  71. {
  72. resolv_conf_path = data["resolv_conf_path"].get<std::string>();
  73. settings.resolv_conf_path = resolv_conf_path.c_str();
  74. }
  75. else if (it.key() == "hosts_path")
  76. {
  77. hosts_path = data["hosts_path"].get<std::string>();
  78. settings.hosts_path = hosts_path.c_str();
  79. }
  80. else
  81. printf("[INFO][load_global] Unknown key : %s\n", it.key().c_str());
  82. }
  83. WORKFLOW_library_init(&settings);
  84. }
  85. static bool load_upstream_server(const wfrest::Json& data,
  86. std::vector<std::string>& hosts,
  87. std::vector<AddressParams>& params)
  88. {
  89. AddressParams param;
  90. hosts.clear();
  91. params.clear();
  92. for (const auto& server : data)
  93. {
  94. if (server.has("host") == false)
  95. {
  96. printf("[ERROR][load_upstream] Invalid upstream server\n");
  97. continue;
  98. }
  99. param = ADDRESS_PARAMS_DEFAULT;
  100. if (server.has("params"))
  101. {
  102. for (const auto& p : server["params"])
  103. {
  104. if (p.key() == "endpoint_params")
  105. set_endpoint_params(p.value(), &param.endpoint_params);
  106. else if (p.key() == "weight")
  107. param.weight = p.value().get<unsigned short>();
  108. else if (p.key() == "max_fails")
  109. param.max_fails = p.value().get<unsigned int>();
  110. else if (p.key() == "dns_ttl_default")
  111. param.dns_ttl_default = p.value().get<unsigned int>();
  112. else if (p.key() == "dns_ttl_min")
  113. param.dns_ttl_min = p.value().get<unsigned int>();
  114. else if (p.key() == "server_type")
  115. param.server_type = p.value().get<int>();
  116. else if (p.key() == "group_id")
  117. param.group_id = p.value().get<int>();
  118. else
  119. printf("[ERROR][load_upstream] Invalid params: %s\n",
  120. p.key().c_str());
  121. }
  122. }
  123. hosts.push_back(server["host"]);
  124. params.push_back(param);
  125. }
  126. if (hosts.size() == 0)
  127. return false;
  128. else
  129. return true;
  130. }
  131. static void load_upstream(const wfrest::Json& data)
  132. {
  133. std::string name;
  134. std::string type;
  135. bool try_another;
  136. std::vector<std::string> hosts;
  137. std::vector<AddressParams> params;
  138. for (const auto& it : data)
  139. {
  140. if (it.has("name") == false ||
  141. it.has("type") == false ||
  142. it.has("server") == false ||
  143. load_upstream_server(it["server"], hosts, params) == false)
  144. {
  145. printf("[ERROR][load_upstream] Invalid upstream\n");
  146. continue;
  147. }
  148. name = it["name"].get<std::string>();
  149. type = it["type"].get<std::string>();
  150. if (it.has("try_another"))
  151. try_another = it["try_another"];
  152. else
  153. try_another = false;
  154. if (type == "weighted_random")
  155. {
  156. UpstreamManager::upstream_create_weighted_random(name, try_another);
  157. }
  158. else if (type == "consistent_hash")
  159. {
  160. UpstreamManager::upstream_create_consistent_hash(name,
  161. default_consistent_hash);
  162. }
  163. else if (type == "round_robin")
  164. {
  165. UpstreamManager::upstream_create_round_robin(name, try_another);
  166. }
  167. else if (type == "manual")
  168. {
  169. UpstreamManager::upstream_create_manual(name,
  170. default_select_route,
  171. try_another,
  172. default_consistent_hash);
  173. }
  174. else if (type == "vnswrr")
  175. {
  176. UpstreamManager::upstream_create_vnswrr(name);
  177. }
  178. else
  179. {
  180. printf("[INFO][load_upstream] Unknown type : %s\n", type.c_str());
  181. continue;
  182. }
  183. for (size_t i = 0; i < hosts.size(); i++)
  184. UpstreamManager::upstream_add_server(name, hosts[i], &params[i]);
  185. }
  186. }
  187. void RPCConfig::load_server()
  188. {
  189. if (this->data["server"].has("port"))
  190. this->s_port = this->data["server"]["port"];
  191. if (this->data["server"].has("root"))
  192. this->root_path = this->data["server"]["root"].get<std::string>();
  193. if (this->data["server"].has("cert_file"))
  194. this->s_cert_file = this->data["server"]["cert_file"].get<std::string>();
  195. if (this->data["server"].has("file_key"))
  196. this->s_file_key = this->data["server"]["file_key"].get<std::string>();
  197. if (this->data["server"].has("error_page"))
  198. {
  199. for (const auto& it : this->data["server"]["error_page"])
  200. {
  201. std::string page;
  202. if (it.has("error") == true && it.has("error") == true)
  203. {
  204. page = it["page"].get<std::string>();
  205. for (const auto& e : it["error"])
  206. this->error_page.insert(std::make_pair(e.get<int>(), page));
  207. }
  208. else
  209. {
  210. printf("[ERROR][load_file_service] Invalid error_page\n");
  211. continue;
  212. }
  213. }
  214. }
  215. }
  216. void RPCConfig::load_client()
  217. {
  218. if (this->data["client"].has("remote_host"))
  219. this->c_host = this->data["client"]["remote_host"].get<std::string>();
  220. if (this->data["client"].has("remote_port"))
  221. this->c_port = this->data["client"]["remote_port"];
  222. if (this->data["client"].has("redirect_max"))
  223. this->c_redirect_max = this->data["client"]["redirect_max"];
  224. if (this->data["client"].has("retry_max"))
  225. this->c_retry_max = this->data["client"]["retry_max"];
  226. if (this->data["client"].has("user_name"))
  227. this->c_user_name = this->data["client"]["user_name"].get<std::string>();
  228. if (this->data["client"].has("password"))
  229. this->c_password = this->data["client"]["password"].get<std::string>();
  230. }
  231. bool RPCConfig::load(const char *file)
  232. {
  233. FILE *fp = fopen(file, "r");
  234. if (!fp)
  235. return false;
  236. this->data = wfrest::Json::parse(fp);
  237. fclose(fp);
  238. if (this->data.is_valid() == false)
  239. return false;
  240. for (const auto& it : this->data)
  241. {
  242. if (it.key() == "server")
  243. this->load_server();
  244. else if (it.key() == "client")
  245. this->load_client();
  246. else if (it.key() == "global")
  247. load_global(it.value());
  248. else if (it.key() == "upstream")
  249. load_upstream(it.value());
  250. else
  251. printf("[INFO][RPCConfig::load] Unknown key: %s\n", it.key().c_str());
  252. }
  253. return true;
  254. };
  255. RPCConfig::~RPCConfig()
  256. {
  257. }