mx

package
v1.0.0-alpha.18 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2021 License: Apache-2.0, MIT Imports: 10 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	// Timestep returns the increment with which the clock is
	// advanced at each time-step.  This is effectively the
	// temporal precision with which events are simulated.
	Timestep() time.Duration

	// After is analogous to time.After in the Go standard library.
	// The supplied function will be called exactly once after the
	// specified duration has elapsed, unless 'cancel' is called.
	After(time.Duration, func()) (cancel func())

	// Ticker is analogous to time.NewTicker in the Go standard
	// library.  The supplied function will be called periodically,
	// until 'cancel' is called.
	Ticker(time.Duration, func()) (cancel func())
}

type ClockController

type ClockController interface {
	Clock
	Advance(time.Time)
}

type FilterFunc

type FilterFunc func(int, host.Host) bool

type HostFactory

type HostFactory interface {
	NewHost(ctx context.Context, opt []config.Option) (host.Host, error)
}

type MapFunc

type MapFunc func(ctx context.Context, i int, h host.Host) error

func Announce

func Announce(sim Simulation, t netsim.Topology, ns string, opt ...discovery.Option) MapFunc

Announce each host in the current selection using the supplied topology.

func Discover

func Discover(sim Simulation, t netsim.Topology, ns string, opt ...discovery.Option) MapFunc

Discover peers for each host in the current selection using the supplied topology.

func (MapFunc) Then

func (f MapFunc) Then(fn MapFunc) MapFunc

type Op

type Op func(Op) (SelectFunc, Op)

func Filter

func Filter(f FilterFunc) Op

Filter returns a new selection that contains the elements of the current selection for which f(element) == true.

func Go

func Go(f MapFunc) Op

Go applies f to each item in the current selection concurrently.

func Map

func Map(f MapFunc) Op

Map applies f to each item in the current selection.

func Nop

func Nop() Op

Nop returns the selection unchanged.

func Select

func Select(f SelectFunc) Op

func Topology

func Topology(sim Simulation, t netsim.Topology, ns string, opt ...discovery.Option) Op

func (Op) Bind

func (op Op) Bind(fn func(SelectFunc) Op) Op

func (Op) Err

func (op Op) Err(ctx context.Context, hs Selection) (err error)

func (Op) ErrArgs

func (op Op) ErrArgs(ctx context.Context, hs ...host.Host) error

func (Op) Eval

func (op Op) Eval(ctx context.Context, hs Selection) (out Selection, err error)

func (Op) EvalArgs

func (op Op) EvalArgs(ctx context.Context, hs ...host.Host) (Selection, error)

func (Op) Go

func (op Op) Go(f MapFunc) Op

func (Op) Map

func (op Op) Map(f MapFunc) Op

func (Op) Must

func (op Op) Must(ctx context.Context, hs Selection) Selection

func (Op) MustArgs

func (op Op) MustArgs(ctx context.Context, hs ...host.Host) Selection

func (Op) Then

func (op Op) Then(f SelectFunc) Op

type Option

type Option func(sim *Simulation)

func WithClock

func WithClock(c ClockController) Option

WithClock sets the simulation clock. If c == nil, a default clock with 10ms accuracy is used.

func WithHostFactory

func WithHostFactory(f HostFactory) Option

WithHostFactory sets the host factory for the simulation. If f == nil, a default factory is used that constructs hosts using an in-process transport.

func WithNamespaceFactory

func WithNamespaceFactory(f func(Clock) netsim.NamespaceProvider) Option

WithNamespaceFactory sets the namespace implementation for the simulation. If ns == nil, a default namespace implementation is used.

type Partition

type Partition int

NewPartition returns a partition of n subsets based on the index number of the current selection.

func (Partition) Get

func (p Partition) Get(idx int) SelectFunc

Get the partition by its index.

func (Partition) Num

func (p Partition) Num() int

type SelectFunc

type SelectFunc func(ctx context.Context, hs Selection) (Selection, error)

func Fail

func Fail(err error) SelectFunc

Fail aborts the Op pipeline and returns the supplied error.

func Just

func Just(hs Selection) SelectFunc

Just discards the current selection and replaces it with hs.

func (SelectFunc) Bind

func (f SelectFunc) Bind(fn SelectFunc) Op

type Selection

type Selection []host.Host

Selection is a set of hosts.

func (Selection) Filter

func (hs Selection) Filter(f FilterFunc) Selection

func (Selection) Go

func (hs Selection) Go(f func(i int, h host.Host) error) error

func (Selection) Len

func (hs Selection) Len() int

func (Selection) Less

func (hs Selection) Less(i, j int) bool

func (Selection) Map

func (hs Selection) Map(f func(i int, h host.Host) error) (err error)

func (Selection) Swap

func (hs Selection) Swap(i, j int)

type Simulation

type Simulation struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"strings"

	"github.com/libp2p/go-libp2p-core/network"
	mx "github.com/wetware/matrix/pkg"
	"github.com/wetware/matrix/pkg/netsim"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	const (
		ns   = "matrix.test"
		echo = "/echo"
	)

	var (
		sim = mx.New(ctx)

		h0 = sim.MustHost(ctx)
		h1 = sim.MustHost(ctx)
	)

	h0.SetStreamHandler(echo, func(s network.Stream) {
		defer s.Close()

		if _, err := io.Copy(s, s); err != nil {
			panic(err)
		}
	})

	mx.Topology(sim, netsim.SelectRing{}, ns).
		MustArgs(ctx, h0, h1)

	s, err := h1.NewStream(ctx, h0.ID(), echo)
	if err != nil {
		panic(err)
	}
	defer s.Close()

	_, err = io.Copy(s, strings.NewReader("Hello, world!"))
	if err != nil {
		panic(err)
	}
	s.CloseWrite()

	var buf bytes.Buffer
	_, err = io.Copy(&buf, s)
	if err != nil {
		panic(err)
	}

	fmt.Println(buf.String())
}
Output:

Hello, world!

func New

func New(ctx context.Context, opt ...Option) Simulation

func (Simulation) Clock

func (s Simulation) Clock() Clock

func (Simulation) MustHost

func (s Simulation) MustHost(ctx context.Context, opt ...config.Option) host.Host

MustHost returns a host or panics if an error was encountered.

func (Simulation) MustHostSet

func (s Simulation) MustHostSet(ctx context.Context, n int, opt ...config.Option) Selection

MustHostSet calls NewHostSet with the supplied parameters and panics if an error is encountered.

func (Simulation) NewDiscovery

func (s Simulation) NewDiscovery(h host.Host, t netsim.Topology) *netsim.DiscoveryService

NewDiscovery returns a discovery.Discovery implementation that supports the Simulation's in-process network.

The topology parameter t can be used to specify an initial connection topology. All peers must use the same instance of t in order to obtain the desired topology.

If t == nil, the topology defaults to netsim.SelectAll.

func (Simulation) NewHost

func (s Simulation) NewHost(ctx context.Context, opt ...config.Option) (host.Host, error)

NewHost assembles and creates a new libp2p host that uses the simulation's network.

The simulation configures hosts to use an in-process network, overriding the following options:

- libp2p.Transport - libp2p.NoTransports - libp2p.ListenAddr - libp2p.ListenAddrStrings - libp2p.NoListenAddrs

Users SHOULD NOT pass any of the above options to NewHost.

func (Simulation) NewHostSet

func (s Simulation) NewHostSet(ctx context.Context, n int, opt ...config.Option) (Selection, error)

NewHostSet builds and configures n hosts with identical parameters.

See NewHost.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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