diff options
author | Valentin Bartenev <vbart@nginx.com> | 2017-10-10 19:15:08 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2017-10-10 19:15:08 +0300 |
commit | e4bea2c75c6643f84eaaaae8f7c474e00e943b8a (patch) | |
tree | c4bf6974a62f962ce19978b416eb65941ae4bd68 /src | |
parent | 93438a0d9e61bf382be3b648c0504fcda2f9c748 (diff) | |
download | unit-e4bea2c75c6643f84eaaaae8f7c474e00e943b8a.tar.gz unit-e4bea2c75c6643f84eaaaae8f7c474e00e943b8a.tar.bz2 |
Optimized application type handling.
Diffstat (limited to 'src')
-rw-r--r-- | src/nxt_application.c | 64 | ||||
-rw-r--r-- | src/nxt_application.h | 3 | ||||
-rw-r--r-- | src/nxt_conf_validation.c | 8 | ||||
-rw-r--r-- | src/nxt_main_process.c | 12 | ||||
-rw-r--r-- | src/nxt_router.c | 19 | ||||
-rw-r--r-- | src/nxt_runtime.c | 2 |
6 files changed, 50 insertions, 58 deletions
diff --git a/src/nxt_application.c b/src/nxt_application.c index 2ecec636..9ce6a8ce 100644 --- a/src/nxt_application.c +++ b/src/nxt_application.c @@ -15,9 +15,9 @@ typedef struct { - nxt_str_t type; - nxt_str_t version; - nxt_str_t file; + nxt_app_type_t type; + nxt_str_t version; + nxt_str_t file; } nxt_module_t; @@ -26,6 +26,7 @@ static nxt_int_t nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, const char *name); static nxt_app_module_t *nxt_app_module_load(nxt_task_t *task, const char *name); +static nxt_app_type_t nxt_app_parse_type(u_char *p, size_t length); static nxt_thread_mutex_t nxt_app_mutex; @@ -132,14 +133,14 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) n = modules->nelts; for (i = 0; i < n; i++) { - nxt_debug(task, "module: %V %V %V", - &module[i].type, &module[i].version, &module[i].file); + nxt_debug(task, "module: %d %V %V", + module[i].type, &module[i].version, &module[i].file); - size += sizeof("{\"type\": \"\",") - 1; + size += sizeof("{\"type\": ,") - 1; size += sizeof(" \"version\": \"\",") - 1; size += sizeof(" \"file\": \"\"},") - 1; - size += module[i].type.length + size += NXT_INT_T_LEN + module[i].version.length + module[i].file.length; } @@ -157,8 +158,8 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) for (i = 0; i < n; i++) { p = nxt_sprintf(p, end, - "{\"type\": \"%V\", \"version\": \"%V\", \"file\": \"%V\"},", - &module[i].type, &module[i].version, &module[i].file); + "{\"type\": %d, \"version\": \"%V\", \"file\": \"%V\"},", + module[i].type, &module[i].version, &module[i].file); } *p++ = ']'; @@ -181,6 +182,7 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, nxt_int_t ret; nxt_uint_t i, n; nxt_module_t *module; + nxt_app_type_t type; nxt_application_module_t *app; /* @@ -211,17 +213,25 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, goto done; } + type = nxt_app_parse_type(app->type.start, app->type.length); + + if (type == NXT_APP_UNKNOWN) { + nxt_log(task, NXT_LOG_NOTICE, "unknown module type %V", app->type); + + goto done; + } + module = modules->elts; n = modules->nelts; for (i = 0; i < n; i++) { - if (nxt_strstr_eq(&app->type, &module[i].type) + if (type == module[i].type && nxt_strstr_eq(&app->version, &module[i].version)) { nxt_log(task, NXT_LOG_NOTICE, "ignoring %s module with the same " "application language version %V %V as in %V", - name, &module[i].type, &module[i].version, + name, &app->type, &app->version, &module[i].file); goto done; @@ -233,10 +243,7 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, goto fail; } - s = nxt_str_dup(mp, &module->type, &app->type); - if (s == NULL) { - goto fail; - } + module->type = type; s = nxt_str_dup(mp, &module->version, &app->version); if (s == NULL) { @@ -994,8 +1001,9 @@ nxt_app_lang_module_t * nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) { u_char *p, *end, *version; - size_t type_length, version_length; + size_t version_length; nxt_uint_t i, n; + nxt_app_type_t type; nxt_app_lang_module_t *lang; end = name->start + name->length; @@ -1013,7 +1021,12 @@ nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) } } - type_length = p - name->start; + type = nxt_app_parse_type(name->start, p - name->start); + + if (type == NXT_APP_UNKNOWN) { + return NULL; + } + version_length = end - version; lang = rt->languages->elts; @@ -1026,7 +1039,7 @@ nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) * so first match chooses the highest version. */ - if (nxt_str_eq(&lang[i].type, name->start, type_length) + if (lang[i].type == type && nxt_strvers_match(lang[i].version, version, version_length)) { return &lang[i]; @@ -1037,16 +1050,21 @@ nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) } -nxt_app_type_t -nxt_app_parse_type(nxt_str_t *str) +static nxt_app_type_t +nxt_app_parse_type(u_char *p, size_t length) { - if (nxt_str_eq(str, "python", 6)) { + nxt_str_t str; + + str.length = length; + str.start = p; + + if (nxt_str_eq(&str, "python", 6)) { return NXT_APP_PYTHON; - } else if (nxt_str_eq(str, "php", 3)) { + } else if (nxt_str_eq(&str, "php", 3)) { return NXT_APP_PHP; - } else if (nxt_str_eq(str, "go", 2)) { + } else if (nxt_str_eq(&str, "go", 2)) { return NXT_APP_GO; } diff --git a/src/nxt_application.h b/src/nxt_application.h index 5561d854..00659a28 100644 --- a/src/nxt_application.h +++ b/src/nxt_application.h @@ -23,7 +23,7 @@ typedef struct nxt_app_module_s nxt_app_module_t; typedef struct { - nxt_str_t type; + nxt_app_type_t type; u_char *version; char *file; nxt_application_module_t *module; @@ -297,7 +297,6 @@ nxt_app_msg_read_length(u_char *src, size_t *length) nxt_app_lang_module_t *nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name); -nxt_app_type_t nxt_app_parse_type(nxt_str_t *str); extern nxt_application_module_t nxt_go_module; diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index 80e1de6f..d7505cd4 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -267,7 +267,6 @@ nxt_conf_vldt_app(nxt_conf_value_t *conf, nxt_str_t *name, nxt_conf_value_t *value) { nxt_str_t type; - nxt_uint_t n; nxt_thread_t *thread; nxt_conf_value_t *type_value; nxt_app_lang_module_t *lang; @@ -299,12 +298,7 @@ nxt_conf_vldt_app(nxt_conf_value_t *conf, nxt_str_t *name, return NXT_ERROR; } - n = nxt_app_parse_type(&lang->type); - if (n != NXT_APP_UNKNOWN) { - return nxt_conf_vldt_object(conf, value, members[n]); - } - - return NXT_ERROR; + return nxt_conf_vldt_object(conf, value, members[lang->type]); } diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c index 4a185a54..819b797b 100644 --- a/src/nxt_main_process.c +++ b/src/nxt_main_process.c @@ -1004,7 +1004,7 @@ fail: static nxt_conf_map_t nxt_app_lang_module_map[] = { { nxt_string("type"), - NXT_CONF_MAP_STR_COPY, + NXT_CONF_MAP_INT, offsetof(nxt_app_lang_module_t, type), }, @@ -1091,8 +1091,8 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) goto fail; } - nxt_debug(task, "lang %V %s \"%s\"", - &lang->type, lang->version, lang->file); + nxt_debug(task, "lang %d %s \"%s\"", + lang->type, lang->version, lang->file); } qsort(rt->languages->elts, rt->languages->nelts, @@ -1119,11 +1119,7 @@ nxt_app_lang_compare(const void *v1, const void *v2) lang1 = v1; lang2 = v2; - if (lang1->type.length != lang2->type.length) { - return lang1->type.length - lang2->type.length; - } - - n = nxt_strncmp(lang1->type.start, lang2->type.start, lang1->type.length); + n = lang1->type - lang2->type; if (n != 0) { return n; diff --git a/src/nxt_router.c b/src/nxt_router.c index d46dae72..32a3b9d3 100644 --- a/src/nxt_router.c +++ b/src/nxt_router.c @@ -960,7 +960,6 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_int_t ret; nxt_str_t name; nxt_app_t *app, *prev; - nxt_app_type_t type; nxt_sockaddr_t *sa; nxt_conf_value_t *conf, *http; nxt_conf_value_t *applications, *application; @@ -1082,20 +1081,6 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_debug(task, "application language module: \"%s\"", lang->file); - type = nxt_app_parse_type(&lang->type); - - if (type == NXT_APP_UNKNOWN) { - nxt_log(task, NXT_LOG_CRIT, "unknown application type: \"%V\"", - &lang->type); - goto app_fail; - } - - if (nxt_app_prepare_msg[type] == NULL) { - nxt_log(task, NXT_LOG_CRIT, "unsupported application type: \"%V\"", - &lang->type); - goto app_fail; - } - ret = nxt_thread_mutex_create(&app->mutex); if (ret != NXT_OK) { goto app_fail; @@ -1107,12 +1092,12 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, app->name.length = name.length; nxt_memcpy(app->name.start, name.start, name.length); - app->type = type; + app->type = lang->type; app->max_workers = apcf.workers; app->timeout = apcf.timeout; app->live = 1; app->max_pending_responses = 2; - app->prepare_msg = nxt_app_prepare_msg[type]; + app->prepare_msg = nxt_app_prepare_msg[lang->type]; nxt_queue_insert_tail(&tmcf->apps, &app->link); diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c index 168f4f19..8fc2bc53 100644 --- a/src/nxt_runtime.c +++ b/src/nxt_runtime.c @@ -90,7 +90,7 @@ nxt_runtime_create(nxt_task_t *task) /* Should not fail. */ lang = nxt_array_add(rt->languages); - lang->type = (nxt_str_t) nxt_string("go"); + lang->type = NXT_APP_GO; lang->version = (u_char *) ""; lang->file = NULL; lang->module = &nxt_go_module; |