Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logic to cancel in-flight promises when connection drops #151

Open
wants to merge 2 commits into
base: support-3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/Redis/Client/RedisClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public final class RedisClient: DatabaseConnection, BasicWorker {

/// The channel
private let channel: Channel

/// Stores the inflight promises so they can be fulfilled when the channel drops
var inflightPromises: [String:EventLoopPromise<RedisData>] = [:]

/// Creates a new Redis client on the provided data source and sink.
init(queue: RedisCommandHandler, channel: Channel) {
Expand All @@ -28,6 +31,12 @@ public final class RedisClient: DatabaseConnection, BasicWorker {
self.extend = [:]
self.isClosed = false
channel.closeFuture.always {
// send closed error for the promises that have not been fulfilled
for promise in self.inflightPromises.values {
promise.fail(error: ChannelError.ioOnClosedChannel)
}
ericchapman marked this conversation as resolved.
Show resolved Hide resolved
self.inflightPromises.removeAll()

self.isClosed = true
}
}
Expand Down Expand Up @@ -55,6 +64,13 @@ public final class RedisClient: DatabaseConnection, BasicWorker {
// create a new promise to fulfill later
let promise = eventLoop.newPromise(RedisData.self)

// logic to store in-flight requests that can be cancelled when connection drops
let key = UUID().uuidString
self.inflightPromises[key] = promise
promise.futureResult.always {
self.inflightPromises.removeValue(forKey: key)
}

// write the message and the promise to the channel, which the `RequestResponseHandler` will capture
return self.channel.writeAndFlush((message, promise))
.flatMap { return promise.futureResult }
Expand Down
28 changes: 28 additions & 0 deletions Tests/RedisTests/RedisDatabaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ class RedisDatabaseTests: XCTestCase {
try redis.delete("hello").wait()
XCTAssertNil(try redis.get("hello", as: String.self).wait())
}

func testDroppedConnection() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let config = RedisClientConfig.makeTest()
let database = try RedisDatabase(config: config)
let redis = try database.newConnection(on: group).wait()
defer { redis.close() }

let timeout: UInt32 = 1
var dataReceived = false
var errorReceived = false

let command = RedisData.array(["brpop", "hello", "\(timeout)"].map { RedisData(bulk: $0) })
_ = redis.send(command).do { data in
dataReceived = true
}.catch { error in
errorReceived = true
}

// Close the connection
redis.close()

// Sleep for an extra second seconds to give the transaction time to complete
sleep(timeout+1)

XCTAssertEqual(dataReceived, false)
XCTAssertEqual(errorReceived, true)
}

func testSelect() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Expand Down