Hotswap
Supports treating a linear sequence of resources as a single resource.
A
a lifetime that is scoped by the
can be swapped in to the
is returned and is finalized when the
call to
Hotswap[F, R] instance is created as a Resource and hence, hasa 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 resourceis returned and is finalized when the
Hotswap is finalized or upon the nextcall to
swap, whichever occurs first.For example, the sequence of three resources
following diagram:
r1, r2, r3 are shown in thefollowing 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
or at least an internal resource reference accumulates. With
instance is the only registered resource and each file is swapped in to the
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 handleor at least an internal resource reference accumulates. With
Hotswap, the Hotswapinstance 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
}
}}}
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.- Companion
- object
class Object
trait Matchable
class Any
Value members
Methods
Allocates a new resource, closes the last one if present, and
returns the newly allocated
returns the newly allocated
R.If there are no further calls to
the last call will be finalized when the lifetime of
this
swap, the resource created bythe last call will be finalized when the lifetime of
this
Hotswap (which is itself tracked by Resource) is over.Since
ensure that no code is using the old
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 closes the old resource immediately, you need toensure 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
over,
closed, and never leak any.
Hotswap isover,
swap will fail, but it will ensure all resources areclosed, and never leak any.