Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used cjson.safe instead of pcall. #3674

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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