diff --git a/src/init.lua b/src/init.lua index daf2ffe..3064a5c 100755 --- a/src/init.lua +++ b/src/init.lua @@ -13,38 +13,38 @@ local server_state_check_timer -- sync api proxy infos from mysql api_sync_timer = function(premature) - if not premature then - local ret, result=pcall(agw_service.syncApiInfo) - if ret then - log(DEBUG, "sync api info over") - else - log(ERR, "sync api info failed:", result) - end - local ok, err = new_timer(api_sync_interval, api_sync_timer) - end + if not premature then + local ret, result = pcall(agw_service.syncApiInfo) + if ret then + log(DEBUG, "sync api info over") + else + log(ERR, "sync api info failed:", result) + end + local ok, err = new_timer(api_sync_interval, api_sync_timer) + end end -- check servers' status server_state_check_timer = function(premature) - if not premature then - local ret, result = pcall(agw_service.checkState) - if ret then - log(DEBUG, "server state check over") - else - log(ERR, "error occured when check server state:", result) - end - local ok, err = new_timer(server_state_check_interval, server_state_check_timer) - end + if not premature then + local ret, result = pcall(agw_service.checkState) + if ret then + log(DEBUG, "server state check over") + else + log(ERR, "error occured when check server state:", result) + end + local ok, err = new_timer(server_state_check_interval, server_state_check_timer) + end end local ok, err = new_timer(api_sync_interval, api_sync_timer) if not ok then - log(ERR, "failed to create timer: ", err) - return + log(ERR, "failed to create timer: ", err) + return end ok, err = new_timer(server_state_check_interval, server_state_check_timer) if not ok then - log(ERR, "failed to create timer: ", err) - return + log(ERR, "failed to create timer: ", err) + return end diff --git a/src/model/api.lua b/src/model/api.lua index 5187d74..a698df5 100755 --- a/src/model/api.lua +++ b/src/model/api.lua @@ -4,30 +4,35 @@ local api_model = {} function api_model.getApis() local db = mysql.getDb() local apis, err, errno, sqlstate = db:query("select * from agw_api", 10) + db:set_keepalive(10000, 100) return apis, err end function api_model.add(service_id, request_uri, original_uri, description) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("INSERT INTO agw_api (service_id,request_uri,original_uri,description)values(\'"..service_id.."\',\'"..request_uri.."\',\'"..original_uri.."\',\'"..description.."\')", 10) + db:set_keepalive(10000, 100) return res, err end function api_model.delete(aid) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_api WHERE id="..aid, 10) + db:set_keepalive(10000, 100) return res, err end function api_model.deleteByServiceId(sid) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_api WHERE service_id="..sid, 10) + db:set_keepalive(10000, 100) return res, err end function api_model.update(id, request_uri, original_uri, description) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("UPDATE agw_api SET request_uri=\'"..request_uri.."\',original_uri=\'"..original_uri.."\',description=\'"..description.."\' WHERE id="..id, 10) + db:set_keepalive(10000, 100) return res, err end @@ -40,12 +45,14 @@ function api_model.getApi(id) else err = "error api id" end + db:set_keepalive(10000, 100) return api, err end function api_model.getServiceApis(sid) local db = mysql.getDb() local services, err, errno, sqlstate = db:query("SELECT * FROM agw_api WHERE service_id="..sid, 10) + db:set_keepalive(10000, 100) return services, err end diff --git a/src/model/domain.lua b/src/model/domain.lua index 86fcd06..8534be0 100755 --- a/src/model/domain.lua +++ b/src/model/domain.lua @@ -4,30 +4,35 @@ local domain_model = {} function domain_model.addDomain(name) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("INSERT INTO agw_domain (name)values(\'"..name.."\')", 10) + db:set_keepalive(10000, 100) return res, err end function domain_model.delete(id) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_domain WHERE id="..id, 10) + db:set_keepalive(10000, 100) return res, err end function domain_model.update(id, name) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("UPDATE agw_domain SET name=\'"..name.."\' WHERE id="..id, 10) + db:set_keepalive(10000, 100) return res, err end function domain_model.getDomain(id) local db = mysql.getDb() local service, err, errno, sqlstate = db:query("SELECT * FROM agw_domain WHERE id="..id, 10) + db:set_keepalive(10000, 100) return service, err end function domain_model.getDomains() local db = mysql.getDb() local res, err, errno, sqlstate = db:query("SELECT * FROM agw_domain ORDER BY id ASC", 10) + db:set_keepalive(10000, 100) return res, err end diff --git a/src/model/module.lua b/src/model/module.lua index b67062b..c398389 100755 --- a/src/model/module.lua +++ b/src/model/module.lua @@ -4,24 +4,28 @@ local service_model = {} function service_model.add(domain_id, name, host, description) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("INSERT INTO agw_service (domain_id, name,host,description)values(\'"..domain_id.."\',\'"..name.."\',\'"..host.."\',\'"..description.."\')", 10) + db:set_keepalive(10000, 100) return res, err end function service_model.deleteByDomainId(did) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_service WHERE domain_id="..did, 10) + db:set_keepalive(10000, 100) return res, err end function service_model.delete(id) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_service WHERE id="..id, 10) + db:set_keepalive(10000, 100) return res, err end function service_model.update(id, name, host, description) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("UPDATE agw_service SET name=\'"..name.."\',host=\'"..host.."\',description=\'"..description.."\' WHERE id="..id, 10) + db:set_keepalive(10000, 100) return res, err end @@ -34,12 +38,14 @@ function service_model.getService(id) else err = "error service id" end + db:set_keepalive(10000, 100) return service, err end function service_model.getServices(domain_id) local db = mysql.getDb() local services, err, errno, sqlstate = db:query("SELECT * FROM agw_service WHERE domain_id="..domain_id, 10) + db:set_keepalive(10000, 100) return services, err end diff --git a/src/model/server.lua b/src/model/server.lua index 77407e1..3a123b7 100755 --- a/src/model/server.lua +++ b/src/model/server.lua @@ -4,30 +4,35 @@ local server_model = {} function server_model.add(service_id, ip, port, weight, description, protocol) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("INSERT INTO agw_server(service_id,ip,port,weight,description,protocol)values(\'"..service_id.."\',\'"..ip.."\',\'"..port.."\',\'"..weight.."\',\'"..description.."\',\'"..protocol.."\')", 10) + db:set_keepalive(10000, 100) return res, err end function server_model.delete(server_id) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_server WHERE id="..server_id, 10) + db:set_keepalive(10000, 100) return res, err end function server_model.deleteByServiceId(sid) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("DELETE FROM agw_server WHERE service_id="..sid, 10) + db:set_keepalive(10000, 100) return res, err end function server_model.update(server_id, ip, port, weight, description, protocol) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("UPDATE agw_server SET ip=\'"..ip.."\',port="..port..",protocol=\'"..protocol.."\',weight=\'"..weight.."\',description=\'"..description.."\' WHERE id="..server_id, 10) + db:set_keepalive(10000, 100) return res, err end function server_model.getServers(state) local db = mysql.getDb() local servers, err, errno, sqlstate = db:query("SELECT * FROM agw_server WHERE status="..state, 10) + db:set_keepalive(10000, 100) if not servers then return end @@ -43,12 +48,14 @@ end function server_model.getServiceServersWithState(service_id, state) local db = mysql.getDb() local servers, err, errno, sqlstate = db:query("SELECT * FROM agw_server WHERE service_id="..service_id.." AND status="..state, 10) + db:set_keepalive(10000, 100) return servers, err end function server_model.getServer(id) local db = mysql.getDb() local servers, err, errno, sqlstate = db:query("SELECT * FROM agw_server WHERE id="..id, 10) + db:set_keepalive(10000, 100) server = nil if table.getn(servers) > 0 then server = servers[1] @@ -61,6 +68,7 @@ end function server_model.getServersByMid(mid) local db = mysql.getDb() local servers, err, errno, sqlstate = db:query("SELECT * FROM agw_server WHERE mid="..mid, 10) + db:set_keepalive(10000, 100) if not servers then return end @@ -70,6 +78,7 @@ end function server_model.getAllServers() local db = mysql.getDb() local servers, err, errno, sqlstate = db:query("SELECT * FROM agw_server", 10) + db:set_keepalive(10000, 100) if not servers then return end @@ -79,6 +88,7 @@ end function server_model.updateState(sid, state) local db = mysql.getDb() local res, err, errno, sqlstate = db:query("UPDATE agw_server SET status="..state.." WHERE id="..sid, 10) + db:set_keepalive(10000, 100) if not res then ngx.log(ngx.ERR, "update server state err:", err); return false