-
Notifications
You must be signed in to change notification settings - Fork 4
/
RedisSemaphore.scala
173 lines (145 loc) · 6.54 KB
/
RedisSemaphore.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package io.chrisdavenport.rediculous.concurrent
import cats._
import cats.syntax.all._
import cats.effect._
import cats.effect.std.Semaphore
import io.chrisdavenport.rediculous._
import io.chrisdavenport.rediculous.{RedisCommands, RedisConnection}
import io.chrisdavenport.rediculous.RedisTransaction.TxResult
import scala.concurrent.duration._
import io.chrisdavenport.rediculous.RedisPipeline
import java.util.UUID
import cats.effect.std.UUIDGen
import cats.data.NonEmptyList
import io.chrisdavenport.rediculous.RedisTransaction.TxResult.Success
import io.chrisdavenport.rediculous.RedisTransaction.TxResult.Aborted
import io.chrisdavenport.rediculous.RedisCtx.syntax.all._
trait MiniSemaphore[F[_]]{
def acquire: F[Unit]
def tryAcquire: F[Boolean]
def release: F[Unit]
def permit: Resource[F, Unit]
}
object MiniSemaphore {
def fromSemaphore[F[_]](semaphore: Semaphore[F]): MiniSemaphore[F] = new MiniSemaphore[F]{
def acquire: F[Unit] = semaphore.acquire
def tryAcquire: F[Boolean] = semaphore.tryAcquire
def release: F[Unit] = semaphore.release
def permit: Resource[F, Unit] = semaphore.permit
}
def fromRedisSemaphore[F[_]: Sync](r: RedisSemaphore.RedisBackedSemaphore[F]): MiniSemaphore[F] = new MiniSemaphore[F]{
def acquire: F[Unit] = r.acquire
def tryAcquire: F[Boolean] = r.tryAcquire
def release: F[Unit] = r.release
def permit: Resource[F, Unit] = r.withPermit
}
}
object RedisSemaphore {
def build[F[_]: Async](
redisConnection: RedisConnection[F],
semname: String, // The Name of the semaphore, also operates as the base of the redis key
limit: Long, // Total Number of Permits Allowed
timeout: FiniteDuration, // Maximum Time A Semaphore Can Be Held For. Will remove durations older than this
poll: FiniteDuration = 10.millis, // On acquire, how frequently
lockAcquireTimeout: FiniteDuration = 1.second, // How long to wait on acquiring a lock
lockTotalTimeout: FiniteDuration = 10.seconds, // How long a lock can be held total (Don't see why we'd hit this)
): F[RedisBackedSemaphore[F]] = Ref.of(List.empty[UUID]).map(
new RedisBackedSemaphore[F](redisConnection, semname, limit, timeout, poll, lockAcquireTimeout, lockTotalTimeout, _)
)
class RedisBackedSemaphore[F[_]: Async](
redisConnection: RedisConnection[F],
semname: String,
limit: Long,
timeout: FiniteDuration,
poll: FiniteDuration,
lockAcquireTimeout: FiniteDuration,
lockTotalTimeout: FiniteDuration,
ownedIdentifiers: Ref[F, List[UUID]]
){
def acquire: F[Unit] =
Concurrent[F].timeout(
tryAcquire.flatMap{
case true => Applicative[F].unit
case false => Temporal[F].sleep(poll) >> acquire
}, timeout)
def tryAcquire: F[Boolean] =
RedisLock.tryAcquireLock(redisConnection, semname,lockAcquireTimeout, lockTotalTimeout)
.flatMap{
case Some(lockIdentifier) =>
tryAcquireSemaphore(redisConnection, semname, limit, timeout).flatTap{_ =>
RedisLock.shutdownLock(redisConnection, semname, lockIdentifier)
}.flatMap{
case None => false.pure[F]
case Some(semaphoreIdentifier) =>
ownedIdentifiers.update(l => semaphoreIdentifier :: l).as(true)
}
case None => false.pure[F]
}
def release: F[Unit] = ownedIdentifiers.modify{
case Nil => (Nil, None)
case x :: xs => (xs, Some(x))
}.flatMap{
case Some(id) => releaseSemaphore(redisConnection, semname, id)
case None =>
Applicative[F].unit
}
def withPermit: Resource[F, Unit] = Resource.make(acquire)(_ => release)
}
private def tryAcquireSemaphore[F[_]:Async](
redisConnection: RedisConnection[F],
semname: String,
limit: Long,
timeout: FiniteDuration
): F[Option[UUID]] = Async[F].delay{
val now = System.currentTimeMillis().millis
val random = UUID.randomUUID()
val identifier = random.toString()
val czset = semname ++ ":owner"
val ctr = semname ++ ":counter"
def cleanup =
RedisCommands.zrem[RedisTransaction](semname, List(identifier)) *>
RedisCommands.zrem[RedisTransaction](czset, List(identifier))
val getSemaphore = (
RedisCommands.zremrangebyscore[RedisTransaction](semname, Double.NegativeInfinity, (now - timeout).toMillis.toDouble),
RedisCtx[RedisTransaction].keyed[Long](semname, NonEmptyList.of("ZINTERSTORE", czset, "2", czset, semname, "WEIGHTS", "1", "0")),
RedisCommands.incr[RedisTransaction](ctr),
).tupled.transact.run(redisConnection).flatMap{
case TxResult.Success(s@(_, _, counter)) =>
val transaction =
(
RedisCommands.zadd[RedisTransaction](semname, List((now.toMillis.toDouble, identifier))),
RedisCommands.zadd[RedisTransaction](czset, List((counter.toDouble, identifier))),
RedisCommands.zrank[RedisTransaction](czset, identifier)
).tupled
transaction.transact.run(redisConnection).flatMap{
case TxResult.Success(value@(_, _, rank)) =>
// println(
// s"""Removed ${s._1} elements from $semname
// |Stored ${s._2} elements into $czset
// |Counter now at ${s._3} at $ctr
// |
// |Added ${value._1} elements to $semname (${now.toMillis.toDouble}, ${identifier})
// |Added ${value._2} elements to $czset (${counter.toDouble}, ${identifier})
// |Current Rank of $czset is $rank
// |""".stripMargin
// )
if (rank < limit) Option(random).pure[F]
else cleanup.transact.run(redisConnection).void.as(Option.empty[UUID])
case TxResult.Aborted => cleanup.transact.run(redisConnection).void.as(Option.empty[UUID])
case TxResult.Error(value) => cleanup.transact.run(redisConnection).void.as(Option.empty[UUID])
}
case TxResult.Aborted => Option.empty[UUID].pure[F]
case TxResult.Error(e) => new Throwable(s"getSemaphore encountered an error $e").raiseError[F, Option[UUID]]
}
getSemaphore
}.flatten
private def releaseSemaphore[F[_]: Async](
redisConnection: RedisConnection[F],
semname: String,
identifier: java.util.UUID
): F[Unit] =
(
RedisCommands.zrem[RedisTransaction](semname, List(identifier.toString)) <*
RedisCommands.zrem[RedisTransaction](semname ++ ":owner", List(identifier.toString))
).transact.run(redisConnection).void
}