Skip to content

Commit

Permalink
Refactor RttCommand to work with ios 13
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
  • Loading branch information
piotrpio authored and Jarema committed Mar 28, 2024
1 parent bf81743 commit a0be00d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -6,7 +6,7 @@ let package = Package(
name: "nats-swift",
platforms: [
.macOS(.v13),
.iOS(.v16)
.iOS(.v13)
],
products: [
.library(name: "Nats", targets: ["Nats"])
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nats/NatsClient/NatsClient.swift
Expand Up @@ -158,7 +158,7 @@ extension NatsClient {
return try await connectionHandler.subscribe(subject)
}

public func rtt() async throws -> Duration {
public func rtt() async throws -> TimeInterval {
guard let connectionHandler = self.connectionHandler else {
throw NatsClientError("internal error: empty connection handler")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Nats/NatsConnection.swift
Expand Up @@ -288,7 +288,7 @@ class ConnectionHandler: ChannelInboundHandler {
let tlsConfig = try makeTLSConfig()
let sslContext = try NIOSSLContext(configuration: tlsConfig)
let sslHandler = try NIOSSLClientHandler(
context: sslContext, serverHostname: s.host())
context: sslContext, serverHostname: s.host)
try await self.channel?.pipeline.addHandler(sslHandler, position: .first)
}

Expand Down Expand Up @@ -375,7 +375,7 @@ class ConnectionHandler: ChannelInboundHandler {
let sslContext = try NIOSSLContext(
configuration: tlsConfig)
let sslHandler = try NIOSSLClientHandler(
context: sslContext, serverHostname: server.host()!)
context: sslContext, serverHostname: server.host!)
//Fixme(jrm): do not ignore error from addHandler future.
channel.pipeline.addHandler(sslHandler).flatMap { _ in
channel.pipeline.addHandler(self)
Expand Down
18 changes: 10 additions & 8 deletions Sources/Nats/RttCommand.swift
Expand Up @@ -11,27 +11,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import NIOCore

internal class RttCommand {
let startTime = ContinuousClock().now
let promise: EventLoopPromise<Duration>?
let startTime = DispatchTime.now()
let promise: EventLoopPromise<TimeInterval>?

static func makeFrom(channel: Channel?) -> RttCommand {
RttCommand(promise: channel?.eventLoop.makePromise(of: Duration.self))
RttCommand(promise: channel?.eventLoop.makePromise(of: TimeInterval.self))
}

private init(promise: EventLoopPromise<Duration>?) {
private init(promise: EventLoopPromise<TimeInterval>?) {
self.promise = promise
}

func setRoundTripTime() {
let now: ContinuousClock.Instant = ContinuousClock().now
let rtt: Duration = now - startTime
let now = DispatchTime.now()
let nanoTime = now.uptimeNanoseconds - startTime.uptimeNanoseconds
let rtt = TimeInterval(nanoTime) / 1_000_000_000 // Convert nanos to seconds
promise?.succeed(rtt)
}

func getRoundTripTime() async throws -> Duration {
try await promise?.futureResult.get() ?? Duration.zero
func getRoundTripTime() async throws -> TimeInterval {
try await promise?.futureResult.get() ?? 0
}
}
4 changes: 2 additions & 2 deletions Tests/NatsTests/Integration/ConnectionTests.swift
Expand Up @@ -54,8 +54,8 @@ class CoreNatsTests: XCTestCase {
.build()
try await client.connect()

let rtt: Duration = try await client.rtt()
XCTAssertGreaterThan(rtt, Duration.zero, "should have RTT")
let rtt: TimeInterval = try await client.rtt()
XCTAssertGreaterThan(rtt, 0, "should have RTT")

try await client.close()
}
Expand Down

0 comments on commit a0be00d

Please sign in to comment.