-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(client) reduce amount of timers on init_worker (#130)
* perf(client) reduce amount of timers on init_worker * tests(*) add prove tests for the client and adjust ci for it * tests(*) reusage of timers, independent on # of workers * tests(*) simple refactor * docs(readme) reduce amount of timers on init_worker * docs(readme) add PR link Co-authored-by: Vinicius Mignot <[email protected]>
- Loading branch information
Showing
7 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.DS_store | ||
*.rock | ||
|
||
t/servroot/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use strict; | ||
use warnings FATAL => 'all'; | ||
use Test::Nginx::Socket::Lua; | ||
|
||
plan tests => 2; | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: load lua-resty-dns-client | ||
--- config | ||
location = /t { | ||
access_by_lua_block { | ||
local client = require("resty.dns.client") | ||
assert(client.init()) | ||
local host = "localhost" | ||
local typ = client.TYPE_A | ||
local answers, err = assert(client.resolve(host, { qtype = typ })) | ||
ngx.say(answers[1].address) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
127.0.0.1 | ||
--- no_error_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use Test::Nginx::Socket; | ||
|
||
plan tests => repeat_each() * (blocks() * 5); | ||
|
||
workers(6); | ||
|
||
no_shuffle(); | ||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: client supports access phase | ||
--- config | ||
location = /t { | ||
access_by_lua_block { | ||
local client = require("resty.dns.client") | ||
assert(client.init()) | ||
local host = "localhost" | ||
local typ = client.TYPE_A | ||
local answers, err = client.resolve(host, { qtype = typ }) | ||
if not answers then | ||
ngx.say("failed to resolve: ", err) | ||
end | ||
ngx.say("address name: ", answers[1].name) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
address name: localhost | ||
--- no_error_log | ||
[error] | ||
dns lookup pool exceeded retries | ||
API disabled in the context of init_worker_by_lua | ||
|
||
|
||
|
||
=== TEST 2: client does not support init_worker phase | ||
--- http_config eval | ||
qq { | ||
init_worker_by_lua_block { | ||
local client = require("resty.dns.client") | ||
assert(client.init()) | ||
local host = "konghq.com" | ||
local typ = client.TYPE_A | ||
answers, err = client.resolve(host, { qtype = typ }) | ||
} | ||
} | ||
--- config | ||
location = /t { | ||
access_by_lua_block { | ||
ngx.say("answers: ", answers) | ||
ngx.say("err: ", err) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
answers: nil | ||
err: dns client error: 101 empty record received | ||
--- no_error_log | ||
[error] | ||
dns lookup pool exceeded retries | ||
API disabled in the context of init_worker_by_lua |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use Test::Nginx::Socket; | ||
|
||
plan tests => repeat_each() * (blocks() * 5); | ||
|
||
workers(6); | ||
|
||
no_shuffle(); | ||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: reuse timers for queries of same name, independent on # of workers | ||
--- http_config eval | ||
qq { | ||
init_worker_by_lua_block { | ||
local client = require("resty.dns.client") | ||
assert(client.init({ | ||
nameservers = { "8.8.8.8" }, | ||
hosts = {}, -- empty tables to parse to prevent defaulting to /etc/hosts | ||
resolvConf = {}, -- and resolv.conf files | ||
order = { "A" }, | ||
})) | ||
local host = "httpbin.org" | ||
local typ = client.TYPE_A | ||
for i = 1, 10 do | ||
client.resolve(host, { qtype = typ }) | ||
end | ||
local host = "mockbin.org" | ||
for i = 1, 10 do | ||
client.resolve(host, { qtype = typ }) | ||
end | ||
workers = ngx.worker.count() | ||
timers = ngx.timer.pending_count() | ||
} | ||
} | ||
--- config | ||
location = /t { | ||
access_by_lua_block { | ||
local client = require("resty.dns.client") | ||
assert(client.init()) | ||
local host = "httpbin.org" | ||
local typ = client.TYPE_A | ||
local answers, err = client.resolve(host, { qtype = typ }) | ||
if not answers then | ||
ngx.say("failed to resolve: ", err) | ||
end | ||
ngx.say("first address name: ", answers[1].name) | ||
host = "mockbin.org" | ||
answers, err = client.resolve(host, { qtype = typ }) | ||
if not answers then | ||
ngx.say("failed to resolve: ", err) | ||
end | ||
ngx.say("second address name: ", answers[1].name) | ||
ngx.say("workers: ", workers) | ||
-- should be 2 timers maximum (1 for each hostname) | ||
ngx.say("timers: ", timers) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
first address name: httpbin.org | ||
second address name: mockbin.org | ||
workers: 6 | ||
timers: 2 | ||
--- no_error_log | ||
[error] | ||
dns lookup pool exceeded retries | ||
API disabled in the context of init_worker_by_lua |