orange icon indicating copy to clipboard operation
orange copied to clipboard

consul_balancer插件的设计问题

Open whiteCcinn opened this issue 4 years ago • 2 comments

使用的Orange版本

v0.7.0

需求或场景

我最近在优化consul_balancer的功能,看到一些代码有一些疑惑,希望得到解答。

我看consul_balancer插件中的一段代码,有一个_watch方法,里面有一个while t do的循环体,实际去检测服务健康的方法是在内部的 _check(service_descriptor, service_index)方法,这样子一来岂不是,一直在发起http请求去检测?不知道之前设计这个插件的时候,是不是故意这样子处理的,在我看来可能定期发起健康检查是不是会好一些?还是说我考虑多余了?

假设我考虑是多余的,那么为什么还要加上一段代码:

       local tNow = ostime()
        if tNow > tLast then
            _timer(0, _watch, service_descriptor)
            return
        end

上面这段代码,难道不是应该为了控制请求频率而存在的吗?

使用的插件

consul_balancer

具体的配置
local function _watch(premature, service_descriptor)
  if premature then
    return nil
  end

  local service_index = 0
  local watch_name = "upstream_watch." .. service_descriptor.id
  local t = _aquire_watch(watch_name)
  local t1 = {
    t1 = t.t.dayfrc,
    t2 = t.t.daynum
  }
  ngx.log(ngx.NOTICE, "consul.balancer: started watching for changes in ", service_descriptor.name, "(", t1.t1, ",", t1.t2, ")")

  local tLast = ostime() + 90
  while t do
    local res, err = _check(service_descriptor, service_index)
    if res == nil then
      ngx.log(ngx.ERR, "consul.balancer: failed while watching for changes in ", service_descriptor.name, " retry scheduled")
      _timer(WATCH_RETRY_TIMER, _watch, service_descriptor)
      return nil
    end

    local rules, service, err = _parse_service(res, err)
    if err ~= nil then
        ngx.log(ngx.ERR, "consul.balancer: failed to parse consul response: ", err)
        return nil, nil, err
    end

    if ngx.worker.exiting() then
      ngx.log(ngx.ERR, "be force exiting")
      os.exit()
      return
    end

    -- TODO: Save only newer data from consul to reduce GC load
    service_index = service.index
    t = _aquire_watch(watch_name)                 
    if not t then
      break
    end

    if t1.t1 ~= t.t.dayfrc or t1.t2 ~= t.t.daynum then
      ngx.log(ngx.ERR, service_descriptor.name , " not my time(", t.t.dayfrc, ",", t.t.daynum, ")")
      return nil
    end

    _persist(service_descriptor.id, service)
    _persist_rule(service_descriptor, rules)
    
    if service_descriptor.log_consul then
      ngx.log(ngx.INFO, "consul.balancer: persisted service ", service_descriptor.name, "(", service_descriptor.id, 
                      ") index: ", service_index, " content: ", json.encode(service))
    end
    
    local tNow = ostime()
    if tNow > tLast then
      _timer(0, _watch, service_descriptor)
      return
    end
  end
  
  ngx.log(ngx.ERR, service_descriptor.name , " stop watching(", t1.t1, ",", t1.t2, ")")
end

whiteCcinn avatar Jul 31 '19 09:07 whiteCcinn

刚才再看了一下,好像是和resty.http有关,频率是在consul. interval中设置。

whiteCcinn avatar Jul 31 '19 09:07 whiteCcinn

看上去

       local tNow = ostime()
        if tNow > tLast then
            _timer(0, _watch, service_descriptor)
            return
        end

这段代码,只是为了定期GC而存在。

whiteCcinn avatar Jul 31 '19 09:07 whiteCcinn