From 6a9c284c2a7503f7e085749d83da2414c2b9e6e8 Mon Sep 17 00:00:00 2001 From: Yim Lee Date: Mon, 14 Nov 2022 08:52:34 -0800 Subject: [PATCH] allow setting per-statement request timeout (#9) Co-authored-by: Rick Newton-Rogers --- Sources/CassandraClient/Statement.swift | 16 ++++++++++++++-- .../CassandraClientTests.swift | 3 +-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Sources/CassandraClient/Statement.swift b/Sources/CassandraClient/Statement.swift index 60dd326..569b4be 100644 --- a/Sources/CassandraClient/Statement.swift +++ b/Sources/CassandraClient/Statement.swift @@ -81,6 +81,10 @@ extension CassandraClient { if let consistency = options.consistency { try checkResult { cass_statement_set_consistency(self.rawPointer, consistency.cassConsistency) } } + + if let requestTimeout = options.requestTimeout { + try checkResult { cass_statement_set_request_timeout(self.rawPointer, requestTimeout) } + } } func setPagingSize(_ pagingSize: Int32) throws { @@ -118,13 +122,21 @@ extension CassandraClient { public struct Options: CustomStringConvertible { /// Sets the statement's consistency level. Default is `.localOne`. public var consistency: CassandraClient.Consistency? + /// Sets the statement's request timeout in milliseconds. Default is `CASS_UINT64_MAX` + public var requestTimeout: UInt64? - public init(consistency: CassandraClient.Consistency? = .none) { + public init(consistency: CassandraClient.Consistency? = nil, requestTimeout: UInt64? = nil) { self.consistency = consistency + self.requestTimeout = requestTimeout } public var description: String { - "Options { consistency: \(String(describing: self.consistency)) }" + """ + Options { + consistency: \(String(describing: self.consistency)), + requestTimeout: \(String(describing: self.requestTimeout)) + } + """ } } } diff --git a/Tests/CassandraClientTests/CassandraClientTests.swift b/Tests/CassandraClientTests/CassandraClientTests.swift index 4bcce76..4ba360a 100644 --- a/Tests/CassandraClientTests/CassandraClientTests.swift +++ b/Tests/CassandraClientTests/CassandraClientTests.swift @@ -194,8 +194,7 @@ final class Tests: XCTestCase { let tableName = "test_\(DispatchTime.now().uptimeNanoseconds)" XCTAssertNoThrow(try self.cassandraClient.run("create table \(tableName) (id int primary key, data text);").wait()) - var options = CassandraClient.Statement.Options() - options.consistency = .localQuorum + let options = CassandraClient.Statement.Options(consistency: .localQuorum) let count = Int.random(in: 5000 ... 6000) var futures = [EventLoopFuture]()