|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 Typelevel |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cats.effect.std |
| 18 | + |
| 19 | +import cats.effect.kernel.{Concurrent, MonadCancel, Ref, Resource} |
| 20 | +import cats.effect.kernel.Resource.ExitCase.Succeeded |
| 21 | +import cats.syntax.all._ |
| 22 | + |
| 23 | +/** |
| 24 | + * A concurrent data structure that exposes a linear sequence of `R` resources as a single |
| 25 | + * [[cats.effect.kernel.Resource]] in `F` without accumulation. |
| 26 | + * |
| 27 | + * A [[NonEmptyHotswap]] is allocated within a [[cats.effect.kernel.Resource]] that dictates the |
| 28 | + * scope of its lifetime. After creation, a `Resource[F, R]` can be swapped in by calling |
| 29 | + * [[swap]]. The newly acquired resource is returned and is released either when the |
| 30 | + * [[NonEmptyHotswap]] is finalized or upon the next call to [[swap]], whichever occurs first. |
| 31 | + * |
| 32 | + * The following diagram illustrates the linear allocation and release of three resources `r1`, |
| 33 | + * `r2`, and `r3` cycled through [[NonEmptyHotswap]]: |
| 34 | + * |
| 35 | + * {{{ |
| 36 | + * create(r1) ----- swap(r2) ---- swap(r3) ---- X |
| 37 | + * | | | | |
| 38 | + * r1 acquired | | | |
| 39 | + * r2 acquired | | |
| 40 | + * r1 released r3 acquired | |
| 41 | + * r2 released | |
| 42 | + * r3 released |
| 43 | + * }}} |
| 44 | + * |
| 45 | + * [[NonEmptyHotswap]] is particularly useful when working with effects that cycle through |
| 46 | + * resources, like writing bytes to files or rotating files every N bytes or M seconds. Without |
| 47 | + * [[NonEmptyHotswap]], such effects leak resources: on each file rotation, a file handle or |
| 48 | + * some internal resource handle accumulates. With [[NonEmptyHotswap]], the only registered |
| 49 | + * resource is the [[NonEmptyHotswap]] itself, and each file is swapped in only after swapping |
| 50 | + * the previous one out. |
| 51 | + * |
| 52 | + * Replaces the deprecated [[Hotswap]] with a safer API. |
| 53 | + */ |
| 54 | +sealed trait NonEmptyHotswap[F[_], R] { |
| 55 | + |
| 56 | + /** |
| 57 | + * Allocates a new resource and closes the previous one. |
| 58 | + * |
| 59 | + * When the lifetime of the [[NonEmptyHotswap]] is completed, the resource allocated by the |
| 60 | + * most recent [[swap]] will be finalized. |
| 61 | + * |
| 62 | + * [[swap]] finalizes the previous resource immediately, so users must ensure that the old `R` |
| 63 | + * is not used thereafter. Failure to do so may result in an error on the _consumer_ side. In |
| 64 | + * any case, no resources will be leaked. |
| 65 | + * |
| 66 | + * To access the current resource, use [[get]], which guarantees that it will not be released |
| 67 | + * while it is being used. |
| 68 | + * |
| 69 | + * If [[swap]] is called after the lifetime of the [[NonEmptyHotswap]] is over, it will raise |
| 70 | + * an error, but will ensure that all resources are finalized before returning. |
| 71 | + */ |
| 72 | + def swap(next: Resource[F, R]): F[Unit] |
| 73 | + |
| 74 | + /** |
| 75 | + * Gets the current resource. The returned resource is guaranteed to be available for the |
| 76 | + * duration of the returned resource. |
| 77 | + */ |
| 78 | + def get: Resource[F, R] |
| 79 | +} |
| 80 | + |
| 81 | +object NonEmptyHotswap { |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a new [[NonEmptyHotswap]] initialized with the specified resource, which represents |
| 85 | + * a [[cats.effect.kernel.Resource]] that can be swapped during the lifetime of this |
| 86 | + * [[NonEmptyHotswap]]. |
| 87 | + */ |
| 88 | + def apply[F[_], R](initial: Resource[F, R])( |
| 89 | + implicit F: Concurrent[F]): Resource[F, NonEmptyHotswap[F, R]] = |
| 90 | + Resource.eval(Semaphore[F](Long.MaxValue)).flatMap { semaphore => |
| 91 | + sealed abstract class State |
| 92 | + case class Acquired(r: R, fin: F[Unit]) extends State |
| 93 | + case object Finalized extends State |
| 94 | + |
| 95 | + def initialize: F[Ref[F, State]] = |
| 96 | + F.uncancelable { poll => |
| 97 | + poll(initial.allocated).flatMap { |
| 98 | + case (r, fin) => |
| 99 | + exclusive.mapK(poll).onCancel(Resource.eval(fin)).surround { |
| 100 | + F.ref(Acquired(r, fin)) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + def finalize(state: Ref[F, State]): F[Unit] = |
| 106 | + state.getAndSet(Finalized).flatMap { |
| 107 | + case Acquired(_, finalizer) => exclusive.surround(finalizer) |
| 108 | + case Finalized => raise("NonEmptyHotswap already finalized") |
| 109 | + } |
| 110 | + |
| 111 | + def raise[A](message: String): F[A] = |
| 112 | + F.raiseError[A](new IllegalStateException(message)) |
| 113 | + |
| 114 | + def exclusive: Resource[F, Unit] = |
| 115 | + Resource.makeFull[F, Unit](poll => poll(semaphore.acquireN(Long.MaxValue)))(_ => |
| 116 | + semaphore.releaseN(Long.MaxValue)) |
| 117 | + |
| 118 | + Resource.make(initialize)(finalize).map { state => |
| 119 | + new NonEmptyHotswap[F, R] { |
| 120 | + |
| 121 | + override def swap(next: Resource[F, R]): F[Unit] = |
| 122 | + F.uncancelable { poll => |
| 123 | + poll(next.allocated).flatMap { |
| 124 | + case (r, fin) => |
| 125 | + exclusive.mapK(poll).onCancel(Resource.eval(fin)).surround { |
| 126 | + swapFinalizer(Acquired(r, fin)) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + override def get: Resource[F, R] = |
| 132 | + Resource.makeFull[F, R] { poll => |
| 133 | + poll(semaphore.acquire) *> // acquire shared lock |
| 134 | + state.get.flatMap { |
| 135 | + case Acquired(r, _) => F.pure(r) |
| 136 | + case _ => raise("Hotswap already finalized") |
| 137 | + } |
| 138 | + }(_ => semaphore.release) |
| 139 | + |
| 140 | + private def swapFinalizer(next: State): F[Unit] = |
| 141 | + state.flatModify { |
| 142 | + case Acquired(_, fin) => |
| 143 | + next -> fin |
| 144 | + case Finalized => |
| 145 | + val fin = next match { |
| 146 | + case Acquired(_, fin) => fin |
| 147 | + case _ => F.unit |
| 148 | + } |
| 149 | + Finalized -> (fin *> raise[Unit]("Cannot swap after finalization")) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Creates a [[NonEmptyHotswap]] of `Resource[F, Option[R]]` containing a `None` |
| 157 | + */ |
| 158 | + def empty[F[_], R](implicit F: Concurrent[F]): Resource[F, NonEmptyHotswap[F, Option[R]]] = |
| 159 | + apply[F, Option[R]](Resource.pure(none)) |
| 160 | + |
| 161 | + implicit final class NonEmptyHotswapOptionalResourcesOpt[F[_], R]( |
| 162 | + private val hs: NonEmptyHotswap[F, Option[R]]) |
| 163 | + extends AnyVal { |
| 164 | + |
| 165 | + /** |
| 166 | + * When the [[cats.effect.kernel.Resource]] contained by a [[NonEmptyHotswap]] is wrapped in |
| 167 | + * an [[scala.Option]] it is not desirable to prevent calls to [[NonEmptyHotswap.swap]] when |
| 168 | + * the [[cats.effect.kernel.Resource Resource]] contains [[scala.None]]. |
| 169 | + * |
| 170 | + * [[getOpt]] preserves this behavior from [[Hotswap.get]] |
| 171 | + */ |
| 172 | + def getOpt(implicit F: MonadCancel[F, Throwable]): Resource[F, Option[R]] = |
| 173 | + Resource.applyFull[F, Option[R]] { poll => |
| 174 | + poll(hs.get.allocatedCase).flatMap { |
| 175 | + case (None, fin) => fin(Succeeded) *> F.pure((None, _ => F.unit)) |
| 176 | + case (r, fin) => F.pure((r, fin)) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + def clear: F[Unit] = hs.swap(Resource.pure(none)) |
| 181 | + } |
| 182 | +} |
0 commit comments