Commit Graph

221 Commits

Author SHA1 Message Date
Franz Busch 6205f04c9b Add availability to SwiftPolyfill.swift
# Motivation

We are missing an availability annotation on the `AsyncStream` extension that we have written to polyfill with older Swift versions.
2023-12-05 10:32:04 +00:00
Fabian Fett a476f69cbb Add missing availibility flags (#99) 2023-11-15 11:26:46 +01:00
hamzahrmalik 5a81dd73c9 Improve error messages (#97) 2023-11-15 10:55:50 +01:00
Fabian Fett 10c8c6b813 Add RedisClusterShardDescriptionProtocol (#93) 2023-09-20 22:29:32 +02:00
Fabian Fett f837e2929f Add DiscardingTaskGroup and Stream polyfills for cluster support (#94) 2023-09-19 21:16:16 +02:00
Fabian Fett 1b69101da1 Add RedisClusterNodeDescription and RedisClusterNodeID (#92) 2023-09-19 15:29:38 +02:00
Fabian Fett 41b77775e1 Add RedisHashSlot (#91) 2023-09-18 09:54:14 +02:00
Joannis Orlandos 9118427c1c Change the 'debug' log statement when a connection is grabbed from a connectionpool to 'trace' (#88) 2023-09-15 10:44:10 +02:00
Fabian Fett 30a43b0195 Close connection pool even if some connections are leased/in creation (#86)
Currently closing a pool, that has leased connections or currently creates connections fails. Such a close attempt brings the pool in an unrecoverable closing state and may lead to crashes. This patch changes the pool shutdown behavior to allow closing the pool even if the pool is not a predefined state.
2023-08-04 16:06:16 +02:00
Fabian Fett 2d626c89d0 Cherry pick GitLab 187: Graceful connection close without sending QUIT command (#85)
Cherry pick: https://gitlab.com/swift-server-community/RediStack/-/merge_requests/187

> Note: Clients should not use this command. Instead, clients should simply close the connection when they're not used anymore. Terminating a connection on the client side is preferable, as it eliminates TIME_WAIT lingering sockets on the server side.

https://redis.io/commands/quit/
2023-08-04 11:54:52 +02:00
Fabian Fett 99cd4a4642 Mark RedisConnectionPool as final (#83) 2023-08-03 13:02:30 +02:00
Fabian Fett 43ea6af862 Make RedisConnection.Configuration.defaultPort thread safe (#81)
`RedisConnection.Configuration.defaultPort` is currently unprotected shared mutable state. To ensure thread safety this patch adds an atomic to back this property. Since setting the `defaultPort` doesn't make much sense for adopters, we deprecate the setter. Lastly we mark `RedisConnection.Configuration` as `Sendable`.
2023-07-08 14:58:55 +02:00
Fabian Fett 97dc6dd15e Split up Configuration file into two: RedisConnectionPool & RedisConnection (#78) 2023-07-07 10:24:47 +02:00
Fabian Fett 95b2d06688 Move RESP3TokenDecoder into its own file (#75) 2023-07-06 02:28:26 -07:00
Fabian Fett ae9f06b503 Move ConnectionPool files into the ConnectionPool folder (#76)
Let's move files that are connected to ConnectionPool into the ConnectionPool folder. Also rename `ConnectionPoolErrors.swift` to `RedisConnectionPoolError.swift` to match the type that is defined within.
2023-07-06 11:24:45 +02:00
Joannis Orlandos 9225dc306c Introduces a RESP3Token wrapper struct around ByteBuffer (#71)
Co-authored-by: Fabian Fett <fabianfett@apple.com>
2023-07-05 17:51:14 +02:00
Fabian Fett 89a29d457c Add support for usernames (#72)
Redis 6.0 adds the ability to specify a username when sending an `AUTH` command. This patch adds this capability to RediStack.
2023-07-03 21:23:07 +02:00
Fabian Fett 3001e41ca6 Remove Context, use Logger everywhere instead (#70)
We have an internal `typealias Context = Logging.Logger`. This is quite confusing. Remove the typealias, use logger everywhere instead.
2023-07-03 12:12:45 +02:00
Fabian Fett 018a9b9626 Add RedisCommandEncoder (#69)
We want to be able to efficiently encode Redis commands that are sent to a server. This patch adds a new `RedisCommandEncoder` that allows us to efficiently create Redis commands without needing to go through RESP representations, that may require us to create Arrays. Further it introduces a `RESP3BlobStringEncodable` that must be implement to send blob strings using `RedisCommandEncoder`. This patch also adds implementations for `String` and  `ByteBuffer` for the new `RESP3BlobStringEncodable` protocol.
2023-07-03 10:28:39 +02:00
Fabian Fett d7c4121e4b README.md: Switch mirroring around (#56) 2023-06-20 13:33:15 +02:00
Fabian Fett 11b4673523 Conform RedisByteDecoder to NIOSingleStepByteToMessageDecoder (#63) 2023-06-19 10:52:59 +02:00
Fabian Fett ef5fdf7e63 Delay connection attempts without addresses. (#64)
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.

Authored-by: Cory Benfield <lukasa@apple.com>
2023-06-19 10:37:15 +02:00
Fabian Fett 5fadd5fe06 Require Swift 5.6 (#65) 2023-06-19 00:40:53 -07:00
Joannis Orlandos 2f3db6ef11 Remove reliance on the NIO umbrella module (#60) 2023-06-19 09:37:04 +02:00
Fabian Fett 10a5c3949e Add soundness and api breakage CI (#58) 2023-06-16 19:50:46 +02:00
Fabian Fett 35e0d67e84 Fix crash in tests: Use explicit RedisCommand instead of tuple (#52) 2023-06-16 19:15:42 +02:00
Marius Seufzer 43d94452a8 call onUnexpectedConnectionClose on closeFuture 2023-06-13 11:34:05 +02:00
Marius Seufzer 2fd4db68e9 fix compiler error for pre 5.7 swift versions 2023-06-12 13:19:09 +02:00
Marius Seufzer 5875335e08 Add onUnexpectedConnectionClose callback to pool
Backports `onUnexpectedConnectionClose` on the pool to 1.x
2023-06-09 18:58:21 +00:00
Gwynne Raskind 3bd5940b07 Make the parameter-less RedisClientError factory statics computed properties. As stored properties, they trigger Thread Sanitizer errors when multiple connections trigger the same errors (usually connectionClosed) too close together due to lazy once-only initialization. 2023-05-27 21:56:49 -05:00
Fabian Fett c85f857554 Add support for graceful shutdown to the RedisCommandHandler 2022-12-07 17:15:42 +01:00
Michael Stegeman 68ccc40414 Switch from NIOAtomic to ManagedAtomic. 2022-11-30 14:29:30 +01:00
Fabian Fett ff495ef0d8 Fix NIOLock warning
# Conflicts:
#	Package.swift
#	Sources/RediStack/RedisConnection.swift
2022-11-26 14:02:12 +01:00
Nathan Harris 5458d6476e Backdeploy #100 fix and deprecate Channel Redis pipeline APIs
This is a backdeploy of commit cfb99ba0f7.

This fixes issue #100
2022-04-23 22:58:39 -05:00
Nathan Harris ad435902f2 Update deprecation annotations to support "fixit" helpers in Xcode 2022-04-23 22:43:49 -05:00
Nathan Harris b277715a02 104 -- Use correct base method for zrevrange overload methods 2022-03-14 23:08:14 -05:00
Nathan Harris 16037bbb82 102 -- Remove usage of deprecated NIO ELF APIs 2022-01-03 12:42:58 -06:00
Nathan Harris 0c3beee7eb 95 -- Backport unexpected channel closure callback
This is a cherry-pick commit of ad316a97ac
2021-08-16 22:01:25 -07:00
Nathan Harris edadec93a1 Get pubsub numsub working
This was a cherry-pick of 3ca471b226
2021-05-04 20:59:50 -07: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 833fc33881 Fix RedisConnectionPool.leaseConnection code example 2020-10-18 18:48:46 +00: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 c8cb256b59 Add default buffer connectionRetryTimeout to avoid literal immediate timeouts
Motivation:

When trying to allow users to configure the connection retry timeout offset,
not having a value provided (deadline of `now`) caused all attempts to use the pool to fail.

Modifications:

- Change: RedisConnectionPool to always have a timeout offset defined

Result:

If users don't specify any value, then the default of 60 seconds will be used.

If users specify "nil" (or `.none`) as the timeout, then a minimum of 10 milliseconds will be used to avoid immediate timeouts

Otherwise, use the user's specified `TimeAmount` as the offset of the timeout
2020-10-06 20:06:29 -07:00
Nathan Harris 3e28e75b6a Add configuration option for RedisConnectionPool lease timeout
Motivation:

With RedisConnectionPool a timeout is provided to prevent infinite loops of
retrying connections, but right now it is hardcoded to 60 seconds.

Users of downstream projects such as Vapor are noticing a "regression" of sorts, as previously
EventLoopFutures would fail immediately if a connection was not made available.

Modifications:

- Add: `connectionRetryTimeout` parameter to `RedisConnectionPool` initializer that still defaults to 60 seconds
- Change: RedisConnectionPool to use the new parameter if available to offset a deadline from "now"

Result:

Users can now configure the connection pool to fail immediately if connections are not available.
2020-10-06 19:33:43 -07:00
Nathan Harris e858c0a66e Resolve PubSub post-commit feedback
Motivation:

To ship PubSub faster, it was merged to the master branch without a peer review. This commit is to address the critical points of feedback given in a post-commit review.

Modifications:

- Add: New RedisClientError case where a "race condition" of removing a pubsub handler and subscription can happen
- Add: New state to RedisPubSubHandler for when it has been removed from a ChannelPipeline
- Change: RedisPubSubHandler to require an `eventLoop` in its initializer
- Change: The subscribe and unsubscribe methods on RedisPubSubHandler to handle the EventLoop hopping to be thread-safe

Result:

PubSub should have a more robust and thread-safe implementation.
2020-10-01 22:30:59 -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