140 Commits

Author SHA1 Message Date
Nathan Harris aa185a0133 Lay groundwork for RESP3 support and flat ChannelHandler hierarchy
## Motivation

Since Redis 6.0, a new serialization protocol format (v3) is available that gives richer semantic reasoning behind the different types to enable commands to better understand the return types to provide in their programming language.

In addition, the `RESPTranslator` type is going to see more direct usage, and the current API doesn't make read well.

## Changes

- Add: Internal `RESPVersion` enum that the `RESPTranslator` will start to use
- Rename: `RESPTranslator.parseBytes` to `RESPTranslator.read(from:)`
2022-12-26 14:43:21 -06:00
Nathan Harris a431ae8c6c [List] Properly map results when key has no values
Fixes #116
2022-12-12 20:38:00 -06:00
Fabian Fett 4ea66c4788 Add support for graceful shutdown to the RedisCommandHandler 2022-12-07 15:51:43 +00:00
Nathan Harris b88fac059d #115 -- Remove logging(to:) method 2022-12-01 13:06:35 -06:00
Fabian Fett c366a16fe8 Explicitly depend on NIO modules 2022-12-01 13:46:41 +01:00
Nathan Harris 00eb9b5e33 Unify PubSub Handler Signature
Motivation

Right now the PubSub handlers are split into three separate closures, with the subscribe/unsubscribe handlers being optional. This won't play well with AsyncStream for being able to respond to all events that a PubSub subscription can cause.

Additionally, the current structure is very verbose in code to maintain - but also adds complexity to developers who are first getting started to understand the lifecycle of PubSub events.

Changes

- Add: New `RedisPubSubEvent` enum that captures the subscribe, unsubscribe, and message lifecycle events
- Add: New `RedisPubSubEventReceiver` that combines the previous 3 closure types
- Add: Dedicated DocC Symbol Extension file for `RedisPubSubHandler`
- Change: `RedisClient.subscribe` and `RedisClient.psubscribe` method signatures to only require a single unlabeled closure
- Rename: `RedisUnsubscribeEventSource` to `RedisPubSubEvent.UnsubscribeEventSource`
- Remove: `RedisSubscriptionMessageReceiver`, `RedisSubscriptionChangeDetails`, `RedisSubscribeHandler`, and `RedisUnsubscribeHandler` types

Result

Developers should have a much easier time getting started and understanding PubSub with assistance from the compiler with types to understand
what they're being given and what's available to them as information to make more informed decisions in their app logic.
2022-11-30 04:33:31 +00:00
Nathan Harris c76203c61a #103 -- Provide greater context to Pub/Sub Unsubscribe events 2022-08-13 00:22:18 -05:00
Nathan Harris 820820d877 Significantly Improve the Configuration API for Pools and Connections
## Motivation

The API for establishing the configuration of a connection pool had a lot of jargon and properties that developers had issues keeping straight and understanding what each does.

This commit provides first-class API support for concepts such as retry strategies, and how the pool handles connection counts.

## Changes

- Add: New ConnectionCountBehavior for determining leaky / non-leaky behavior
- Add: New ConnectionRetryStrategy for allowing customization of retry behavior
- Change: RedisConnection.defaultPort to be a computed property
- Change: The logging keys of pool connection retry metadata
- Rename: Several configuration properties to drop prefixes or to be combined into new structures

## Result

Developers should have a much better experience exploring the available configuration options for pools and connections, being able to understand how each piece works with the underlying system.
2022-04-24 23:37:39 -05:00
Nathan Harris cfb99ba0f7 #100 -- Fix addPubSubHandler not checking if already added 2022-04-24 03:34:10 +00:00
Nathan Harris 284b7f09bc Add overload of ping command for nil message style 2022-04-19 23:18:26 -05:00
Nathan Harris 9da5773e7a 92 -- Accept event loop and logger in RedisClient commands
There are many times that developers want exact control over which EventLoop will be executing their chained EventLoopFuture callbacks
and which Logger will do the logging in calls deep within RediStack.

