You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/12/05 03:27:25 UTC

[GitHub] [apisix] tzssangglass commented on a diff in pull request #8380: feat: add consul discovery module

tzssangglass commented on code in PR #8380:
URL: https://github.com/apache/apisix/pull/8380#discussion_r1039112336


##########
apisix/discovery/consul/init.lua:
##########
@@ -0,0 +1,416 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local require            = require
+local local_conf         = require("apisix.core.config_local").local_conf()
+local core               = require("apisix.core")
+local core_sleep         = require("apisix.core.utils").sleep
+local resty_consul       = require('resty.consul')
+local http               = require('resty.http')
+local util               = require("apisix.cli.util")
+local ipairs             = ipairs
+local error              = error
+local ngx                = ngx
+local unpack             = unpack
+local tonumber           = tonumber
+local pairs              = pairs
+local ngx_timer_at       = ngx.timer.at
+local ngx_timer_every    = ngx.timer.every
+local log                = core.log
+local json_delay_encode  = core.json.delay_encode
+
+local all_services = core.table.new(0, 5)
+local default_service
+local default_weight
+local skip_service_map = core.table.new(0, 1)
+local dump_params
+
+local events
+local events_list
+local consul_services
+
+local _M = {
+    version = 0.1,
+}
+
+
+local function discovery_consul_callback(data, event, source, pid)
+    all_services = data
+    log.notice("update local variable all_services, event is: ", event,
+            "source: ", source, "server pid:", pid,
+            ", all services: ", json_delay_encode(all_services, true))
+end
+
+
+function _M.all_nodes()
+    return all_services
+end
+
+
+function _M.nodes(service_name)
+    if not all_services then
+        log.error("all_services is nil, failed to fetch nodes for : ", service_name)
+        return
+    end
+
+    local resp_list = all_services[service_name]
+
+    if not resp_list then
+        log.error("fetch nodes failed by ", service_name, ", return default service")
+        return default_service and {default_service}
+    end
+
+    log.info("process id: ", ngx.worker.id(), ", all_services[", service_name, "] = ",
+            json_delay_encode(resp_list, true))

Review Comment:
   it is better to use
   
   ```
   local ngx_worker_id = ngx.worker.id
   
   ...
   
   log.info("process id: ", ngx_worker_id(), ", all_services[", service_name, "] = ",
               json_delay_encode(resp_list, true))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org