Packages

sealed trait Hotswap[F[_], R] extends AnyRef

Supports treating a linear sequence of resources as a single resource.

A Hotswap[F, R] instance is created as a Resource and hence, has a lifetime that is scoped by the Resource. After creation, a Resource[F, R] can be swapped in to the Hotswap by calling swap. The acquired resource is returned and is finalized when the Hotswap is finalized or upon the next call to swap, whichever occurs first.

For example, the sequence of three resources r1, r2, r3 are shown in the following diagram:

>----- swap(r1) ---- swap(r2) ---- swap(r3) ----X
|        |             |             |          |
Creation |             |             |          |
        r1 acquired    |             |          |
                      r2 acquired    |          |
                      r1 released   r3 acquired |
                                    r2 released |
                                               r3 released

This class is particularly useful when working with pulls that cycle through resources -- e.g., writing bytes to files, rotating files every N bytes or M seconds. Without Hotswap, such pulls leak resources -- on each file rotation, a file handle or at least an internal resource reference accumulates. With Hotswap, the Hotswap instance is the only registered resource and each file is swapped in to the Hotswap.

Usage typically looks something like:

Stream.resource(Hotswap(mkResource)).flatMap { case (hotswap, r) =>
  // Use r, call hotswap.swap(mkResource) as necessary
}

See fs2.io.file.writeRotate for an example of usage.

Source
Hotswap.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Hotswap
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def clear: F[Unit]

    Runs the finalizer of the current resource, if any, and restores this Hotswap to its initial state.

    Runs the finalizer of the current resource, if any, and restores this Hotswap to its initial state.

    Like swap, you need to ensure that no code is using the old R when clear is called. Similarly, calling clear after the lifetime of this Hotswap results in an error.

  2. abstract def swap(next: Resource[F, R]): F[R]

    Allocates a new resource, closes the last one if present, and returns the newly allocated R.

    Allocates a new resource, closes the last one if present, and returns the newly allocated R.

    If there are no further calls to swap, the resource created by the last call will be finalized when the lifetime of this Hotswap (which is itself tracked by Resource) is over.

    Since swap closes the old resource immediately, you need to ensure that no code is using the old R when swap is called. Failing to do so is likely to result in an error on the _consumer_ side. In any case, no resources will be leaked by swap.

    If you try to call swap after the lifetime of this Hotswap is over, swap will fail, but it will ensure all resources are closed, and never leak any.

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  8. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  9. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  10. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  11. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  12. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  13. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  14. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  15. def toString(): String
    Definition Classes
    AnyRef → Any
  16. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  17. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  18. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

Inherited from AnyRef

Inherited from Any

Ungrouped