-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
211 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
modules/redis-client/src/main/scala/io/renku/redis/client/ConnectionCreator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client | ||
|
||
import cats.effect.{Async, Resource} | ||
import cats.syntax.all.* | ||
import dev.profunktor.redis4cats.connection.{RedisClient, RedisMasterReplica, RedisURI} | ||
import dev.profunktor.redis4cats.data.{ReadFrom, RedisCodec} | ||
import dev.profunktor.redis4cats.effect.Log | ||
import dev.profunktor.redis4cats.streams.{RedisStream, Streaming} | ||
import dev.profunktor.redis4cats.{Redis, RedisCommands} | ||
import fs2.Stream | ||
import io.lettuce.core.{ | ||
RedisCredentials, | ||
RedisURI as JRedisURI, | ||
StaticCredentialsProvider | ||
} | ||
import scodec.bits.ByteVector | ||
|
||
sealed private trait ConnectionCreator[F[_]]: | ||
|
||
def createStreamingConnection | ||
: Stream[F, Streaming[[A] =>> Stream[F, A], String, ByteVector]] | ||
|
||
def createStringCommands: Resource[F, RedisCommands[F, String, String]] | ||
|
||
object ConnectionCreator: | ||
|
||
def create[F[_]: Async: Log](cfg: RedisConfig): Resource[F, ConnectionCreator[F]] = | ||
val uri = createRedisUri(cfg) | ||
cfg.maybeMasterSet match { | ||
case None => | ||
RedisClient[F] | ||
.fromUri(RedisURI.fromUnderlying(uri)) | ||
.map(new SingleConnectionCreator(_)) | ||
case Some(masterSet) => | ||
uri.setSentinelMasterId(masterSet.value) | ||
Resource | ||
.pure[F, RedisURI](RedisURI.fromUnderlying(uri)) | ||
.map(new MasterReplicaConnectionCreator(_)) | ||
} | ||
|
||
private def createRedisUri(cfg: RedisConfig): JRedisURI = { | ||
val uri = JRedisURI.create(cfg.host.value, cfg.port.value) | ||
cfg.maybeDB.foreach(db => uri.setDatabase(db.value)) | ||
cfg.maybePassword.foreach(pass => | ||
uri.setCredentialsProvider( | ||
new StaticCredentialsProvider(RedisCredentials.just(null, pass.value)) | ||
) | ||
) | ||
uri | ||
} | ||
|
||
private class SingleConnectionCreator[F[_]: Async: Log](client: RedisClient) | ||
extends ConnectionCreator[F]: | ||
|
||
override def createStreamingConnection | ||
: Stream[F, Streaming[[A] =>> Stream[F, A], String, ByteVector]] = | ||
RedisStream | ||
.mkStreamingConnection[F, String, ByteVector](client, StringBytesCodec.instance) | ||
|
||
override def createStringCommands: Resource[F, RedisCommands[F, String, String]] = | ||
Redis[F].fromClient(client, RedisCodec.Utf8) | ||
|
||
private class MasterReplicaConnectionCreator[F[_]: Async: Log](uri: RedisURI) | ||
extends ConnectionCreator[F]: | ||
|
||
override def createStreamingConnection | ||
: Stream[F, Streaming[[A] =>> Stream[F, A], String, ByteVector]] = | ||
RedisStream | ||
.mkMasterReplicaConnection[F, String, ByteVector](StringBytesCodec.instance, uri)( | ||
None | ||
) | ||
|
||
override def createStringCommands: Resource[F, RedisCommands[F, String, String]] = | ||
RedisMasterReplica[F] | ||
.make(RedisCodec.Utf8, uri)(ReadFrom.UpstreamPreferred.some) | ||
.flatMap(Redis[F].masterReplica) |
59 changes: 59 additions & 0 deletions
59
modules/redis-client/src/main/scala/io/renku/redis/client/RedisConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.redis.client | ||
|
||
final case class RedisConfig( | ||
host: RedisHost, | ||
port: RedisPort, | ||
maybeDB: Option[RedisDB], | ||
maybePassword: Option[RedisPassword] = None, | ||
maybeMasterSet: Option[RedisMasterSet] = None | ||
) | ||
|
||
opaque type RedisHost = String | ||
object RedisHost { | ||
def apply(v: String): RedisHost = v | ||
extension (self: RedisHost) def value: String = self | ||
} | ||
|
||
opaque type RedisPort = Int | ||
object RedisPort { | ||
def apply(v: Int): RedisPort = v | ||
extension (self: RedisPort) | ||
def value: Int = self | ||
def plusOne: RedisPort = self + 1 | ||
} | ||
|
||
opaque type RedisDB = Int | ||
object RedisDB { | ||
def apply(v: Int): RedisDB = v | ||
extension (self: RedisDB) def value: Int = self | ||
} | ||
|
||
opaque type RedisPassword = String | ||
object RedisPassword { | ||
def apply(v: String): RedisPassword = v | ||
extension (self: RedisPassword) def value: String = self | ||
} | ||
|
||
opaque type RedisMasterSet = String | ||
object RedisMasterSet { | ||
def apply(v: String): RedisMasterSet = v | ||
extension (self: RedisMasterSet) def value: String = self | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
modules/redis-client/src/main/scala/io/renku/redis/client/RedisUrl.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.