Git Product home page Git Product logo

Comments (2)

agentzh avatar agentzh commented on July 25, 2024

@rabbate Your code looks good to me though you can save some CPU cycles by putting your main logic into a function in an external Lua module (similar to the resty.mysql module itself).

As long as your nginx worker processes are not blocked by anything, the timeout errors should be something in your mysql server or on the wire in between. It's recommended to always retry 1 or 2 times automatically upon database query failures to take into account such transient timeout errors.

from lua-resty-mysql.

aftabnaveed avatar aftabnaveed commented on July 25, 2024

As long as your nginx worker processes are not blocked by anything, the timeout errors should be something in your mysql server or on the wire in between. It's recommended to always retry 1 or 2 times automatically upon database query failures to take into account such transient timeout errors.

@agentzh Are you able to give an example of something like that. I am getting exactly the same error for this code:

local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
    ngx.say("failed to instantiate mysql: ", err)
    return
end

db:set_timeout(2000)
-- connect to the database
local ok, err, errorcode, sqlstate = db:connect {
    host = ngx.var.mysql_host,
    port = 3306,
    database = "db",
    user = "root",
    password = "password"
}

if not ok then
    ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
end


local request_uri = ngx.quote_sql_str(ngx.var.request_uri)
local query = "select target_path from core_url_rewrite where concat('/',request_path)=" .. request_uri .. " AND store_id=" .. ngx.var.store_id

res, err, errcode, sqlstate = db:query(query)

if err then
    ngx.say("Error occured: ", err, errcode, sqlstate)
end

local cjson = require "cjson"
ngx.say(cjson.encode(res))

if res then
    ngx.req.set_uri("/" .. res[1].target_path, true)
    return
end

Error Error occured: failed to receive packet header: timeout

from lua-resty-mysql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.