Skip to content

Commit

Permalink
used cjson.safe instead of pcall.
Browse files Browse the repository at this point in the history
  • Loading branch information
moonming committed Jan 18, 2019
1 parent d968ee9 commit 1d37e83
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local ngx_balancer = require("ngx.balancer")
local json = require("cjson")
local cjson = require("cjson.safe")
local util = require("util")
local dns_util = require("util.dns")
local configuration = require("configuration")
Expand Down Expand Up @@ -114,9 +114,9 @@ local function sync_backends()
return
end

local ok, new_backends = pcall(json.decode, backends_data)
if not ok then
ngx.log(ngx.ERR, "could not parse backends data: " .. tostring(new_backends))
local new_backends, err = cjson.decode(backends_data)
if not new_backends then
ngx.log(ngx.ERR, "could not parse backends data: ", err)
return
end

Expand Down
11 changes: 6 additions & 5 deletions rootfs/etc/nginx/lua/configuration.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local json = require("cjson")
local cjson = require("cjson.safe")

-- this is the Lua representation of Configuration struct in internal/ingress/types.go
local configuration_data = ngx.shared.configuration_data
Expand Down Expand Up @@ -49,17 +49,18 @@ local function handle_servers()

local raw_servers = fetch_request_body()

local ok, servers = pcall(json.decode, raw_servers)
if not ok then
ngx.log(ngx.ERR, "could not parse servers: " .. tostring(servers))
local servers, err = cjson.decode(raw_servers)
if not servers then
ngx.log(ngx.ERR, "could not parse servers: ", err)
ngx.status = ngx.HTTP_BAD_REQUEST
return
end

local err_buf = {}
for _, server in ipairs(servers) do
if server.hostname and server.sslCert.pemCertKey then
local success, err = certificate_data:safe_set(server.hostname, server.sslCert.pemCertKey)
local success
success, err = certificate_data:safe_set(server.hostname, server.sslCert.pemCertKey)
if not success then
if err == "no memory" then
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
Expand Down
8 changes: 4 additions & 4 deletions rootfs/etc/nginx/lua/monitor.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local socket = ngx.socket.tcp
local cjson = require('cjson')
local cjson = require("cjson.safe")
local assert = assert

local metrics_batch = {}
Expand Down Expand Up @@ -49,9 +49,9 @@ local function flush(premature)
local current_metrics_batch = metrics_batch
metrics_batch = {}

local ok, payload = pcall(cjson.encode, current_metrics_batch)
if not ok then
ngx.log(ngx.ERR, "error while encoding metrics: " .. tostring(payload))
local payload, err = cjson.encode(current_metrics_batch)
if not payload then
ngx.log(ngx.ERR, "error while encoding metrics: ", err)
return
end

Expand Down
8 changes: 4 additions & 4 deletions rootfs/etc/nginx/lua/tcp_udp_balancer.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local ngx_balancer = require("ngx.balancer")
local json = require("cjson")
local cjson = require("cjson.safe")
local util = require("util")
local dns_util = require("util.dns")
local configuration = require("tcp_udp_configuration")
Expand Down Expand Up @@ -99,9 +99,9 @@ local function sync_backends()
return
end

local ok, new_backends = pcall(json.decode, backends_data)
if not ok then
ngx.log(ngx.ERR, "could not parse backends data: " .. tostring(new_backends))
local new_backends, err = cjson.decode(backends_data)
if not new_backends then
ngx.log(ngx.ERR, "could not parse backends data: ", err)
return
end

Expand Down

0 comments on commit 1d37e83

Please sign in to comment.