Skip to content

Commit

Permalink
Removed support for swift4 and nio1.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett committed Nov 8, 2019
1 parent 72c238b commit b0507c0
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 253 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ matrix:
include:
- os: Linux
dist: trusty
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-4.2.3-release/ubuntu1404/swift-4.2.3-RELEASE/swift-4.2.3-RELEASE-ubuntu14.04.tar.gz"
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.0-release/ubuntu1404/swift-5.0-RELEASE/swift-5.0-RELEASE-ubuntu14.04.tar.gz"
sudo: required
- os: Linux
dist: trusty
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.0-release/ubuntu1404/swift-5.0-RELEASE/swift-5.0-RELEASE-ubuntu14.04.tar.gz"
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.1.1-release/ubuntu1404/swift-5.1.1-RELEASE/swift-5.1.1-RELEASE-ubuntu14.04.tar.gz"
sudo: required
- os: osx
osx_image: xcode10.1
- os: osx
osx_image: xcode10.2

- os: osx
osx_image: xcode11.2

before_install:
- ./.travis.d/before-install.sh
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.2
// swift-tools-version:5.0

import PackageDescription

Expand All @@ -9,9 +9,9 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git",
from: "1.13.2"),
from: "2.0.0"),
.package(url: "https://github.com/SwiftNIOExtras/swift-nio-redis.git",
from: "0.9.0")
from: "0.10.2")
],
targets: [
.target (name: "Redis", dependencies: [ "NIORedis" ]),
Expand Down
20 changes: 0 additions & 20 deletions [email protected]

This file was deleted.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# SwiftNIO Redis Client

![Swift4](https://img.shields.io/badge/swift-4-blue.svg)
![Swift5](https://img.shields.io/badge/swift-5-blue.svg)
![macOS](https://img.shields.io/badge/os-macOS-green.svg?style=flat)
![tuxOS](https://img.shields.io/badge/os-tuxOS-green.svg?style=flat)
Expand Down
120 changes: 28 additions & 92 deletions Sources/Redis/RedisClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,21 @@ open class RedisClient : RedisCommandTarget {
_ = bootstrap.channelOption(ChannelOptions.reuseAddr, value: 1)

_ = bootstrap.channelInitializer { [weak self] channel in
#if swift(>=5)
return channel.pipeline
.configureRedisPipeline()
.flatMap { [weak self] in
guard let me = self else {
//assert(self != nil, "bootstrap running, but client gone?!")
let error = channel.eventLoop.makePromise(of: Void.self)
error.fail(Error.internalInconsistency)
return error.futureResult
}

return channel.pipeline
.addHandler(Handler(client: me),
name: "de.zeezide.nio.redis.client")
}
#else

return channel.pipeline
.configureRedisPipeline()
.flatMap { [weak self] in
guard let me = self else {
//assert(self != nil, "bootstrap running, but client gone?!")
let error = channel.eventLoop.makePromise(of: Void.self)
error.fail(Error.internalInconsistency)
return error.futureResult
}

return channel.pipeline
.configureRedisPipeline()
.thenThrowing { [weak self] in
guard let me = self else {
//assert(self != nil, "bootstrap running, but client gone?!")
throw Error.internalInconsistency
}

let handler = Handler(client: me)
_ = channel.pipeline.add(name: "de.zeezide.nio.redis.client",
handler: handler)
}
#endif
.addHandler(Handler(client: me),
name: "de.zeezide.nio.redis.client")
}
}
}
#if false
Expand Down Expand Up @@ -353,13 +339,8 @@ open class RedisClient : RedisCommandTarget {

// MARK: - Commands

#if swift(>=5)
var callQueue = CircularBuffer<RedisCommandCall>(initialCapacity: 16)
var pendingCalls = CircularBuffer<RedisCommandCall>(initialCapacity: 16)
#else
var callQueue = CircularBuffer<RedisCommandCall>(initialRingCapacity: 16)
var pendingCalls = CircularBuffer<RedisCommandCall>(initialRingCapacity: 16)
#endif

public func enqueueCommandCall(_ call: RedisCommandCall) { // Q: any
guard eventLoop.inEventLoop else {
Expand All @@ -383,18 +364,10 @@ open class RedisClient : RedisCommandTarget {
_ = _connect(host: options.hostname ?? "localhost", port: options.port)

case .requestedQuit, .quit:
#if swift(>=5)
callQueue.forEach { $0.promise.fail(Error.stopped) }
#else
callQueue.forEach { $0.promise.fail(error: Error.stopped) }
#endif
callQueue.forEach { $0.promise.fail(Error.stopped) }

case .error(let error):
#if swift(>=5)
callQueue.forEach { $0.promise.fail(error) }
#else
callQueue.forEach { $0.promise.fail(error: error) }
#endif
callQueue.forEach { $0.promise.fail(error) }

case .connecting, .authenticating: break

Expand All @@ -412,11 +385,7 @@ open class RedisClient : RedisCommandTarget {
channel.write(call.command)
.map { self.pendingCalls.append(call) }
.whenFailure {
#if swift(>=5)
call.promise.fail(Error.writeError($0))
#else
call.promise.fail(error: Error.writeError($0))
#endif
call.promise.fail(Error.writeError($0))
}
}
channel.flush()
Expand All @@ -428,19 +397,11 @@ open class RedisClient : RedisCommandTarget {
if !pendingCalls.isEmpty {
let call = pendingCalls.removeFirst()

#if swift(>=5)
if !call.command.isSubscribe {
call.promise.succeed(value)
return
}
call.promise.succeed(.bulkString(nil)) // TBD
#else
if !call.command.isSubscribe {
call.promise.succeed(result: value)
return
}
call.promise.succeed(result: .bulkString(nil)) // TBD
#endif
if !call.command.isSubscribe {
call.promise.succeed(value)
return
}
call.promise.succeed(.bulkString(nil)) // TBD
}

// PubSub handling
Expand Down Expand Up @@ -490,21 +451,12 @@ open class RedisClient : RedisCommandTarget {
var channel : Channel? { @inline(__always) get { return state.channel } }

public func quit() {
#if swift(>=5)
_enqueueCommandCall(RedisCommandCall(["QUIT"], eventLoop: eventLoop))
.whenComplete { _ in
self.state = .quit
self.subscribeListeners.removeAll()
self.messageListeners.removeAll()
}
#else
_enqueueCommandCall(RedisCommandCall(["QUIT"], eventLoop: eventLoop))
.whenComplete {
self.state = .quit
self.subscribeListeners.removeAll()
self.messageListeners.removeAll()
}
#endif
_enqueueCommandCall(RedisCommandCall(["QUIT"], eventLoop: eventLoop))
.whenComplete { _ in
self.state = .quit
self.subscribeListeners.removeAll()
self.messageListeners.removeAll()
}
_processQueue()
}

Expand Down Expand Up @@ -675,22 +627,6 @@ open class RedisClient : RedisCommandTarget {
self.client.handlerCaughtError(error, in: context)
_ = context.close(promise: nil)
}

#if swift(>=5) // NIO 2 API - default
#else // NIO 1 API Shims
func channelInactive(ctx context: ChannelHandlerContext) {
channelInactive(context: context)
}

func channelRead(ctx context: ChannelHandlerContext, data: NIOAny) {
channelRead(context: context, data: data)
}

public func errorCaught(ctx context: ChannelHandlerContext, error: Error)
{
errorCaught(context: context, error: error)
}
#endif // NIO 1 API Shims
}

}
Expand Down
6 changes: 1 addition & 5 deletions Sources/Redis/RedisCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ public class RedisCommandCall {
convenience init<T: Collection>(_ values: T, eventLoop: EventLoop)
where T.Element : RESPEncodable
{
#if swift(>=5)
self.init(command: RedisCommand(values), promise: eventLoop.makePromise())
#else
self.init(command: RedisCommand(values), promise: eventLoop.newPromise())
#endif
self.init(command: RedisCommand(values), promise: eventLoop.makePromise())
}
}

Expand Down
78 changes: 23 additions & 55 deletions Sources/Redis/RedisCommandTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,55 +341,29 @@ import class NIO.EventLoopFuture
import enum NIORedis.RESPValue
import protocol NIORedis.RESPEncodable

#if swift(>=5)
fileprivate extension EventLoopFuture {

func whenCB(file: StaticString = #file, line: UInt = #line,
_ cb: @escaping ( Swift.Error?, Value? ) -> Void) -> Void
{
self.map(file: file, line: line) { cb(nil, $0) }
.whenFailure { cb($0, nil) }
}
}

fileprivate extension EventLoopFuture where Value == RESPValue {

func whenCB<U: RedisTypeTransformable>(file: StaticString = #file,
line: UInt = #line,
_ cb: @escaping ( Swift.Error?, U? ) -> Void) -> Void
{
self.map(file: file, line: line) {
do { cb(nil, try U.extractFromRESPValue($0)) }
catch { cb(error, nil) }
}
.whenFailure { cb($0, nil) }
}
}
#else // NIO1
fileprivate extension EventLoopFuture {

func whenCB(file: StaticString = #file, line: UInt = #line,
_ cb: @escaping ( Swift.Error?, T? ) -> Void) -> Void
{
self.map(file: file, line: line) { cb(nil, $0) }
.whenFailure { cb($0, nil) }
}
fileprivate extension EventLoopFuture {

func whenCB(file: StaticString = #file, line: UInt = #line,
_ cb: @escaping ( Swift.Error?, Value? ) -> Void) -> Void
{
self.map(file: file, line: line) { cb(nil, $0) }
.whenFailure { cb($0, nil) }
}
}

fileprivate extension EventLoopFuture where T == RESPValue {

func whenCB<U: RedisTypeTransformable>(file: StaticString = #file,
line: UInt = #line,
_ cb: @escaping ( Swift.Error?, U? ) -> Void) -> Void
{
self.map(file: file, line: line) {
do { cb(nil, try U.extractFromRESPValue($0)) }
catch { cb(error, nil) }
}
.whenFailure { cb($0, nil) }
}
fileprivate extension EventLoopFuture where Value == RESPValue {

func whenCB<U: RedisTypeTransformable>(file: StaticString = #file,
line: UInt = #line,
_ cb: @escaping ( Swift.Error?, U? ) -> Void) -> Void
{
self.map(file: file, line: line) {
do { cb(nil, try U.extractFromRESPValue($0)) }
catch { cb(error, nil) }
}
.whenFailure { cb($0, nil) }
}
#endif // NIO1
}

public extension RedisCommandTarget {

Expand All @@ -400,15 +374,9 @@ public extension RedisCommandTarget {
{
let call = RedisCommandCall(values, eventLoop: eventLoop)

#if swift(>=5)
let future = call.promise.futureResult.flatMapThrowing {
try U.extractFromRESPValue($0)
}
#else
let future = call.promise.futureResult.thenThrowing {
try U.extractFromRESPValue($0)
}
#endif
let future = call.promise.futureResult.flatMapThrowing {
try U.extractFromRESPValue($0)
}
enqueueCommandCall(call)
return future
}
Expand Down
Loading

0 comments on commit b0507c0

Please sign in to comment.