upstream-keepalive-pool.t 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. use t::APISIX;
  18. my $nginx_binary = $ENV{'TEST_NGINX_BINARY'} || 'nginx';
  19. my $version = eval { `$nginx_binary -V 2>&1` };
  20. if ($version !~ m/\/apisix-nginx-module/) {
  21. plan(skip_all => "apisix-nginx-module not installed");
  22. } else {
  23. plan('no_plan');
  24. }
  25. repeat_each(1);
  26. log_level('debug');
  27. no_root_location();
  28. no_shuffle();
  29. add_block_preprocessor(sub {
  30. my ($block) = @_;
  31. if (!defined $block->request) {
  32. $block->set_value("request", "GET /t");
  33. }
  34. });
  35. run_tests();
  36. __DATA__
  37. === TEST 1: bad pool size
  38. --- config
  39. location /t {
  40. content_by_lua_block {
  41. local t = require("lib.test_admin").test
  42. local code, body = t('/apisix/admin/upstreams/1',
  43. ngx.HTTP_PUT,
  44. [[{
  45. "type": "roundrobin",
  46. "nodes": {
  47. "127.0.0.1:1983": 1
  48. },
  49. "keepalive_pool": {
  50. "size": 0
  51. }
  52. }]]
  53. )
  54. if code >= 300 then
  55. ngx.status = code
  56. end
  57. ngx.print(body)
  58. }
  59. }
  60. --- error_code: 400
  61. --- response_body
  62. {"error_msg":"invalid configuration: property \"keepalive_pool\" validation failed: property \"size\" validation failed: expected 0 to be at least 1"}
  63. === TEST 2: set route/upstream
  64. --- config
  65. location /t {
  66. content_by_lua_block {
  67. local t = require("lib.test_admin").test
  68. local code, body = t('/apisix/admin/upstreams/1',
  69. ngx.HTTP_PUT,
  70. [[{
  71. "type": "roundrobin",
  72. "nodes": {
  73. "127.0.0.1:1980": 1
  74. },
  75. "keepalive_pool": {
  76. "size": 4,
  77. "idle_timeout": 8,
  78. "requests": 16
  79. }
  80. }]]
  81. )
  82. if code >= 300 then
  83. ngx.status = code
  84. ngx.print(body)
  85. return
  86. end
  87. local code, body = t('/apisix/admin/routes/1',
  88. ngx.HTTP_PUT,
  89. [[{
  90. "uri":"/hello",
  91. "upstream_id": 1
  92. }]])
  93. if code >= 300 then
  94. ngx.status = code
  95. end
  96. ngx.print(body)
  97. }
  98. }
  99. === TEST 3: hit
  100. --- config
  101. location /t {
  102. content_by_lua_block {
  103. local http = require "resty.http"
  104. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  105. .. "/hello"
  106. for i = 1, 3 do
  107. local httpc = http.new()
  108. local res, err = httpc:request_uri(uri)
  109. if not res then
  110. ngx.say(err)
  111. return
  112. end
  113. ngx.print(res.body)
  114. end
  115. }
  116. }
  117. --- response_body
  118. hello world
  119. hello world
  120. hello world
  121. --- grep_error_log eval
  122. qr/lua balancer: keepalive .*/
  123. --- grep_error_log_out eval
  124. qr/^lua balancer: keepalive create pool, crc32: \S+, size: 4
  125. lua balancer: keepalive no free connection, cpool: \S+
  126. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  127. lua balancer: keepalive reusing connection \S+, requests: 1, cpool: \S+
  128. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  129. lua balancer: keepalive reusing connection \S+, requests: 2, cpool: \S+
  130. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  131. $/
  132. === TEST 4: only reuse one time
  133. --- config
  134. location /t {
  135. content_by_lua_block {
  136. local t = require("lib.test_admin").test
  137. local code, body = t('/apisix/admin/upstreams/1',
  138. ngx.HTTP_PUT,
  139. [[{
  140. "type": "roundrobin",
  141. "nodes": {
  142. "127.0.0.1:1980": 1
  143. },
  144. "keepalive_pool": {
  145. "size": 1,
  146. "idle_timeout": 8,
  147. "requests": 2
  148. }
  149. }]]
  150. )
  151. if code >= 300 then
  152. ngx.status = code
  153. end
  154. ngx.print(body)
  155. }
  156. }
  157. === TEST 5: hit
  158. --- config
  159. location /t {
  160. content_by_lua_block {
  161. local http = require "resty.http"
  162. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  163. .. "/hello"
  164. for i = 1, 3 do
  165. local httpc = http.new()
  166. local res, err = httpc:request_uri(uri)
  167. if not res then
  168. ngx.say(err)
  169. return
  170. end
  171. ngx.print(res.body)
  172. end
  173. }
  174. }
  175. --- response_body
  176. hello world
  177. hello world
  178. hello world
  179. --- grep_error_log eval
  180. qr/lua balancer: keepalive .*/
  181. --- grep_error_log_out eval
  182. qr/^lua balancer: keepalive create pool, crc32: \S+, size: 1
  183. lua balancer: keepalive no free connection, cpool: \S+
  184. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  185. lua balancer: keepalive reusing connection \S+, requests: 1, cpool: \S+
  186. lua balancer: keepalive not saving connection \S+, cpool: \S+, connections: 0
  187. lua balancer: keepalive free pool \S+, crc32: \S+
  188. lua balancer: keepalive create pool, crc32: \S+, size: 1
  189. lua balancer: keepalive no free connection, cpool: \S+
  190. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  191. $/
  192. === TEST 6: set upstream without keepalive_pool
  193. --- config
  194. location /t {
  195. content_by_lua_block {
  196. local t = require("lib.test_admin").test
  197. local code, body = t('/apisix/admin/upstreams/1',
  198. ngx.HTTP_PUT,
  199. [[{
  200. "type": "roundrobin",
  201. "nodes": {
  202. "127.0.0.1:1980": 1
  203. }
  204. }]]
  205. )
  206. if code >= 300 then
  207. ngx.status = code
  208. ngx.print(body)
  209. return
  210. end
  211. }
  212. }
  213. === TEST 7: should not override default value
  214. --- config
  215. location /t {
  216. content_by_lua_block {
  217. local http = require "resty.http"
  218. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  219. .. "/hello"
  220. for i = 1, 3 do
  221. local httpc = http.new()
  222. local res, err = httpc:request_uri(uri)
  223. if not res then
  224. ngx.say(err)
  225. return
  226. end
  227. ngx.print(res.body)
  228. end
  229. }
  230. }
  231. --- response_body
  232. hello world
  233. hello world
  234. hello world
  235. --- grep_error_log eval
  236. qr/lua balancer: keepalive .*/
  237. --- grep_error_log_out eval
  238. qr/^lua balancer: keepalive create pool, crc32: \S+, size: 320
  239. lua balancer: keepalive no free connection, cpool: \S+
  240. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  241. lua balancer: keepalive reusing connection \S+, requests: 1, cpool: \S+
  242. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  243. lua balancer: keepalive reusing connection \S+, requests: 2, cpool: \S+
  244. lua balancer: keepalive saving connection \S+, cpool: \S+, connections: 1
  245. $/
  246. === TEST 8: upstreams with different client cert
  247. --- config
  248. location /t {
  249. content_by_lua_block {
  250. local t = require("lib.test_admin")
  251. local test = require("lib.test_admin").test
  252. local json = require("toolkit.json")
  253. local ssl_cert = t.read_file("t/certs/mtls_client.crt")
  254. local ssl_key = t.read_file("t/certs/mtls_client.key")
  255. local ssl_cert2 = t.read_file("t/certs/apisix.crt")
  256. local ssl_key2 = t.read_file("t/certs/apisix.key")
  257. local code, body = test('/apisix/admin/upstreams/1',
  258. ngx.HTTP_PUT,
  259. [[{
  260. "scheme": "https",
  261. "type": "roundrobin",
  262. "nodes": {
  263. "127.0.0.1:1983": 1
  264. },
  265. "keepalive_pool": {
  266. "size": 4
  267. }
  268. }]]
  269. )
  270. if code >= 300 then
  271. ngx.status = code
  272. ngx.print(body)
  273. return
  274. end
  275. local data = {
  276. scheme = "https",
  277. type = "roundrobin",
  278. nodes = {
  279. ["127.0.0.1:1983"] = 1,
  280. },
  281. tls = {
  282. client_cert = ssl_cert,
  283. client_key = ssl_key,
  284. },
  285. keepalive_pool = {
  286. size = 8
  287. }
  288. }
  289. local code, body = test('/apisix/admin/upstreams/2',
  290. ngx.HTTP_PUT,
  291. json.encode(data)
  292. )
  293. if code >= 300 then
  294. ngx.status = code
  295. ngx.print(body)
  296. return
  297. end
  298. local data = {
  299. scheme = "https",
  300. type = "roundrobin",
  301. nodes = {
  302. ["127.0.0.1:1983"] = 1,
  303. },
  304. tls = {
  305. client_cert = ssl_cert2,
  306. client_key = ssl_key2,
  307. },
  308. keepalive_pool = {
  309. size = 16
  310. }
  311. }
  312. local code, body = test('/apisix/admin/upstreams/3',
  313. ngx.HTTP_PUT,
  314. json.encode(data)
  315. )
  316. if code >= 300 then
  317. ngx.status = code
  318. ngx.print(body)
  319. return
  320. end
  321. for i = 1, 3 do
  322. local code, body = test('/apisix/admin/routes/' .. i,
  323. ngx.HTTP_PUT,
  324. [[{
  325. "uri":"/hello/]] .. i .. [[",
  326. "plugins": {
  327. "proxy-rewrite": {
  328. "uri": "/hello"
  329. }
  330. },
  331. "upstream_id": ]] .. i .. [[
  332. }]])
  333. if code >= 300 then
  334. ngx.status = code
  335. ngx.print(body)
  336. return
  337. end
  338. end
  339. }
  340. }
  341. --- response_body
  342. === TEST 9: hit
  343. --- upstream_server_config
  344. ssl_client_certificate ../../certs/mtls_ca.crt;
  345. ssl_verify_client on;
  346. --- config
  347. location /t {
  348. content_by_lua_block {
  349. local http = require "resty.http"
  350. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  351. for i = 1, 12 do
  352. local idx = (i % 3) + 1
  353. local httpc = http.new()
  354. local res, err = httpc:request_uri(uri .. "/hello/" .. idx)
  355. if not res then
  356. ngx.say(err)
  357. return
  358. end
  359. if idx == 2 then
  360. assert(res.status == 200)
  361. else
  362. assert(res.status == 400)
  363. end
  364. end
  365. }
  366. }
  367. === TEST 10: upstreams with different client cert (without pool)
  368. --- config
  369. location /t {
  370. content_by_lua_block {
  371. local t = require("lib.test_admin")
  372. local test = require("lib.test_admin").test
  373. local json = require("toolkit.json")
  374. local ssl_cert = t.read_file("t/certs/mtls_client.crt")
  375. local ssl_key = t.read_file("t/certs/mtls_client.key")
  376. local ssl_cert2 = t.read_file("t/certs/apisix.crt")
  377. local ssl_key2 = t.read_file("t/certs/apisix.key")
  378. local code, body = test('/apisix/admin/upstreams/1',
  379. ngx.HTTP_PUT,
  380. [[{
  381. "scheme": "https",
  382. "type": "roundrobin",
  383. "nodes": {
  384. "127.0.0.1:1983": 1
  385. }
  386. }]]
  387. )
  388. if code >= 300 then
  389. ngx.status = code
  390. ngx.print(body)
  391. return
  392. end
  393. local data = {
  394. scheme = "https",
  395. type = "roundrobin",
  396. nodes = {
  397. ["127.0.0.1:1983"] = 1,
  398. },
  399. tls = {
  400. client_cert = ssl_cert,
  401. client_key = ssl_key,
  402. }
  403. }
  404. local code, body = test('/apisix/admin/upstreams/2',
  405. ngx.HTTP_PUT,
  406. json.encode(data)
  407. )
  408. if code >= 300 then
  409. ngx.status = code
  410. ngx.print(body)
  411. return
  412. end
  413. local data = {
  414. scheme = "https",
  415. type = "roundrobin",
  416. nodes = {
  417. ["127.0.0.1:1983"] = 1,
  418. },
  419. tls = {
  420. client_cert = ssl_cert2,
  421. client_key = ssl_key2,
  422. }
  423. }
  424. local code, body = test('/apisix/admin/upstreams/3',
  425. ngx.HTTP_PUT,
  426. json.encode(data)
  427. )
  428. if code >= 300 then
  429. ngx.status = code
  430. ngx.print(body)
  431. return
  432. end
  433. for i = 1, 3 do
  434. local code, body = test('/apisix/admin/routes/' .. i,
  435. ngx.HTTP_PUT,
  436. [[{
  437. "uri":"/hello/]] .. i .. [[",
  438. "plugins": {
  439. "proxy-rewrite": {
  440. "uri": "/hello"
  441. }
  442. },
  443. "upstream_id": ]] .. i .. [[
  444. }]])
  445. if code >= 300 then
  446. ngx.status = code
  447. ngx.print(body)
  448. return
  449. end
  450. end
  451. }
  452. }
  453. --- response_body
  454. === TEST 11: hit
  455. --- upstream_server_config
  456. ssl_client_certificate ../../certs/mtls_ca.crt;
  457. ssl_verify_client on;
  458. --- config
  459. location /t {
  460. content_by_lua_block {
  461. local http = require "resty.http"
  462. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  463. for i = 1, 12 do
  464. local idx = (i % 3) + 1
  465. local httpc = http.new()
  466. local res, err = httpc:request_uri(uri .. "/hello/" .. idx)
  467. if not res then
  468. ngx.say(err)
  469. return
  470. end
  471. if idx == 2 then
  472. assert(res.status == 200)
  473. else
  474. assert(res.status == 400)
  475. end
  476. end
  477. }
  478. }
  479. === TEST 12: upstreams with different SNI
  480. --- config
  481. location /t {
  482. content_by_lua_block {
  483. local t = require("lib.test_admin")
  484. local test = require("lib.test_admin").test
  485. local json = require("toolkit.json")
  486. local code, body = test('/apisix/admin/upstreams/1',
  487. ngx.HTTP_PUT,
  488. [[{
  489. "scheme": "https",
  490. "type": "roundrobin",
  491. "nodes": {
  492. "127.0.0.1:1983": 1
  493. },
  494. "pass_host": "rewrite",
  495. "upstream_host": "a.com",
  496. "keepalive_pool": {
  497. "size": 4
  498. }
  499. }]]
  500. )
  501. if code >= 300 then
  502. ngx.status = code
  503. ngx.print(body)
  504. return
  505. end
  506. local data = {
  507. scheme = "https",
  508. type = "roundrobin",
  509. nodes = {
  510. ["127.0.0.1:1983"] = 1,
  511. },
  512. pass_host = "rewrite",
  513. upstream_host = "b.com",
  514. keepalive_pool = {
  515. size = 8
  516. }
  517. }
  518. local code, body = test('/apisix/admin/upstreams/2',
  519. ngx.HTTP_PUT,
  520. json.encode(data)
  521. )
  522. if code >= 300 then
  523. ngx.status = code
  524. ngx.print(body)
  525. return
  526. end
  527. for i = 1, 2 do
  528. local code, body = test('/apisix/admin/routes/' .. i,
  529. ngx.HTTP_PUT,
  530. [[{
  531. "uri":"/hello/]] .. i .. [[",
  532. "plugins": {
  533. "proxy-rewrite": {
  534. "uri": "/hello"
  535. }
  536. },
  537. "upstream_id": ]] .. i .. [[
  538. }]])
  539. if code >= 300 then
  540. ngx.status = code
  541. ngx.print(body)
  542. return
  543. end
  544. end
  545. }
  546. }
  547. --- response_body
  548. === TEST 13: hit
  549. --- config
  550. location /t {
  551. content_by_lua_block {
  552. local http = require "resty.http"
  553. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  554. for i = 1, 4 do
  555. local idx = i % 2 + 1
  556. local httpc = http.new()
  557. local res, err = httpc:request_uri(uri .. "/hello/" .. idx)
  558. local res, err = httpc:request_uri(uri)
  559. if not res then
  560. ngx.say(err)
  561. return
  562. end
  563. ngx.print(res.body)
  564. end
  565. }
  566. }
  567. --- grep_error_log eval
  568. qr/lua balancer: keepalive create pool, .*/
  569. --- grep_error_log_out eval
  570. qr/^lua balancer: keepalive create pool, crc32: \S+, size: 8
  571. lua balancer: keepalive create pool, crc32: \S+, size: 4
  572. $/
  573. === TEST 14: upstreams with SNI, then without SNI
  574. --- config
  575. location /t {
  576. content_by_lua_block {
  577. local t = require("lib.test_admin")
  578. local test = require("lib.test_admin").test
  579. local json = require("toolkit.json")
  580. local code, body = test('/apisix/admin/upstreams/1',
  581. ngx.HTTP_PUT,
  582. [[{
  583. "scheme": "https",
  584. "type": "roundrobin",
  585. "nodes": {
  586. "127.0.0.1:1983": 1
  587. },
  588. "pass_host": "rewrite",
  589. "upstream_host": "a.com",
  590. "keepalive_pool": {
  591. "size": 4
  592. }
  593. }]]
  594. )
  595. if code >= 300 then
  596. ngx.status = code
  597. ngx.print(body)
  598. return
  599. end
  600. local data = {
  601. scheme = "http",
  602. type = "roundrobin",
  603. nodes = {
  604. ["127.0.0.1:1980"] = 1,
  605. },
  606. pass_host = "rewrite",
  607. upstream_host = "b.com",
  608. keepalive_pool = {
  609. size = 8
  610. }
  611. }
  612. local code, body = test('/apisix/admin/upstreams/2',
  613. ngx.HTTP_PUT,
  614. json.encode(data)
  615. )
  616. if code >= 300 then
  617. ngx.status = code
  618. ngx.print(body)
  619. return
  620. end
  621. for i = 1, 2 do
  622. local code, body = test('/apisix/admin/routes/' .. i,
  623. ngx.HTTP_PUT,
  624. [[{
  625. "uri":"/hello/]] .. i .. [[",
  626. "plugins": {
  627. "proxy-rewrite": {
  628. "uri": "/hello"
  629. }
  630. },
  631. "upstream_id": ]] .. i .. [[
  632. }]])
  633. if code >= 300 then
  634. ngx.status = code
  635. ngx.print(body)
  636. return
  637. end
  638. end
  639. }
  640. }
  641. --- response_body
  642. === TEST 15: hit
  643. --- config
  644. location /t {
  645. content_by_lua_block {
  646. local http = require "resty.http"
  647. local uri = "http://127.0.0.1:" .. ngx.var.server_port
  648. for i = 0, 1 do
  649. local idx = i % 2 + 1
  650. local httpc = http.new()
  651. local res, err = httpc:request_uri(uri .. "/hello/" .. idx)
  652. local res, err = httpc:request_uri(uri)
  653. if not res then
  654. ngx.say(err)
  655. return
  656. end
  657. ngx.print(res.body)
  658. end
  659. }
  660. }
  661. --- grep_error_log eval
  662. qr/lua balancer: keepalive create pool, .*/
  663. --- grep_error_log_out eval
  664. qr/^lua balancer: keepalive create pool, crc32: \S+, size: 4
  665. lua balancer: keepalive create pool, crc32: \S+, size: 8
  666. $/
  667. === TEST 16: backend serve http and grpc with the same port
  668. --- config
  669. location /t {
  670. content_by_lua_block {
  671. local t = require("lib.test_admin")
  672. local test = require("lib.test_admin").test
  673. local json = require("toolkit.json")
  674. local data = {
  675. uri = "",
  676. upstream = {
  677. scheme = "",
  678. type = "roundrobin",
  679. nodes = {
  680. ["127.0.0.1:10054"] = 1,
  681. },
  682. keepalive_pool = {
  683. size = 4
  684. }
  685. }
  686. }
  687. data.uri = "/helloworld.Greeter/SayHello"
  688. data.upstream.scheme = "grpc"
  689. local code, body = test('/apisix/admin/routes/1',
  690. ngx.HTTP_PUT,
  691. json.encode(data)
  692. )
  693. if code >= 300 then
  694. ngx.status = code
  695. ngx.print(body)
  696. return
  697. end
  698. data.uri = "/hello"
  699. data.upstream.scheme = "http"
  700. local code, body = test('/apisix/admin/routes/2',
  701. ngx.HTTP_PUT,
  702. json.encode(data)
  703. )
  704. if code >= 300 then
  705. ngx.status = code
  706. ngx.print(body)
  707. return
  708. end
  709. }
  710. }
  711. --- response_body
  712. === TEST 17: hit http
  713. --- request
  714. GET /hello
  715. --- response_body chomp
  716. hello http
  717. === TEST 18: hit grpc
  718. --- http2
  719. --- exec
  720. grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
  721. --- response_body
  722. {
  723. "message": "Hello apisix"
  724. }