All commands will now accept an optional EventLoop and Logger to hop to, and using the logger for desired logs.
2022-04-04 00:06:47 -05:00
Nathan Harris 498b6a5eb5 Conform RedisCommand to Equatable 2022-03-15 23:38:13 -05:00
Nathan Harris 252b0b8061 Improve KEYS command to be more type-safe 2022-03-15 23:30:30 -05:00
Nathan Harris 370ef8c4ac 101 -- Add KEYS command 2022-03-15 21:12:22 -05:00
Nathan Harris 6c4ca52f74 104 -- Add real world test case to ensure revrange bug doesn't persist 2022-03-15 04:33:35 +00:00
Daniel Ramteke b449334c8a [Commands] Add STRLEN 2022-02-21 19:10:34 +00:00
Nathan Harris ad316a97ac 95 -- Add callback closure on RedisConnection invoked on channel close 2021-08-16 21:47:20 -07:00
Peter Adams 3ca471b226 Get pubsub numsub working 2021-05-04 16:26:35 +00:00
Peter Adams e08b42616b Get pubsub numpat working 2021-05-03 20:49:16 +00:00
Peter Adams 410a5b2d03 Change the scan test to accept up to 8 odd keys
This attempts to fix #23
2021-05-03 20:43:04 +00:00
Peter Adams 9958e2d13b Fix pubsub channels 2021-05-02 20:39:18 +01:00
Nathan Harris f0d123fdaa Disable scan unit tests again as they are still flaky 2021-04-29 09:31:38 -07:00
Peter Adams 0c13e4f26c Get scan working on the same as redis-cli 6.2.1 2021-04-28 13:45:06 +01:00
Cory Benfield 1168ed09f7 Add support for service discovery.
The newly-released Service Discovery framework gives us the interesting
opportunity to make RediStack aware of complex service discovery tools.
This patch supplies a simple adaptor to integrat Service Discovery with
RediStack's pooled client, allowing users to work with arbitrary service
discovery systems.
2021-03-08 17:18:38 +00:00
Cory Benfield 338a6f4aa1 Delay connection attempts without addresses.
In some circumstances users may have connection pools configured without
any SocketAddresses ready to go. This is particularly likely in service
discovery configurations. Right now, the effect of attempting to use
such a pool is two fold. First, we'll emit a bunch of error level logs
telling users we have no addresses. Second, we'll fall into the
exponential backoff phase of connection establishment.

The first property is annoying, but the second one is actively harmful.
If your construction is timed incorrectly, we'll have the awkward
problem of burning a bunch of CPU trying to create connections we know
we cannot, and then a lengthy delay after the addresses are actually
configured before we start trying to use them. That's the worst of all
worlds.

This patch adds logic to detect the attempt to create connections when
we don't have any configured addresses and delays them. This path should
improve performance and ergonomics when in this mode.
2021-03-03 21:53:45 +00:00
Nathan Harris 3c6713038d Refactor RedisCommand into a general use object
Motivation:

RediStack today represents a command as a temporary object for the purpose of writing to the channel.

While it is useful to have an object for that purpose, commands handled in this way require immediate execution
and aren't available for other purposes.

Commands can serve a better purpose as a lightweight object to support delayed execution,
so that pipeling as described in issue #63 could be possible.

Modifications:

- Add: `get` overloads for JSON codable interaction on `RedisClient`
- Add: New `RedisZRangeResultOption` type for better interactions with zrange operations that can optionally return scores
- Add: New `RedisHashFieldKey` for type-safety when working with Hash field keys much like `RedisKey`
- Change: A few API types from enums to structs for library evolution
- Change: `RedisCommandHandler` to operate on a tuple of `RESPValue, EventLoopPromise<RESPValue>` rather than `RedisCommand`
- Change: `RedisCommand` to be a generic struct with the keyword, arguments, and a transform closure to defer execution
- Change: Almost all `RedisClient` command extensions to be factory methods on `RedisCommand` instead
- Change: Many response types to be optional to avoid developers having to do `isNull` checks on their own
- Change: `RedisClient.send(command:arguments:)` to be generic with `send(_:)` as the signature
- Rename: RedisClient extensions for scan methods to be more discoverable and legible as `scanHashField`, etc.

Result:

It should be easier to support a clean pipelining API with deferred command execution,
with extensions being easier for 2nd party developers, and the maintenance overhead of all of the command extensions
should be a little lighter when it comes to changing HOW commands are sent such as adding a context parameter
2020-12-02 08:41:59 +00:00
Nathan Harris 8bf26fb661 Rename RedisKeyLifetime to be nested in RedisKey
Motivation:

`RedisKeyLifetime` already has "RedisKey" as a prefix so it naturally fits as a nested type.

Modifications:

- Change: `RedisKeyLifetime` to be nested in `RedisKey` and named `Lifetime`
- Rename: `RedisKeyLifetime.Lifetime` to `Duration`
- Deprecate: `RedisKeyLifetime` and the nested type `Lifetime`

Result:

