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
- Alphabetic
- By Inheritance
- Hotswap
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
-
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 oldR
whenclear is called
. Similarly, callingclear
after the lifetime of thisHotswap
results in an error. -
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 thisHotswap
(which is itself tracked byResource
) is over.Since
swap
closes the old resource immediately, you need to ensure that no code is using the oldR
whenswap
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 byswap
.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
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native() @HotSpotIntrinsicCandidate()
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
Deprecated Value Members
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] ) @Deprecated @deprecated
- Deprecated
(Since version ) see corresponding Javadoc for more information.