Kitura / Kitura-redis

Swift Redis library
Apache License 2.0
94 stars 25 forks source link

dies with concurrent connections Connected to Redis Error Domain=RedisDomain Code=1 "Error: Unknown response type" UserInfo={NSLocalizedDescription=Error: Unknown response type} #77

Closed gurugeek closed 4 years ago

gurugeek commented 4 years ago

I am running a simple route that fetches data from Redis

router.get("/:name") { request, response,

        next in guard let name = request.parameters["name"]
        else {
            _ = response.send(status: .badRequest)
            return next()
        }

        redis.connect(host: "localhost", port: 6379) { (redisError: NSError?) in
        if let error = redisError {
            print(error)
        } else {
            print("Connected to Redis")
        }
              }

        // Get the page key -- slug is the key
        redis.get(name) { (string: RedisString?, redisError: NSError?) in
            if let error = redisError {
                print(error)
            } else if let string = string?.asString {
                let body = ["body": string,
                            "date": date]

                do {
                    try response.render("pageredis.stencil", context: body)
                } catch {
                    print(error.localizedDescription)
                }

                // print("Redis \(string)")
                // response.send(string)
            }
        }
    }

Now a simple test

 ab -n 100 -c 10  http://127.0.0.1:8081/index
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)...apr_pollset_poll: The timeout specified has expired (70007)
Total of 91 requests completed

makes the script die with the following errors

Error Domain=RedisDomain Code=1 "Error: Unknown response type" UserInfo={NSLocalizedDescription=Error: Unknown response type}
Error Domain=RedisDomain Code=1 "Error: Unknown response type" UserInfo={NSLocalizedDescription=Error: Unknown response type}

PressDotToys(62529,0x70000d69d000) malloc: double free for ptr 0x7fda3f038a00
PressDotToys(62529,0x70000d69d000) malloc: *** set a breakpoint in malloc_error_break to debug
gurugeek commented 4 years ago

alright to fix it you need to wrap the connection in execute

execute { redis.connect(host: "localhost", port: 6379) { (redisError: NSError?) in if let error = redisError { print(error) } else { print("Connected to Redis") } } }

see step 5 here https://www.kitura.io/docs/routing/raw-routing