otp

package
v0.0.0-...-74b8615 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package otp implements Erlang-inspired concurrency patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Recv

func Recv[T any](mb *Mailbox, match func(T) bool) T

Recv checks mb to see if any messages that have been sent to it are matched by the given function. A message is considered to be a match if it both can be type asserted to T and the match function returns true. If there is such a message, it is removes from the Mailbox and returned. If there is no such message, Recv blocks until such a message arrives.

If match is nil, all messages will be considered to be matching.

For a non-blocking variant that returns immediately whether or not a matching message is present, see TryRecv.

func TryRecv

func TryRecv[T any](mb *Mailbox, match func(T) bool) (msg T, ok bool)

TryRecv is like Recv but doesn't block, returning immediately whether not a matching message is present in the Mailbox. If no message matches, it returns false as the second return.

Types

type Mailbox

type Mailbox struct {
	// contains filtered or unexported fields
}

Mailbox is an OTP process mailbox. It works similarly to a channel but is not tied to a specific type and features a dynamic buffer. Sends to a mailbox are always asynchronous. A zero-value Mailbox is ready to use.

func (*Mailbox) Send

func (mb *Mailbox) Send(msg any)

Send delivers a message to the Mailbox. If there are any blocked receives, they will check the new message to see if it is what they're waiting for after this function returns.

type MonitoredProcessExited

type MonitoredProcessExited struct {
	Proc *Proc
}

MonitoredProcessExited is a message sent to processes that are monitoring another process when the monitored process exits. See [Process.Monitor].

type Proc

type Proc struct {
	// contains filtered or unexported fields
}

Proc is an OTP process.

func Go

func Go(f func(ctx context.Context) error) *Proc

Go runs f as an OTP process. The passed context will be canceled when the process is requested to exit, as well as if the process exits on its own. If f panics with an error, it will be recovered from and treated as though that error had been returned. Non-error panic values will be repanicked.

The current process can be retrieved from the provided context using the Self function.

func Self

func Self(ctx context.Context) *Proc

Self returns the current process from the context, or nil if there is none.

func (*Proc) Done

func (p *Proc) Done() <-chan struct{}

Done returns a channel that will be closed once the process has fully exited.

func (*Proc) Mailbox

func (p *Proc) Mailbox() *Mailbox

Mailbox returns the process's Mailbox.

func (*Proc) Monitor

func (p *Proc) Monitor(s Sender)

Monitor registers s as monitoring p. When p exits, all monitoring processes will be sent a MonitoredProcessExited message.

If p has already exited when Monitor is called, the message will be sent to s immediately.

func (*Proc) Send

func (p *Proc) Send(msg any)

Send sends a message to the process's mailbox.

func (*Proc) Stop

func (p *Proc) Stop()

Stop signals to the process that it should exit by canceling its root context.

func (*Proc) Unmonitor

func (p *Proc) Unmonitor(s Sender)

Unmonitor unregisters s from monitoring p. If s is not monitoring p, this function is a no-op.

func (*Proc) Wait

func (p *Proc) Wait() error

Wait will block until the process has exited and then return any error that it exited with.

type Sender

type Sender interface {
	Send(msg any)
}

Sender is an interface wrapping the Send method. Its primary implementation is Mailbox.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL