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 2021/02/20 12:04:54 UTC

[GitHub] [apisix] tokers commented on a change in pull request #3615: feat: add consul kv discovery module

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



##########
File path: apisix/discovery/consul_kv.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 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 string_gmatch      = string.gmatch
+local tonumber           = tonumber
+local pairs              = pairs
+local ipairs             = ipairs
+local ngx_timer_at       = ngx.timer.at
+local log                = core.log
+local ngx_decode_base64  = ngx.decode_base64
+
+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",
+            }
+        },
+        delay = {type = "integer", minimum = 1, default = 3},
+        connect_type = {
+            type = "string",
+            enum = {"long", "short"},
+            default = "long"
+        },
+        prefix = {type = "string", default = "upstreams"},
+        weight = {type = "integer", minimum = 0, default = 1},
+        timeout = {
+            type = "object",
+            properties = {
+                connect = {type = "integer", minimum = 1, default = 2000},

Review comment:
       Maybe use seconds as the unit is better here.

##########
File path: apisix/discovery/consul_kv.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 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 string_gmatch      = string.gmatch
+local tonumber           = tonumber
+local pairs              = pairs
+local ipairs             = ipairs
+local ngx_timer_at       = ngx.timer.at
+local log                = core.log
+local ngx_decode_base64  = ngx.decode_base64
+
+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",
+            }
+        },
+        delay = {type = "integer", minimum = 1, default = 3},
+        connect_type = {

Review comment:
       Why not using `keepalive`, which is more semantic.
   ```suggestion
           keepalive = {
               type = "boolean",
               default = true,
           }
   ```

##########
File path: apisix/discovery/consul_kv.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 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 string_gmatch      = string.gmatch
+local tonumber           = tonumber
+local pairs              = pairs
+local ipairs             = ipairs
+local ngx_timer_at       = ngx.timer.at
+local log                = core.log
+local ngx_decode_base64  = ngx.decode_base64
+
+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",
+            }
+        },
+        delay = {type = "integer", minimum = 1, default = 3},
+        connect_type = {
+            type = "string",
+            enum = {"long", "short"},
+            default = "long"
+        },
+        prefix = {type = "string", default = "upstreams"},
+        weight = {type = "integer", minimum = 0, default = 1},
+        timeout = {
+            type = "object",
+            properties = {
+                connect = {type = "integer", minimum = 1, default = 2000},
+                send = {type = "integer", minimum = 1, default = 2000},
+                read = {type = "integer", minimum = 1, default = 2000}
+            },
+            default = {
+                connect = 2000,
+                send = 2000,
+                read = 2000
+            }
+        },
+        skip_keys = {
+            type = "array",
+            minItems = 1,
+            items = {
+                type = "string",
+            }
+        },
+        default_server = {
+            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 = {
+                    connect = 1,
+                    send = 1,
+                    read = 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, "] = ",
+        core.json.delay_encode(resp_list, true))
+
+    return resp_list
+end
+
+
+local function parse_instance(node, server_name_prefix)
+    local cj = cjson
+    local key = node.Key
+    local pr = default_prefix_rule
+
+    if key == cj.null or not key or #key == 0 then

Review comment:
       Just localize the `cjson.null` to a variable in the module level is OK.




----------------------------------------------------------------
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:
users@infra.apache.org