The global namespace is a little less cluttered with the types falling naturally where they already are.
2020-11-27 14:05:14 -08:00
Nathan Harris b2367ac33e 81 -- Add Configuration types for initialization 2020-11-22 00:17:22 -08:00
Nathan Harris 61cc879efe Change RedisConnection to end subscriptions when not allowed
Motivation:

When `RedisConnection.allowSubscriptions` is set to `false`, the connection could still be in a subscription state
leaving further commands to fail slowly from a full roundtrip to Redis, rather than succeeding as expected.

This changes the implementation so that it triggers a full unsubscribe from patterns and channels when set to `false`.

Modifications:

- Change: `RedisConnection.allowSubscriptions` to call `unsubscribe()` and `punsubscribe()` when set to `false`
- Change: `RedisPubSubHandler` to prefix storage of all dictionary keys to avoid name clashes between pattern and channel subscriptions

Result:

Developers should now have more deterministic and unsurprising behavior with PubSub
in regards to subscription management and connection state.
2020-10-16 22:14:56 -07:00
Nathan Harris e0d47f7330 Fix [p]unsubscribe from all
Motivation:

The methods of unsubscribing from all channels / patterns were not working as expected as they need to be special-case handled.

Modifications:

- Change: `RedisPubSubHandler` to be special-case unsubscribe when no arguments are provided

Result:

Developers should now properly be able to unsubscribe from all channels / patterns with a single method call.
2020-10-16 20:41:27 -07:00
Nathan Harris 42e8d4b127 Allow repeated commands to same connection in pool
Motivation:

Some Redis commands are very connection specific that have impacts on future access that makes it difficult in the current
checkout-use-return cycle that `RedisConnectionPool` uses.

Developers need a way to borrow a specific connection, chain several commands together, and then return the connection to the pool.

Modifications:

- Add: `leaseConnection` method to `RedisConnectionPool` which provides a connection from the pool and returns it after a provided closure's ELF resolves
- Add: `allowSubscriptions` property to `RedisConnection` for controlling the ability to make PubSub subscriptions
- Add: `RedisClientError.pubsubNotAllowed` case for when `RedisConnection.allowSubscriptions` is set to `false` and a subscription was still attempted

Result:

Developers should now have an "escape hatch" with `RedisConnectionPool` to do limited exclusive chains of operations on a specific connection.
2020-10-15 13:39:58 -07:00
Nathan Harris 56f0ab0bc0 Add test case for RedisConnectionPool connectionRetryTimeout 2020-10-06 20:19:26 -07:00
Nathan Harris e7b597fc65 Add support for PubSub
Motivation:

One of the great features of Redis is being able to subscribe and receive messages published to specific channels
as a way of acting as a message queue for processing jobs.

PubSub requires a specific understanding of the connection model that can only be implemented directly in this library.

Modifications:

- Add: `RedisPubSubHandler` to sit in front of `RedisCommandHandler` to manage subscription callbacks and Redis registration
- Add: `publish` and the `pubsub` commands
- Add: `addPubSubHandler` extension to `NIO.Channel`
- Add: Type-safe String wrapper of `RedisChannelName` for PubSub methods
- Add: `pubsubSubscriptionNotFound` error case
- Add: `isSubscribed` property to `RedisConnection`
- Add: `availableConnectionCount` and `leasedConnectionCount` properties to `RedisConnectionPool`
- Add: Metrics for PubSub
- Add: `makeNewPool` factory method to `RedisConnectionPoolIntegrationTestCase`
- Change: `RedisClient` to require methods for PubSub management, as they are intrinsicly tied to the client's connection model
- Change: Parsing of `PING` response for handling special case in PubSub mode
- Rename: `ActiveConnectionGauge` to `RedisMetrics.IncrementalGauge`

Result:

Developers will now be able to use Redis in PubSub mode with both connections and pools.

This resolves #6
2020-09-29 22:23:44 -07:00
Nathan Harris 90244e327b Refactor Logging implementation
Motivation:

The original implementation of Logging was done in more haste than should have been, without proper attention given to the semantic requirements.

As the Swift ecosystem has matured a bit, lessons have been learned on handling metadata and passing of external context into internal subcomponents.

A mixture of the "protocol-based context passing" and "explicit context passing" patterns have been adopted.

Both patterns are more fully described in the Swift forum discussion: https://forums.swift.org/t/the-context-passing-problem/39162

Modifications:

