yongboy commented on a change in pull request #3615:
URL: https://github.com/apache/apisix/pull/3615#discussion_r580719261



##########
File path: apisix/discovery/consul_kv.lua
##########
@@ -0,0 +1,415 @@
+--
+-- 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 resty_consul       = require('resty.consul')
+local cjson              = require('cjson')
+local http               = require('resty.http')
+local ipairs             = ipairs
+local error              = error
+local ngx                = ngx
+local unpack             = unpack
+local ngx_re_match      = ngx.re.match
+local tonumber           = tonumber
+local pairs              = pairs
+local ipairs             = ipairs
+local ngx_timer_at       = ngx.timer.at
+local ngx_timer_every    = ngx.timer.every
+local log                = core.log
+local ngx_decode_base64  = ngx.decode_base64
+local json_delay_encode  = core.json.delay_encode
+local cjson_null          = cjson.null
+
+local applications = core.table.new(0, 5)
+local default_service
+local default_weight
+local default_prefix_rule
+local skip_keys_map = core.table.new(0, 1)
+
+local events
+local events_list
+local consul_apps
+
+local schema = {
+    type = "object",
+    properties = {
+        servers = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string",
+            }
+        },
+        fetch_interval = {type = "integer", minimum = 1, default = 3},
+        keepalive = {
+            type = "boolean",
+            default = true
+        },
+        prefix = {type = "string", default = "upstreams"},
+        weight = {type = "integer", minimum = 1, default = 1},
+        timeout = {
+            type = "object",
+            properties = {
+                connect = {type = "integer", minimum = 1, default = 2000},
+                read = {type = "integer", minimum = 1, default = 2000},
+                wait = {type = "integer", minimum = 1, default = 60}
+            },
+            default = {
+                connect = 2000,
+                read = 2000,
+                wait = 60,
+            }
+        },
+        skip_keys = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string",
+            }
+        },
+        default_service = {
+            type = "object",
+            properties = {
+                host = {type = "string"},
+                port = {type = "integer"},
+                metadata = {
+                    type = "object",
+                    properties = {
+                        fail_timeout = {type = "integer", default = 1},
+                        weigth = {type = "integer", default = 1},
+                        max_fails = {type = "integer", default = 1}
+                    },
+                    default = {
+                        fail_timeout = 1,
+                        weigth = 1,
+                        max_fails = 1
+                    }
+                }
+            }
+        }
+    },
+
+    required = {"servers"}
+}
+
+local _M = {
+    version = 0.3,
+}
+
+
+local function discovery_consul_callback(data, event, source, pid)
+    applications = data
+    log.notice("update local variable application, event is: ", event,
+        "source: ", source, "server pid:", pid,
+        ", application: ", core.json.encode(applications, true))
+end
+
+
+function _M.all_nodes()
+    return applications
+end
+
+
+function _M.nodes(service_name)
+    if not applications then
+        log.error("application is nil, failed to fetch nodes for : ", 
service_name)
+        return
+    end
+
+    local resp_list = applications[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(), ", applications[", service_name, 
"] = ",
+        json_delay_encode(resp_list, true))
+
+    return resp_list
+end
+
+
+local function parse_instance(node, server_name_prefix)
+    local key = node.Key
+
+    if key == cjson_null or not key or #key == 0 then
+        log.error("consul_key_empty, server_name_prefix: ", server_name_prefix,
+            ", node: ", json_delay_encode(node, true))
+        return false
+    end
+
+    local result = ngx_re_match(key, default_prefix_rule)

Review comment:
       Good idea!




----------------------------------------------------------------
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.

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


Reply via email to