From 977cfcb4c7c02181f54846a8e0fd3185b0b82e9f Mon Sep 17 00:00:00 2001 From: halfcrazy Date: Mon, 19 Mar 2018 00:31:49 +0800 Subject: [PATCH] add luacheck to lint lua files (#2205) --- .luacheckrc | 2 ++ .travis.yml | 7 +++++++ Makefile | 4 ++++ rootfs/etc/nginx/lua/balancer.lua | 11 ++++++----- 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 .luacheckrc diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 000000000..62aeb1703 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,2 @@ +std = 'ngx_lua' +globals = {'_'} diff --git a/.travis.yml b/.travis.yml index ce5b7c586..8bcfa511a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,11 @@ services: language: go +addons: + apt: + packages: + - luarocks + notifications: email: on_failure: always @@ -32,6 +37,8 @@ jobs: include: - stage: Static Check script: + - sudo luarocks install luacheck + - make luacheck - go get github.com/golang/lint/golint - make verify-all - stage: Coverage diff --git a/Makefile b/Makefile index d804a0c12..6091d2df5 100644 --- a/Makefile +++ b/Makefile @@ -180,6 +180,10 @@ cover: vet: @go vet $(shell go list ${PKG}/... | grep -v vendor) +.PHONY: luacheck +luacheck: + luacheck -q ./rootfs/etc/nginx/lua/ + .PHONY: release release: all-container all-push echo "done" diff --git a/rootfs/etc/nginx/lua/balancer.lua b/rootfs/etc/nginx/lua/balancer.lua index be07405e1..63528908b 100644 --- a/rootfs/etc/nginx/lua/balancer.lua +++ b/rootfs/etc/nginx/lua/balancer.lua @@ -6,11 +6,11 @@ local lrucache = require("resty.lrucache") local resty_lock = require("resty.lock") -- measured in seconds --- for an Nginx worker to pick up the new list of upstream peers +-- for an Nginx worker to pick up the new list of upstream peers -- it will take + BACKENDS_SYNC_INTERVAL local BACKENDS_SYNC_INTERVAL = 1 -ROUND_ROBIN_LOCK_KEY = "round_robin" +local ROUND_ROBIN_LOCK_KEY = "round_robin" local round_robin_state = ngx.shared.round_robin_state @@ -37,8 +37,8 @@ local function balance() -- Round-Robin round_robin_lock:lock(backend_name .. ROUND_ROBIN_LOCK_KEY) - local index = round_robin_state:get(backend_name) - local index, endpoint = next(backend.endpoints, index) + local last_index = round_robin_state:get(backend_name) + local index, endpoint = next(backend.endpoints, last_index) if not index then index = 1 endpoint = backend.endpoints[index] @@ -96,7 +96,8 @@ function _M.call() local host, port = balance() - local ok, err = ngx_balancer.set_current_peer(host, port) + local ok + ok, err = ngx_balancer.set_current_peer(host, port) if ok then ngx.log(ngx.INFO, "current peer is set to " .. host .. ":" .. port) else