-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a fairness
parameter
#564
Changes from all commits
0ccac6f
88596f5
e97d4d1
93e1700
2ca9332
3a26b5d
00efb07
090fd56
413476b
fb5d68e
6f78b94
7e87fc3
9dfd7df
63b80ed
277410a
867c867
89a6c7b
4d529c5
3fbf862
5873581
325172e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2019 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package org.typelevel.keypool | ||
|
||
/** | ||
* Fairness defines the order in which pending requests acquire a connection from the pool. | ||
* | ||
* Lifo will process requests in last-in-first-out order. Fifo will process requests in | ||
* first-in-first-out order. | ||
*/ | ||
sealed trait Fairness extends Product with Serializable | ||
object Fairness { | ||
case object Lifo extends Fairness | ||
case object Fifo extends Fairness | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (c) 2019 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package org.typelevel.keypool.internal | ||
|
||
import cats.syntax.all._ | ||
import cats.effect.kernel.syntax.all._ | ||
import cats.effect.kernel._ | ||
import scala.collection.immutable.{Queue => ScalaQueue} | ||
import scala.annotation.nowarn | ||
|
||
import org.typelevel.keypool.Fairness | ||
|
||
/** | ||
* RequestSemaphore moderates access to pooled connections by setting the number of permits | ||
* available to the total number of connections. This is a custom semaphore implementation that only | ||
* provides the `permit` operation. Additionally it takes a [[Fairness]] parameter, used to toggle | ||
* the order in which requests acquire a permit. | ||
* | ||
* Derived from cats-effect MiniSemaphore | ||
* https://github.com/typelevel/cats-effect/blob/v3.5.4/kernel/shared/src/main/scala/cats/effect/kernel/MiniSemaphore.scala#L29 | ||
*/ | ||
private[keypool] abstract class RequestSemaphore[F[_]] { | ||
samspills marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just a nit but I'm struggling to understand what
Maybe it's more likely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I struggled a lot with naming here. It's a |
||
def permit: Resource[F, Unit] | ||
} | ||
|
||
private[keypool] object RequestSemaphore { | ||
private trait BackingQueue[CC[_], A] { | ||
def cleanup(fa: CC[A], elem: A): CC[A] | ||
def offer(fa: CC[A], elem: A): CC[A] | ||
def take(fa: CC[A]): (CC[A], A) | ||
def nonEmpty(fa: CC[A]): Boolean | ||
} | ||
|
||
private implicit def listQueue[A <: AnyRef]: BackingQueue[List, A] = new BackingQueue[List, A] { | ||
def cleanup(fa: List[A], elem: A) = fa.filterNot(_ eq elem) | ||
def offer(fa: List[A], elem: A) = elem :: fa | ||
def take(fa: List[A]) = (fa.tail, fa.head) | ||
def nonEmpty(fa: List[A]) = fa.nonEmpty | ||
} | ||
|
||
private implicit def queueQueue[A <: AnyRef]: BackingQueue[ScalaQueue, A] = | ||
new BackingQueue[ScalaQueue, A] { | ||
def cleanup(fa: ScalaQueue[A], elem: A) = fa.filterNot(_ eq elem) | ||
def offer(fa: ScalaQueue[A], elem: A) = fa :+ elem | ||
def take(fa: ScalaQueue[A]) = (fa.tail, fa.head) | ||
def nonEmpty(fa: ScalaQueue[A]) = fa.nonEmpty | ||
} | ||
|
||
private case class State[CC[_], A](waiting: CC[A], permits: Int)(implicit | ||
@nowarn ev: BackingQueue[CC, A] | ||
) | ||
|
||
def apply[F[_]](fairness: Fairness, numPermits: Int)(implicit | ||
F: Concurrent[F] | ||
): F[RequestSemaphore[F]] = { | ||
|
||
def require(condition: Boolean, errorMessage: => String): F[Unit] = | ||
if (condition) F.unit else new IllegalArgumentException(errorMessage).raiseError[F, Unit] | ||
|
||
require(numPermits >= 0, s"numPermits must be nonnegative, was: $numPermits") *> { | ||
fairness match { | ||
case Fairness.Fifo => | ||
F.ref(State(ScalaQueue.empty[Deferred[F, Unit]], numPermits)).map(semaphore(_)) | ||
case Fairness.Lifo => | ||
F.ref(State(List.empty[Deferred[F, Unit]], numPermits)).map(semaphore(_)) | ||
} | ||
} | ||
} | ||
|
||
private def semaphore[F[_], CC[_]]( | ||
state: Ref[F, State[CC, Deferred[F, Unit]]] | ||
)(implicit | ||
F: GenConcurrent[F, _], | ||
B: BackingQueue[CC, Deferred[F, Unit]] | ||
): RequestSemaphore[F] = { | ||
new RequestSemaphore[F] { | ||
private def acquire: F[Unit] = | ||
F.deferred[Unit].flatMap { wait => | ||
val cleanup = state.update { case s @ State(waiting, permits) => | ||
if (B.nonEmpty(waiting)) | ||
State(B.cleanup(waiting, wait), permits) | ||
else s | ||
} | ||
|
||
state.flatModifyFull { case (poll, State(waiting, permits)) => | ||
if (permits == 0) { | ||
State(B.offer(waiting, wait), permits) -> poll(wait.get).onCancel(cleanup) | ||
} else | ||
State(waiting, permits - 1) -> F.unit | ||
} | ||
} | ||
|
||
private def release: F[Unit] = | ||
state.flatModify { case State(waiting, permits) => | ||
if (B.nonEmpty(waiting)) { | ||
val (rest, next) = B.take(waiting) | ||
State(rest, permits) -> next.complete(()).void | ||
} else | ||
State(waiting, permits + 1) -> F.unit | ||
} | ||
|
||
def permit: Resource[F, Unit] = | ||
Resource.makeFull((poll: Poll[F]) => poll(acquire))(_ => release) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does Typelevel really need to explicitly license code from different projects to each other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure, but especially since keypool and cats-effect are licensed differently I thought so (I would defer to @rossabaker's wisdom here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I didn't write it, and I copy it into a new project, I credit it, even if I'm a maintainer on both projects. It's probably not strictly necessary in this case, but once we play fast and loose with attribution, it's a slippery slope toward working for Microsoft1.
Footnotes
fuckers ↩