Kong / lua-resty-dns-client

Lua DNS client, load balancer, and utility library
http://kong.github.io/lua-resty-dns-client/topics/README.md.html
Apache License 2.0
152 stars 52 forks source link

consistent_hashing:getPeer may skip one loop on same condition #110

Open wuding1104 opened 3 years ago

wuding1104 commented 3 years ago

As the code say start at line 185 (consistent_hashing.lua) :

  local index = handle.hashValue
  local ip, port, hostname
  while (index - 1) ~= handle.hashValue do
    if index == 0 then
      index = self.points
    end

    address = self.continuum[index]
    if address ~= nil and address.available and not address.disabled then
      ...
    end

    index = index - 1
  end

Think about one condition: handle.hashValue = 1, self.points=3 and only self.continuum[2] is available, the code will skip index=2 and return nil.

Beside, when handle.hashValue = 2, self.points=3 and all self.continuum is not available, the code will loop forever!!! I don't know whether the value of self.healthy is false which can prevent this happen at line 164 on this condition.