network

package
v0.23.8 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2022 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/rs/zerolog"

	splitterNetwork "github.com/onflow/flow-go/engine/common/splitter/network"
	"github.com/onflow/flow-go/model/flow"
	"github.com/onflow/flow-go/network"

	testnet "github.com/onflow/flow-go/utils/unittest/network"
)

func main() {
	// create a mock network
	net := testnet.NewNetwork()

	// create a splitter network
	logger := zerolog.Nop()
	splitterNet := splitterNetwork.NewNetwork(net, logger)

	// generate a random origin ID
	var id flow.Identifier
	rand.Seed(0)
	rand.Read(id[:])

	// create engines
	engineProcessFunc := func(engineID int) testnet.EngineProcessFunc {
		return func(channel network.Channel, originID flow.Identifier, event interface{}) error {
			fmt.Printf("Engine %d received message: channel=%v, originID=%v, event=%v\n", engineID, channel, originID, event)
			return nil
		}
	}
	engine1 := testnet.NewEngine().OnProcess(engineProcessFunc(1))
	engine2 := testnet.NewEngine().OnProcess(engineProcessFunc(2))
	engine3 := testnet.NewEngine().OnProcess(engineProcessFunc(3))

	// register engines with splitter network
	channel := network.Channel("foo-channel")
	_, err := splitterNet.Register(channel, engine1)
	if err != nil {
		fmt.Println(err)
	}
	_, err = splitterNet.Register(channel, engine2)
	if err != nil {
		fmt.Println(err)
	}
	_, err = splitterNet.Register(channel, engine3)
	if err != nil {
		fmt.Println(err)
	}

	// send message to network
	err = net.Send(channel, id, "foo")
	if err != nil {
		fmt.Println(err)
	}

}
Output:

Engine 1 received message: channel=foo-channel, originID=0194fdc2fa2ffcc041d3ff12045b73c86e4ff95ff662a5eee82abdf44a2d0b75, event=foo
Engine 2 received message: channel=foo-channel, originID=0194fdc2fa2ffcc041d3ff12045b73c86e4ff95ff662a5eee82abdf44a2d0b75, event=foo
Engine 3 received message: channel=foo-channel, originID=0194fdc2fa2ffcc041d3ff12045b73c86e4ff95ff662a5eee82abdf44a2d0b75, event=foo

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Network

type Network struct {
	network.Network
	// contains filtered or unexported fields
}

Network is the splitter network. It is a wrapper around the default network implementation and should be passed in to engine constructors that require a network to register with. When an engine is registered with the splitter network, a splitter engine is created for the given channel (if one doesn't already exist) and the engine is registered with that splitter engine. As a result, multiple engines can register with the splitter network on the same channel and will each receive all events on that channel.

func NewNetwork

func NewNetwork(
	net network.Network,
	log zerolog.Logger,
) *Network

NewNetwork returns a new splitter network.

func (*Network) Done

func (n *Network) Done() <-chan struct{}

Done returns a done channel that is closed once the network has fully stopped. For the splitter network, this is true once the wrapped network has stopped.

func (*Network) Ready

func (n *Network) Ready() <-chan struct{}

Ready returns a ready channel that is closed once the network has fully started. For the splitter network, this is true once the wrapped network has started.

func (*Network) Register

func (n *Network) Register(channel network.Channel, engine network.Engine) (network.Conduit, error)

Register will subscribe the given engine with the spitter on the given channel, and all registered engines will be notified with incoming messages on the channel. The returned Conduit can be used to send messages to engines on other nodes subscribed to the same channel

Jump to

Keyboard shortcuts

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