View Source Membrane.ResourceGuard (Membrane Core v1.0.1)

Utility for handling resources that must be cleaned up after use.

This utility uses a separate process that allows registering functions that are called when the owner process (passed to start_link/1) dies for any reason. Each Membrane component spawns its resource guard on startup and provides it via callback context.

Example

def handle_setup(ctx, state) do
  resource = MyResource.create()

  Membrane.ResourceGuard.register(ctx.resource_guard, fn ->
    MyResource.cleanup(resource)
  end)

  {:ok, %{state | my_resource: resource}}
end

Summary

Types

t()

Utility for handling resources that must be cleaned up after use

Functions

Returns a specification to start this module under a supervisor.

Executes all cleanup functions registered in the resource gurard.

Executes cleanup functions registered with the specifc tag.

Registers a resource cleanup function in the resource guard.

Unregisters a resource cleanup function from the resource guard.

Types

@type t() :: pid()

Utility for handling resources that must be cleaned up after use

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

@spec cleanup(t()) :: :ok

Executes all cleanup functions registered in the resource gurard.

Link to this function

cleanup(resource_guard, tag)

View Source
@spec cleanup(t(), tag :: any()) :: :ok

Executes cleanup functions registered with the specifc tag.

If many cleanup functions are registered with the same tag, all of them are executed.

Link to this function

register(resource_guard, cleanup_function, opts \\ [])

View Source
@spec register(
  t(),
  (-> any()),
  opts :: [tag: any(), timeout: milliseconds :: non_neg_integer()]
) :: tag
when tag: any()

Registers a resource cleanup function in the resource guard.

Registered functions are called in the order reverse to the registration order. Function returns a tag of the registered cleanup function. Tag can be passed under a :tag key in opts. Many functions can be registered with the same tag. If there is no :tag key in opts, tag will be result of make_ref().

Link to this function

start_link(owner_pid \\ self())

View Source
@spec start_link(owner_pid :: pid()) :: {:ok, t()}
Link to this function

unregister(resource_guard, tag)

View Source
@spec unregister(t(), tag :: any()) :: :ok

Unregisters a resource cleanup function from the resource guard.

All cleanup functions with tag tag are deleted.