- Add: `RedisLogging` namespace with references to static keys and labels that are used for Logging throughout the library
- Add: `Logger` static computed properties to access the Logger prototypes used in connection and connection pools
- Add: `RedisClientWithUserContext` protocol and `UserContextRedisClient` types to assist with wrapping client types for custom logger contexts
- Remove: `logger` property from `RedisClient` requirements
- Change: Many log statements to have higher or lower log levels for their appropriate context
- Change: `RedisConnection` and `RedisConnectionPool` to conform to `RedisClientWithUserContext`
- Change: `logging(to:)` protocol requirement to return a `RedisClient` existential
- Change: ConnectionPool to explicitly pass a logger instance around for pooling methods

Result:

Logging in RediStack will now have a stronger contract of where and how logs will be generated and which context will be used.

Fixes #79 and #74
2020-08-31 22:07:59 -07:00
Nathan Harris 5c4fe9619b Disable logging unit tests until they are fixed 2020-08-30 14:42:52 -07:00
Nathan Harris 610915a909 Add new 'RedisTypes' module
Motivation:

Redis is written in C, so even though it has concepts of "types" such as SortedSet or List
its commands are all "free-floating" functions.

This can make it unfamiliar for those new to Redis to work within its systems and understand the relation of all of the commands.

RediStack can improve this by giving a way of having a consistent reference to a Redis type and all of its associated methods.

Modifications:

- Add: New library product called "RedisTypes"
- Add: First type to "RedisTypes", `RedisSet`

Result:

Newcomers to Redis will have an easier time getting familiar with the APIs and working with its types by having wrappers that
provide a familiar Swift Standard Library API tailored to Redis APIs.
2020-08-30 14:33:21 -07:00
Nathan Harris d2fcb61f56 Drop support for Swift 5.0 2020-08-09 16:34:59 -07:00
Nathan Harris cc3add5dc2 68 -- Catch situations where the remote connection is closed 2020-08-01 23:05:33 -07:00
George Barnett e7b451c42f Add SETEX and PSETEX commands
Motivation:

The SETEX and PSETEX commands are missing.

Modifications:

- Add SETEX command
- Add PSETEX command
- Add integration tests

Result:

Users can atomically set a key with an expire
2020-06-04 17:24:10 +01:00
George Barnett ddfc7b0ae0 Add SET options
Motivation:

SET has a range of options for setting expirations and conditionally
setting a value.

Modification:

- Add another `set` function with a range of options. Options are
  modelled as `struct`s backed by private `enum`s to allow additional
  options to be added without breaking API.
- Added tests

Result:

Options may be specified with `set`, and resolves #67
2020-06-04 16:01:02 +00:00
Lukasa 5dbd716acf Implement a simple Redis Connection Pool.
Motivation:

Users of Redis will frequently want to be able to run queries in
parallel, while bounding the number of connections they use. They will
also often want to be able to reuse connections, without having to
arrange to manage those connections themselves. These are jobs usually
done by a Connection Pool.

This new connection pool will conform to `RedisClient` so a pool of clients and a single connection are interchangeable.

Connection Pools come in a wide range of shapes and sizes. In NIO
applications and frameworks, there are a number of questions that have
to be answered by any pool implementation:

1. Is the pool safe to share across EventLoops: that is, is its
   interface thread-safe?
2. Is the pool _tied_ to an EventLoop: that is, can the pool return
   connections that belong on lots of event loops, or just one?
3. If the pool is not tied to an EventLoop, is it possible to influence
   its choice about what event loop it uses for a given connection?

Question 1 is straightforward: it is almost always a trivial win to
ensure that the public interface to a connection pool is thread-safe.
NIO makes it possible to do this fairly cheaply in the case when the
pool is only used on a single loop.

Question 2 is a lot harder. Pools that are not tied to a specific
EventLoop have two advantages. The first is that it is easier to bound
maximum concurrency by simply configuring the pool, instead of needing
to do math on the number of pools and the number of event loops. The
second is that non-tied pools can arrange to keep busy applications
close to this maximum concurrency regardless of how the application
spreads its load across loops.

However, pools that are tied to a specific EventLoop have advantages
too. The first is one of implementation simplicity. As they always serve
connections on a single EventLoop, they can arrange to have all of their
state on that event loop too. This avoids the need to acquire locks on
that loop, making internal state management easier and more obviously
correct without having to worry about how long locks are held for.

The second advantage is that they can be used for latency sensitive
use-cases without needing to go to the work of (3). In cases where
latency is very important, it can be valuable to ensure that any Channel
that needs a connection can get one on the same event loop as itself.
This avoids the need to thread-hop in order to communicate between the
pooled connection and the user connection, reducing the latency of
operations.

