PHP: implemented "targets" option.

This allows to specify multiple subsequent targets inside PHP applications.
For example:

  {
      "listeners": {
          "*:80": {
              "pass": "routes"
          }
      },

      "routes": [
          {
              "match": {
                  "uri": "/info"
              },

              "action": {
                  "pass": "applications/my_app/phpinfo"
              }
          },
          {
              "match": {
                  "uri": "/hello"
              },

              "action": {
                  "pass": "applications/my_app/hello"
              }
          },
          {
              "action": {
                  "pass": "applications/my_app/rest"
              }
          }
      ],

      "applications": {
          "my_app": {
              "type": "php",
              "targets": {
                  "phpinfo": {
                      "script": "phpinfo.php",
                      "root": "/www/data/admin",
                  },

                  "hello": {
                      "script": "hello.php",
                      "root": "/www/data/test",
                  },

                  "rest": {
                      "root": "/www/data/example.com",
                      "index": "index.php"
                  },
              }
          }
      }
  }
This commit is contained in:
Valentin Bartenev
2020-05-14 13:15:01 +03:00
parent 0174c971b5
commit 376d758dd7
11 changed files with 515 additions and 266 deletions

View File

@@ -27,6 +27,7 @@ typedef struct {
uint32_t requests;
nxt_conf_value_t *limits_value;
nxt_conf_value_t *processes_value;
nxt_conf_value_t *targets_value;
} nxt_router_app_conf_t;
@@ -1272,6 +1273,12 @@ static nxt_conf_map_t nxt_router_app_conf[] = {
NXT_CONF_MAP_PTR,
offsetof(nxt_router_app_conf_t, processes_value),
},
{
nxt_string("targets"),
NXT_CONF_MAP_PTR,
offsetof(nxt_router_app_conf_t, targets_value),
},
};
@@ -1423,12 +1430,13 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
{
u_char *p;
size_t size;
nxt_mp_t *mp;
uint32_t next;
nxt_mp_t *mp, *app_mp;
uint32_t next, next_target;
nxt_int_t ret;
nxt_str_t name, path;
nxt_str_t name, path, target;
nxt_app_t *app, *prev;
nxt_str_t *t;
nxt_str_t *t, *s, *targets;
nxt_uint_t n, i;
nxt_router_t *router;
nxt_app_joint_t *app_joint;
nxt_conf_value_t *conf, *http, *value, *websocket;
@@ -1501,13 +1509,20 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
size = nxt_conf_json_length(application, NULL);
app = nxt_malloc(sizeof(nxt_app_t) + name.length + size);
if (app == NULL) {
app_mp = nxt_mp_create(4096, 128, 1024, 64);
if (nxt_slow_path(app_mp == NULL)) {
goto fail;
}
app = nxt_mp_get(app_mp, sizeof(nxt_app_t) + name.length + size);
if (app == NULL) {
goto app_fail;
}
nxt_memzero(app, sizeof(nxt_app_t));
app->mem_pool = app_mp;
app->name.start = nxt_pointer_to(app, sizeof(nxt_app_t));
app->conf.start = nxt_pointer_to(app, sizeof(nxt_app_t)
+ name.length);
@@ -1522,7 +1537,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
prev = nxt_router_app_find(&router->apps, &name);
if (prev != NULL && nxt_strstr_eq(&app->conf, &prev->conf)) {
nxt_free(app);
nxt_mp_destroy(app_mp);
nxt_queue_remove(&prev->link);
nxt_queue_insert_tail(&tmcf->previous, &prev->link);
@@ -1538,6 +1553,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
apcf.requests = 0;
apcf.limits_value = NULL;
apcf.processes_value = NULL;
apcf.targets_value = NULL;
app_joint = nxt_malloc(sizeof(nxt_app_joint_t));
if (nxt_slow_path(app_joint == NULL)) {
@@ -1587,6 +1603,30 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
apcf.spare_processes = apcf.processes;
}
if (apcf.targets_value != NULL) {
n = nxt_conf_object_members_count(apcf.targets_value);
targets = nxt_mp_get(app_mp, sizeof(nxt_str_t) * n);
if (nxt_slow_path(targets == NULL)) {
goto app_fail;
}
next_target = 0;
for (i = 0; i < n; i++) {
value = nxt_conf_next_object_member(apcf.targets_value,
&target, &next_target);
s = nxt_str_dup(app_mp, &targets[i], &target);
if (nxt_slow_path(s == NULL)) {
goto app_fail;
}
}
} else {
targets = NULL;
}
nxt_debug(task, "application type: %V", &apcf.type);
nxt_debug(task, "application processes: %D", apcf.processes);
nxt_debug(task, "application request timeout: %M", apcf.timeout);
@@ -1628,6 +1668,8 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
app->max_pending_responses = 2;
app->max_requests = apcf.requests;
app->targets = targets;
engine = task->thread->engine;
app->engine = engine;
@@ -1839,7 +1881,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
app_fail:
nxt_free(app);
nxt_mp_destroy(app_mp);
fail:
@@ -1847,7 +1889,7 @@ fail:
nxt_queue_remove(&app->link);
nxt_thread_mutex_destroy(&app->mutex);
nxt_free(app);
nxt_mp_destroy(app->mem_pool);
} nxt_queue_loop;
@@ -4538,7 +4580,7 @@ nxt_router_free_app(nxt_task_t *task, void *obj, void *data)
nxt_assert(nxt_queue_is_empty(&app->idle_ports));
nxt_thread_mutex_destroy(&app->mutex);
nxt_free(app);
nxt_mp_destroy(app->mem_pool);
app_joint->app = NULL;
@@ -4992,6 +5034,8 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r,
req = (nxt_unit_request_t *) out->mem.free;
out->mem.free += req_size;
req->app_target = r->app_target;
req->content_length = content_length;
p = (u_char *) (req->fields + fields_count);