vapor / redis

Vapor provider for RediStack
MIT License
458 stars 57 forks source link

Sending the Redis transaction commands #190

Closed ingeekim closed 2 years ago

ingeekim commented 2 years ago

I am trying to simulate the Redis MULTI ... EXEC pair instead of waiting pipeline command in RediStack:

func multiHandler(req: Request) throws -> EventLoopFuture<HTTPStatus> {
        req.redis.send(command: "MULTI")
        req.redis.send(command: "PING")
        req.redis.send(command: "PING")
        req.redis.send(command: "EXEC")

        return Planet.query(on: req.db).all().transform(to: .ok)
 }

The console shows me:

[ DEBUG ] sending command [rdstk_args: [], rdstk_command: MULTI, rdstk_conn_id: ...
[ DEBUG ] sending command [rdstk_args: [], rdstk_command: PING, rdstk_conn_id: ...
[ DEBUG ] command succeeded [rdstk_conn_id: 79FF1ED0-6916-4957-91DF-58575FAF0C87, rdstk_conpool_id: AF9B78D6-92E0-4B78-AC41-1A15E4B4D01D, rdstk_result: OK, request-id: 3DFCC3EF-F197-4DBE-BF50-780A5010F24B] (RediStack/RedisConnection.swift:272)
...
[ DEBUG ] command succeeded [rdstk_conn_id: 79FF1ED0-6916-4957-91DF-58575FAF0C87, rdstk_conpool_id: AF9B78D6-92E0-4B78-AC41-1A15E4B4D01D, rdstk_result: QUEUED, request-id: 3DFCC3EF-F197-4DBE-BF50-780A5010F24B] (RediStack/RedisConnection.swift:272)
[ ERROR ] command failed [error: (Redis) ERR EXEC without MULTI, rdstk_conn_id: 63E078A3-A47F-455C-A3DE-E35AC69D9DA0, rdstk_conpool_id: AF9B78D6-92E0-4B78-AC41-1A15E4B4D01D, request-id: 3DFCC3EF-F197-4DBE-BF50-780A5010F24B] (RediStack/RedisConnection.swift:267)

This is one of the problem in using the transaction commands of Redis, MULTI, EXEC, WATCH, UNWATCH, and DISCARD, which require the leaseConnection to maintain a single connection, by leaseConnection. My suggestion is to build a new keyword "transaction" such as

let response = req.redis.transaction("MULTI") { 
  let response1 = req.redis.send(command1)
  let response2 = req.redis.send(command2)
  ...
 }.exec()

The transaction maintains a single connection. Further development for error managements such as WATCH, UNWATCH, DISCARD commands would be appreciated.

Mordil commented 2 years ago

This has been fixed in 4.4.0 - please update your packages and try now.