Kong / kong-pongo

Tooling to run plugin tests with Kong and Kong Enterprise
Apache License 2.0
153 stars 54 forks source link

Redis connect always takes 20 seconds #315

Open niaomingjian opened 2 years ago

niaomingjian commented 2 years ago

Enabled the redis in pongo. Tested the following simple code. Redis connect always takes 20 seconds.

local helpers = require "spec.helpers"
local redis = require "resty.redis"

local PLUGIN_NAME = "myplugin"

local fmt = string.format
local SLEEP_TIME = 1

local REDIS_HOST = helpers.redis_host  --  host ip : "192.168.28.223" -- SERVICE_NETWORK_NAME alias: "pongo-4956b805-redis.pongo-4956b805"  --  internal ip: 192.168.32.4
local REDIS_PORT = 6379
local REDIS_DB_1 = 1
local REDIS_DB_2 = 2

for _, strategy in helpers.all_strategies({"postgres"}) do
  describe(PLUGIN_NAME .. ": (access) [#" .. strategy .. "]", function()
    local client

    lazy_setup(function()

      local bp = helpers.get_db_utils(strategy == "off" and "postgres" or strategy, nil, { PLUGIN_NAME })

      -- Inject a test route. No need to create a service, there is a default
      -- service which will echo the request.
      local route1 = bp.routes:insert({
        hosts = { "test1.com" },
      })

      -- start kong
      assert(helpers.start_kong({
        -- set the strategy
        database   = strategy,
        -- use the custom test template to create a local mock server
        nginx_conf = "spec/fixtures/custom_nginx.template",
        -- make sure our plugin gets loaded
        plugins = "bundled," .. PLUGIN_NAME,
        -- write & load declarative config, only if 'strategy=off'
        declarative_config = strategy == "off" and helpers.make_yaml_file() or nil,
      }))
    end)

    lazy_teardown(function()
      helpers.stop_kong(nil, true)
    end)

    before_each(function()
      client = helpers.proxy_client()
    end)

    after_each(function()
      if client then client:close() end
    end)

    describe("test", function()
      it("connect redis", function()

        local red = redis:new()
        red:set_timeout(2000)

        finally(function()
          if red then
            red:close()
          end
        end)

        print("connect----", os.date("%Y-%m-%d %H:%M:%S", os.time(os.date("*t"))))
        print(REDIS_HOST)
        assert(red:connect(REDIS_HOST, REDIS_PORT))
        print("select----", os.date("%Y-%m-%d %H:%M:%S", os.time(os.date("*t"))))
        assert(red:select(REDIS_DB_1))
        local size_1 = assert(red:dbsize())

        print("size----", size_1)
        local r = client:get("/status/500", {
          headers = {
            host = "test1.com"
          }
        })

        assert.response(r).has.status(500)
        ngx.sleep(SLEEP_TIME)

      end)
    end)

  end)
end

output:

Kong version: 2.8.1
[==========] Running tests from scanned files.
[----------] Global test environment setup.
[----------] Running tests from /kong-plugin/spec/myplugin/03-redis_spec.lua
2022/09/06 09:38:12 [warn] [Penlight 1.12.0] the contents of module 'pl.text' has moved into 'pl.stringx' (deprecated after 1.11.0, scheduled for removal in 2.0.0)
[ RUN      ] /kong-plugin/spec/myplugin/03-redis_spec.lua @ 57: myplugin: (access) [#postgres] test connect redis
connect----2022-09-06 09:38:48
redis
select----2022-09-06 09:39:08
size----0
[       OK ] /kong-plugin/spec/myplugin/03-redis_spec.lua @ 57: myplugin: (access) [#postgres] test connect redis (21024.36 ms)
[----------] 1 test from /kong-plugin/spec/myplugin/03-redis_spec.lua (57041.46 ms total)

[----------] Global test environment teardown.
[==========] 1 test from 1 test file ran. (57042.39 ms total)
[  PASSED  ] 1 test

If pongo expose and access redis with host ip or internal ip in docker network (like pongo-daa49114), redis connect fast.

Kong version: 2.8.1
[==========] Running tests from scanned files.
[----------] Global test environment setup.
[----------] Running tests from /kong-plugin/spec/myplugin/03-redis_spec.lua
2022/09/06 09:59:11 [warn] [Penlight 1.12.0] the contents of module 'pl.text' has moved into 'pl.stringx' (deprecated after 1.11.0, scheduled for removal in 2.0.0)
[ RUN      ] /kong-plugin/spec/myplugin/03-redis_spec.lua @ 57: myplugin: (access) [#postgres] test connect redis
connect----2022-09-06 09:59:32
192.168.28.223
select----2022-09-06 09:59:32
size----0
[       OK ] /kong-plugin/spec/myplugin/03-redis_spec.lua @ 57: myplugin: (access) [#postgres] test connect redis (1121.69 ms)
[----------] 1 test from /kong-plugin/spec/myplugin/03-redis_spec.lua (22398.11 ms total)

[----------] Global test environment teardown.
[==========] 1 test from 1 test file ran. (22399.30 ms total)
[  PASSED  ] 1 test

Host Centos8

docker version

Server: Docker Engine - Community
 Engine:
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:01:29 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker compose version Docker Compose version v2.6.0

In a windows PC installed docker for desktop with WSL2, redis connect with SERVICE_NETWORK_NAME alias (i.e. pongo-xxxxxxxx-redis.pongo-xxxxxxxx) is fast, but slow in the CentOS machine. connect with service name redis is slow too.

Host Windows10

docker version

Client: Docker Engine - Community
 Cloud integration: 1.0.17
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:02 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:10 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker compose version Docker Compose version v2.0.0-rc.1

Why did redis connect take so much time (with service name redis)? Anybody encountered this case?

niaomingjian commented 2 years ago

Redis can connect fast only if I run pongo in my VirtualBox VM and turn off VM's internet network at the same time. It seems it takes 20 seconds to look up redis if the internet network is open. What's the root cause of this situation?