Given the simplicity and latency benefits (which we deem particularly
important for Redis use-cases), we concluded that a good initial
implementation will be a pool that has a thread-safe interface, but is
tied to a single EventLoop. This allows a compact, easy-to-verify
implementation of the pool with great low-latency performance and simple
implementation logic, that can still be accessed from any EventLoop in
cases when latency is not a concern.

Modifications:

- Add new internal `ConnectionPool` object
- Add new `RedisConnectionPool` object
- Add new `RedisConnectionPoolError` type
- Add tests for new types

Results:

Users will have access to a pooled Redis client.
2020-06-03 16:43:10 +00:00
George Barnett 4b06ece03a Add SETNX command
Motivation:

The SETNX command is missing.

Modifications:

- Add SETNX command
- Add integration test

Result:

Users can set a key only if it does not already exist
2020-06-02 11:36:00 +01:00
George Barnett 4cd5855762 Add TTL and PTTL commands
Motivation:

The TTL and PTTL commands are missing.

Modifications:

- Add TTL and PTTL commands
- Add integration tests

Result:

- Users can query the ttl in seconds or milliseconds of a key
2020-05-29 12:40:35 +00:00
George Barnett 123d9c94fc Add EXISTS command
Motivation:

The EXISTS command was missing.

Modifications:

- Add 'EXISTS' to basic commands
- Add integration tests

Result:

The existence of a key can be checked.
2020-05-29 02:00:30 +00:00
Cory Benfield 2211dbf36b Detect and throw on invalid integer.
Motivation:

parseInteger did not distinguish between not having enough bytes for an
integer and not being able to parse the integer that was present. This
was a bit tricky for code internally, where some call sites had extra
code looking for spooky action at a distance in order to determine if
the integer failed to parse.

This is unnecessary: parseInteger is sufficiently aware of what's going
on to address this problem itself.

Modifications:

- Added a new parser error (acceptable as we haven't tagged 1.0 yet).
- Throw it from parseInteger if the integer is invalid.

Result:

parseInteger clearly communicates if the integer failed to parse.
2020-05-27 16:00:20 +00:00
Cory Benfield 638fbb0754 Clean up indexing of ByteBufferView
Motivation:

ByteBufferView is not zero indexed, but parseSimpleString assumes it is.

Modifications:

- Correctly compute on the distance between two indices.
- New, somewhat contrived, test case.

Result:

No functional change: because RediStack assumes the remote peer will
always correctly terminate with /r/n, there is no point at which this
code could misbehave in the current implementation. However, with small
changes it is possible to trigger it, as the new test demonstrates.
2020-05-27 10:44:38 +01:00
Nathan Harris 83f8b78c2e Disable additional scan command unit tests until #23 is fixed 2020-03-19 19:43:21 -07:00
Nathan Harris 41f9377d31 Refine Redis Command API
Motivation:

It was noticed that many of the commands are cumbersome to use with boilerplate type casting for each use that can be simplified within the library
by doing type conversion before returning the value to an end user.

Modifications:

Many APIs that return a `RESPValue` now have overloads to provide a `RESPValueConvertible` type that the value will be turned into before being returned.

For a few APIs that returned `RESPValue`, they did so as an Optional. Those APIs have been changed to always provide a `RESPValue` and return `.null` in cases where `nil` was returned.

In addition, the `@inlinable` attribute has been removed from any non-generic command API.

Result:

Developers should have less code boilerplate for turning values from `RESPValue` to their desired type with many commands.
2020-03-19 19:30:54 -07:00
Nathan Harris 86f2eb69c9 Disable Scan command unit test until #23 is completed 2020-03-19 16:17:35 -07:00
Nathan Harris 249999851e Rework SortedSet and List range APIs
Motivation:

The SortedSet and List range commands (LTRIM, LRANGE, ZRANGE, etc.) are stringly-based and not flexible with Swift syntax.

Modifications:

- Add overloads of LTRIM that support the gambit of Range Standard Library types
- Rework LRANGE to mirror LTRIM method signatures
- Rework ZScore Range based commands to be more type-safe with `RedisZScoreBound` enum
- Rework ZLex Range based commands to be more type-safe with `RedisZLexBound` enum
- Rework ZCOUNT, ZLEXCOUNT, ZRANGE, ZREVRANGE, ZREMRANGEBYLEX, ZREMRANGEBYRANK, ZREMRANGEBYSCORE methods to be more type-safe and support Swift Range syntax

Result:

Working with SortedSet ranges should be much more type safe, and expressive with Swift's Range syntax.
2019-12-30 17:17:25 -08:00