Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 16 additions & 32 deletions Sources/Redis/Connection/RedisConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import Logging
import NIOCore
import NIOPosix
import NIOSSL
import RESP

#if canImport(Network)
Expand Down Expand Up @@ -81,24 +82,6 @@ public struct RedisConnection: Sendable {
(self.requestStream, self.requestContinuation) = AsyncStream.makeStream(of: RequestStreamElement.self)
}

#if canImport(Network)
/// Initialize Client with TLS options
public init(
address: ServerAddress,
configuration: RedisClientConfiguration,
transportServicesTLSOptions: TSTLSOptions,
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
logger: Logger
) throws {
self.address = address
self.configuration = configuration
self.eventLoopGroup = eventLoopGroup
self.logger = logger
self.tlsOptions = transportServicesTLSOptions.options
(self.requestStream, self.requestContinuation) = AsyncStream.makeStream(of: RequestStreamElement.self)
}
#endif

public func run() async throws {
let asyncChannel = try await self.makeClient(
address: self.address
Expand Down Expand Up @@ -265,26 +248,14 @@ public struct RedisConnection: Sendable {
result =
try await bootstrap
.connect(host: host, port: port) { channel in
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(RESPTokenHandler())
return try NIOAsyncChannel<RESPToken, ByteBuffer>(
wrappingChannelSynchronously: channel,
configuration: .init()
)
}
setupChannel(channel)
}
self.logger.debug("Client connnected to \(host):\(port)")
case .unixDomainSocket(let path):
result =
try await bootstrap
.connect(unixDomainSocketPath: path) { channel in
channel.eventLoop.makeCompletedFuture {
try channel.pipeline.syncOperations.addHandler(RESPTokenHandler())
return try NIOAsyncChannel<RESPToken, ByteBuffer>(
wrappingChannelSynchronously: channel,
configuration: .init()
)
}
setupChannel(channel)
}
self.logger.debug("Client connnected to socket path \(path)")
}
Expand All @@ -294,6 +265,19 @@ public struct RedisConnection: Sendable {
}
}

private func setupChannel(_ channel: Channel) -> EventLoopFuture<NIOAsyncChannel<RESPToken, ByteBuffer>> {
channel.eventLoop.makeCompletedFuture {
if case .enable(let sslContext, let tlsServerName) = self.configuration.tls.base {
try channel.pipeline.syncOperations.addHandler(NIOSSLClientHandler(context: sslContext, serverHostname: tlsServerName))
}
try channel.pipeline.syncOperations.addHandler(RESPTokenHandler())
return try NIOAsyncChannel<RESPToken, ByteBuffer>(
wrappingChannelSynchronously: channel,
configuration: .init()
)
}
}

/// create a BSD sockets based bootstrap
private func createSocketsBootstrap() -> ClientBootstrap {
ClientBootstrap(group: self.eventLoopGroup)
Expand Down
178 changes: 0 additions & 178 deletions Sources/Redis/Connection/TSTLSOptions.swift

This file was deleted.

35 changes: 0 additions & 35 deletions Sources/Redis/RedisClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ import NIOTransportServices
///
/// Supports TLS via both NIOSSL and Network framework.
public struct RedisClient {
enum MultiPlatformTLSConfiguration: Sendable {
case niossl(TLSConfiguration)
#if canImport(Network)
case ts(TSTLSOptions)
#endif
}

/// Server address
let serverAddress: ServerAddress
/// configuration
Expand All @@ -39,8 +32,6 @@ public struct RedisClient {
let eventLoopGroup: EventLoopGroup
/// Logger
let logger: Logger
/// TLS configuration
let tlsConfiguration: MultiPlatformTLSConfiguration?

/// Initialize Redis client
///
Expand All @@ -53,40 +44,14 @@ public struct RedisClient {
public init(
_ address: ServerAddress,
configuration: RedisClientConfiguration = .init(),
tlsConfiguration: TLSConfiguration? = nil,
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup.singleton,
logger: Logger
) {
self.serverAddress = address
self.configuration = configuration
self.eventLoopGroup = eventLoopGroup
self.logger = logger
self.tlsConfiguration = tlsConfiguration.map { .niossl($0) }
}

#if canImport(Network)
/// Initialize Redis client
///
/// - Parameters:
/// - address: redis database address
/// - configuration: Redis client configuration
/// - transportServicesTLSOptions: Redis TLS connection configuration
/// - eventLoopGroup: EventLoopGroup to run WebSocket client on
/// - logger: Logger
public init(
_ address: ServerAddress,
configuration: RedisClientConfiguration = .init(),
transportServicesTLSOptions: TSTLSOptions,
eventLoopGroup: NIOTSEventLoopGroup = NIOTSEventLoopGroup.singleton,
logger: Logger
) {
self.serverAddress = address
self.configuration = configuration
self.eventLoopGroup = eventLoopGroup
self.logger = logger
self.tlsConfiguration = .ts(transportServicesTLSOptions)
}
#endif
}

extension RedisClient {
Expand Down
35 changes: 31 additions & 4 deletions Sources/Redis/RedisClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,46 @@
//
//===----------------------------------------------------------------------===//

import NIOSSL

/// Configuration for the redis client
public struct RedisClientConfiguration: Sendable {
public enum RESPVersion: Sendable {
case v2
case v3
public struct RESPVersion: Sendable, Equatable {
enum Base {
case v2
case v3
}
let base: Base

public static var v2: Self { .init(base: .v2) }
public static var v3: Self { .init(base: .v3) }
}

public struct TLS: Sendable {
enum Base {
case disable
case enable(NIOSSLContext, String?)
}
let base: Base

public static var disable: Self { .init(base: .disable) }
public static func enable(tlsConfiguration: TLSConfiguration, tlsServerName: String?) throws -> Self {
.init(base: .enable(try NIOSSLContext(configuration: tlsConfiguration), tlsServerName))
}
}

public var respVersion: RESPVersion
public var tls: TLS

/// Initialize RedisClientConfiguration
/// - Parameters
/// - respVersion: RESP version to use
public init(respVersion: RESPVersion = .v3) {
/// - tlsConfiguration: TLS configuration
public init(
respVersion: RESPVersion = .v3,
tls: TLS = .disable
) {
self.respVersion = respVersion
self.tls = tls
}
}