openresty / openresty/lua-resty-redis

Error handling in the example in the "Synopsis" section of README.markdown

Open
#206 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
2k
Forks
447
Avg merge
2h 24m
Merged PRs (30d)
3

Description

I'm trying to understand the lack of connection handling during the return conditions in the example given in the package synopsis. I've no prior experience with Lua so maybe there's more going on implicitly than it obvious to someone without experience in the language.

Specifically, there seems to be some "normal" query conditions where the routine is returned from without either closing the connection or returning it to the pool. It would be useful to either have consistent treatment of the connection during the return conditions, or an explanation of the consequences of not closing the connection or returning it to the pool and what problems will result from this, or not.

I've annotates some of the returns. My comments below shouldn't be taken too literally since it is obviously not valid to "goto" over a local variable declaration, but are just indicators of the different types of conditions - bad data vs bad connection, etc - leading to returning from the routine.

  ...
    server {
        location /test {
            content_by_lua_block {
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeouts(1000, 1000, 1000) -- 1 sec

                local ok, err = red:connect("127.0.0.1", 6379)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return   -- HERE: Connection not succeeded: return may be the right thing to do.
                end

                ok, err = red:set("dog", "an animal")
                if not ok then
                    ngx.say("failed to set dog: ", err)
                    return   -- HERE: Is this a connection issue or a normal data condition? If just clashing keys then should it be a "goto done"?
                end

...

                local res, err = red:get("dog")
                if not res then
                    ngx.say("failed to get dog: ", err)
                    return   -- HERE: Is this the result of a connection issue?
                end

                if res == ngx.null then
                    ngx.say("dog not found.")
                    return   -- HERE: Simple data not found, so should this be "goto done"?
                end

 ...
 
::done::
                local ok, err = red:set_keepalive(10000, 100)
                if not ok then
                    ngx.say("failed to set keepalive: ", err)
                    return
                end

...

            }
        }
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Synopsis example in README.markdown and inspect each return after red:connect, red:set, and red:get. Verify how the example handles failed operations, missing data, and red:set_keepalive, then update the example or its explanation so connection cleanup behavior is consistent and the consequences are clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.