{"action":"created","comment":{"url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/comments/65770071","id":65770071,"diff_hunk":"@@ -349,6 +353,55 @@ ngx_http_lua_upstream_set_peer_down(lua_State * L)\n }\n \n \n+static int\n+ngx_http_lua_upstream_get_chosen_peer(lua_State * L)\n+{\n+    ngx_str_t                           host;\n+    ngx_http_upstream_srv_conf_t       *us;\n+    ngx_http_request_t                 *r;\n+    ngx_http_request_t                 *fake_r;\n+    ngx_peer_connection_t              *peer;\n+    int                                 rc;\n+\n+    if (lua_gettop(L) != 1) {\n+        return luaL_error(L, \"exactly one argument expected\");\n+    }\n+\n+    host.data = (u_char *) luaL_checklstring(L, 1, &host.len);\n+\n+    us = ngx_http_lua_upstream_find_upstream(L, &host);\n+    if (us == NULL) {\n+        lua_pushnil(L);\n+        lua_pushliteral(L, \"upstream not found\");\n+        return 2;\n+    }\n+\n+    r = ngx_http_lua_get_request(L);\n+\n+    // we create a fake request object, which we will use to fetch the upstream","path":"src/ngx_http_lua_upstream_module.c","position":47,"original_position":47,"commit_id":"e08678790c97390030c00274a5d7c9a714614512","original_commit_id":"e08678790c97390030c00274a5d7c9a714614512","user":{"login":"agentzh","id":56241,"avatar_url":"https://avatars.githubusercontent.com/u/56241?v=3","gravatar_id":"","url":"https://api.github.com/users/agentzh","html_url":"https://github.com/agentzh","followers_url":"https://api.github.com/users/agentzh/followers","following_url":"https://api.github.com/users/agentzh/following{/other_user}","gists_url":"https://api.github.com/users/agentzh/gists{/gist_id}","starred_url":"https://api.github.com/users/agentzh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/agentzh/subscriptions","organizations_url":"https://api.github.com/users/agentzh/orgs","repos_url":"https://api.github.com/users/agentzh/repos","events_url":"https://api.github.com/users/agentzh/events{/privacy}","received_events_url":"https://api.github.com/users/agentzh/received_events","type":"User","site_admin":false},"body":"Style: no C++-style line comments.","created_at":"2016-06-03T20:25:16Z","updated_at":"2016-06-03T20:25:16Z","html_url":"https://github.com/openresty/lua-upstream-nginx-module/pull/25#discussion_r65770071","pull_request_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25","_links":{"self":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/comments/65770071"},"html":{"href":"https://github.com/openresty/lua-upstream-nginx-module/pull/25#discussion_r65770071"},"pull_request":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25"}}},"pull_request":{"url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25","id":72532854,"html_url":"https://github.com/openresty/lua-upstream-nginx-module/pull/25","diff_url":"https://github.com/openresty/lua-upstream-nginx-module/pull/25.diff","patch_url":"https://github.com/openresty/lua-upstream-nginx-module/pull/25.patch","issue_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues/25","number":25,"state":"open","locked":false,"title":"Added ngx.upstream.get_chosen_peer() function","user":{"login":"kipras","id":1502290,"avatar_url":"https://avatars.githubusercontent.com/u/1502290?v=3","gravatar_id":"","url":"https://api.github.com/users/kipras","html_url":"https://github.com/kipras","followers_url":"https://api.github.com/users/kipras/followers","following_url":"https://api.github.com/users/kipras/following{/other_user}","gists_url":"https://api.github.com/users/kipras/gists{/gist_id}","starred_url":"https://api.github.com/users/kipras/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kipras/subscriptions","organizations_url":"https://api.github.com/users/kipras/orgs","repos_url":"https://api.github.com/users/kipras/repos","events_url":"https://api.github.com/users/kipras/events{/privacy}","received_events_url":"https://api.github.com/users/kipras/received_events","type":"User","site_admin":false},"body":"lua-upstream-nginx-module did not have an API function for retrieving the upstream peer that nginx would choose. Instead you could get a list of IPs (with full configuration) and you have to choose one yourself. So it is up to the user of the API to try to implement it the same way as the nginx version.\r\n\r\nWhich leads to problems. For example if you are using `ip_hash` and you rely on the fact that the same downstream client IPs will always connect to the same upstream origin IPs. And then if you have some of your code connecting to the origin with `proxy_pass` and some of it connecting to the origin with Lua code - you will have issues.\r\n\r\nThe best would be if you could reuse nginx APIs and have nginx return the upstream IP that it would choose. This function does just that. And this way you avoid having to try to replicate nginx features in your Lua code, and eliminate any possible inconsistencies, for good. This also avoids possible future issues if nginx implementation for its upstream peer choosing modules would change.\r\n\r\nNOTE that this function does not explicitly return the IP of a previous `proxy_pass`. For this you could just use `$upstream_addr`. This function is useful when there is no `proxy_pass` and you connect to the upstream from Lua.\r\n\r\nNOTE 2: if you could provide some feedback (e.g. where this could be improved) - that would be great :)","created_at":"2016-06-03T16:18:03Z","updated_at":"2016-06-03T20:25:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"957f933ed9a81f535880dc45d1a42c7a43319905","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25/commits","review_comments_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25/comments","review_comment_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues/25/comments","statuses_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/statuses/e08678790c97390030c00274a5d7c9a714614512","head":{"label":"zenedge:get_chosen_peer","ref":"get_chosen_peer","sha":"e08678790c97390030c00274a5d7c9a714614512","user":{"login":"zenedge","id":5904993,"avatar_url":"https://avatars.githubusercontent.com/u/5904993?v=3","gravatar_id":"","url":"https://api.github.com/users/zenedge","html_url":"https://github.com/zenedge","followers_url":"https://api.github.com/users/zenedge/followers","following_url":"https://api.github.com/users/zenedge/following{/other_user}","gists_url":"https://api.github.com/users/zenedge/gists{/gist_id}","starred_url":"https://api.github.com/users/zenedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zenedge/subscriptions","organizations_url":"https://api.github.com/users/zenedge/orgs","repos_url":"https://api.github.com/users/zenedge/repos","events_url":"https://api.github.com/users/zenedge/events{/privacy}","received_events_url":"https://api.github.com/users/zenedge/received_events","type":"Organization","site_admin":false},"repo":{"id":60359577,"name":"lua-upstream-nginx-module","full_name":"zenedge/lua-upstream-nginx-module","owner":{"login":"zenedge","id":5904993,"avatar_url":"https://avatars.githubusercontent.com/u/5904993?v=3","gravatar_id":"","url":"https://api.github.com/users/zenedge","html_url":"https://github.com/zenedge","followers_url":"https://api.github.com/users/zenedge/followers","following_url":"https://api.github.com/users/zenedge/following{/other_user}","gists_url":"https://api.github.com/users/zenedge/gists{/gist_id}","starred_url":"https://api.github.com/users/zenedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zenedge/subscriptions","organizations_url":"https://api.github.com/users/zenedge/orgs","repos_url":"https://api.github.com/users/zenedge/repos","events_url":"https://api.github.com/users/zenedge/events{/privacy}","received_events_url":"https://api.github.com/users/zenedge/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/zenedge/lua-upstream-nginx-module","description":"Nginx C module to expose Lua API to ngx_lua for Nginx upstreams","fork":true,"url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module","forks_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/forks","keys_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/keys{/key_id}","collaborators_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/teams","hooks_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/hooks","issue_events_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/issues/events{/number}","events_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/events","assignees_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/assignees{/user}","branches_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/branches{/branch}","tags_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/tags","blobs_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/git/refs{/sha}","trees_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/git/trees{/sha}","statuses_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/statuses/{sha}","languages_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/languages","stargazers_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/stargazers","contributors_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/contributors","subscribers_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/subscribers","subscription_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/subscription","commits_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/commits{/sha}","git_commits_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/git/commits{/sha}","comments_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/comments{/number}","issue_comment_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/issues/comments{/number}","contents_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/contents/{+path}","compare_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/compare/{base}...{head}","merges_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/merges","archive_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/downloads","issues_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/issues{/number}","pulls_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/pulls{/number}","milestones_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/milestones{/number}","notifications_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/labels{/name}","releases_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/releases{/id}","deployments_url":"https://api.github.com/repos/zenedge/lua-upstream-nginx-module/deployments","created_at":"2016-06-03T15:48:39Z","updated_at":"2016-06-03T15:48:41Z","pushed_at":"2016-06-03T16:06:04Z","git_url":"git://github.com/zenedge/lua-upstream-nginx-module.git","ssh_url":"git@github.com:zenedge/lua-upstream-nginx-module.git","clone_url":"https://github.com/zenedge/lua-upstream-nginx-module.git","svn_url":"https://github.com/zenedge/lua-upstream-nginx-module","homepage":null,"size":46,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"openresty:master","ref":"master","sha":"1608c198bd7e0b68a9fc77b2da8cec2b86236805","user":{"login":"openresty","id":7390180,"avatar_url":"https://avatars.githubusercontent.com/u/7390180?v=3","gravatar_id":"","url":"https://api.github.com/users/openresty","html_url":"https://github.com/openresty","followers_url":"https://api.github.com/users/openresty/followers","following_url":"https://api.github.com/users/openresty/following{/other_user}","gists_url":"https://api.github.com/users/openresty/gists{/gist_id}","starred_url":"https://api.github.com/users/openresty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openresty/subscriptions","organizations_url":"https://api.github.com/users/openresty/orgs","repos_url":"https://api.github.com/users/openresty/repos","events_url":"https://api.github.com/users/openresty/events{/privacy}","received_events_url":"https://api.github.com/users/openresty/received_events","type":"Organization","site_admin":false},"repo":{"id":15853845,"name":"lua-upstream-nginx-module","full_name":"openresty/lua-upstream-nginx-module","owner":{"login":"openresty","id":7390180,"avatar_url":"https://avatars.githubusercontent.com/u/7390180?v=3","gravatar_id":"","url":"https://api.github.com/users/openresty","html_url":"https://github.com/openresty","followers_url":"https://api.github.com/users/openresty/followers","following_url":"https://api.github.com/users/openresty/following{/other_user}","gists_url":"https://api.github.com/users/openresty/gists{/gist_id}","starred_url":"https://api.github.com/users/openresty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openresty/subscriptions","organizations_url":"https://api.github.com/users/openresty/orgs","repos_url":"https://api.github.com/users/openresty/repos","events_url":"https://api.github.com/users/openresty/events{/privacy}","received_events_url":"https://api.github.com/users/openresty/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openresty/lua-upstream-nginx-module","description":"Nginx C module to expose Lua API to ngx_lua for Nginx upstreams","fork":false,"url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module","forks_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/forks","keys_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/teams","hooks_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/hooks","issue_events_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues/events{/number}","events_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/events","assignees_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/assignees{/user}","branches_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/branches{/branch}","tags_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/tags","blobs_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/git/refs{/sha}","trees_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/statuses/{sha}","languages_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/languages","stargazers_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/stargazers","contributors_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/contributors","subscribers_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/subscribers","subscription_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/subscription","commits_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/commits{/sha}","git_commits_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/git/commits{/sha}","comments_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/comments{/number}","issue_comment_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues/comments{/number}","contents_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/contents/{+path}","compare_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/merges","archive_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/downloads","issues_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues{/number}","pulls_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls{/number}","milestones_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/milestones{/number}","notifications_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/labels{/name}","releases_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/releases{/id}","deployments_url":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/deployments","created_at":"2014-01-13T00:14:19Z","updated_at":"2016-05-31T18:30:24Z","pushed_at":"2016-06-03T16:18:04Z","git_url":"git://github.com/openresty/lua-upstream-nginx-module.git","ssh_url":"git@github.com:openresty/lua-upstream-nginx-module.git","clone_url":"https://github.com/openresty/lua-upstream-nginx-module.git","svn_url":"https://github.com/openresty/lua-upstream-nginx-module","homepage":null,"size":120,"stargazers_count":178,"watchers_count":178,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":68,"mirror_url":null,"open_issues_count":12,"forks":68,"open_issues":12,"watchers":178,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25"},"html":{"href":"https://github.com/openresty/lua-upstream-nginx-module/pull/25"},"issue":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues/25"},"comments":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/issues/25/comments"},"review_comments":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25/comments"},"review_comment":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/pulls/25/commits"},"statuses":{"href":"https://api.github.com/repos/openresty/lua-upstream-nginx-module/statuses/e08678790c97390030c00274a5d7c9a714614512"}}}}
{"actor":{"display_login":"agentzh"}}