source

package
v0.6.992 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2019 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package listener provides a functionalities to discover and inspect sources.

Index

Constants

This section is empty.

Variables

View Source
var PollInterval = time.Second * 3
View Source
var PollTimeout = time.Second * 5

Functions

func Diff added in v0.6.8

func Diff(old, cur []core.Source) (add []core.Source, remove []core.Source)

Diff returns respectively the list of items that has to be added and removed from "old" to create the same list as "cur".

Types

type Confidence added in v0.6.8

type Confidence int
const (
	Low Confidence = iota
	High
)

type Config added in v0.6.91

type Config struct {
	Store           Store
	Provider        Provider
	MetricsExporter MetricsExporter
}

type Conn

type Conn struct {
	net.Conn

	OnClose func() // Callback for close event.
	OnRead  func(df *DataFlow)
	OnWrite func(df *DataFlow)
	// contains filtered or unexported fields
}

Conn is a wrapper around net.Conn, with the addition of some functions useful to uniquely identify the connection and receive callbacks on close events.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the underlying net.Conn, calling the OnClose callback afterwards.

func (*Conn) Read added in v0.6.91

func (c *Conn) Read(p []byte) (int, error)

Read is the io.Reader implementation of Conn. It forwards the request to the underlying net.Conn, but it also records the number of bytes tranferred and the duration of the transmission. It then exposes the data using the OnRead callback.

func (*Conn) Write added in v0.6.91

func (c *Conn) Write(p []byte) (int, error)

Write is the io.Writer implementation of Conn. It forwards the request to the underlying net.Conn, but it also records the number of bytes tranferred and the duration of the transmission. It then exposes the data using the OnWrite callback.

type DataFlow added in v0.6.91

type DataFlow struct {
	Type      string
	StartedAt time.Time // Start of the first data transmitted.
	EndedAt   time.Time // Time of the last byte read/written. May be overridden multiple times.
	N         int       // Number of bytes transmitted.
	Avg       float64   // Avg bytes/seconds.
}

DataFlow collects data about a data tranmission.

func (*DataFlow) Start added in v0.6.91

func (f *DataFlow) Start()

Begin sets the data flow start value.

func (*DataFlow) Stop added in v0.6.91

func (f *DataFlow) Stop(n int)

End computes the Avg, N, and End valus considering start as beginning of this data transmission.

type DialHook added in v0.6.8

type DialHook func(ref, network, address string, err error)

DialHook describes the function used to notify about dial errors.

type Hooker added in v0.6.8

type Hooker struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*Hooker) Add added in v0.6.8

func (h *Hooker) Add(err *hookErr)

func (*Hooker) HandleDialErr added in v0.6.8

func (h *Hooker) HandleDialErr(ref, network, address string, err error)

func (*Hooker) HookErr added in v0.6.8

func (h *Hooker) HookErr(id string) error

type Interface

type Interface struct {

	// If OnDialErr is not nil, it is called each time that the
	// dialer is not able to create a network connection.
	OnDialErr DialHook
	// contains filtered or unexported fields
}

Interface is a wrapper around net.Interface and implements the core.Source interface, i.e. is it capable of providing network connections through the device it is referring to.

func (*Interface) Close added in v0.6.7

func (i *Interface) Close() error

Close closes all open connections.

func (*Interface) DialContext

func (i *Interface) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext dials a connection of type `network` to `address`. If an error is encoutered, it is both returned and logged using the OnDialErr function, if available. `Follow` is called is called on the net.Conn before returning it. This function dials the connection using the interface's actual device as mean.

func (*Interface) Follow added in v0.6.7

func (i *Interface) Follow(conn net.Conn) net.Conn

Follow wraps the net.Conn around a Conn type, and keeps track of its callbacks, sending the metrics collected with the OnRead and OnWrite hooks. The connection is added to a set of followed connections, allowing the interface to perform operations on the entire list of open connections. The connection is removed from such list when the conn's OnClose function is called.

func (*Interface) ID

func (i *Interface) ID() string

ID implements the core.Source interface.

func (*Interface) Len added in v0.6.7

func (i *Interface) Len() int

Len returns the number of open connections.

func (*Interface) SendMetrics added in v0.6.91

func (i *Interface) SendMetrics(labels map[string]string, data *DataFlow)

SendMetrics sends the data using the Interface's MetricsExporter. It is safe to use by multiple goroutines.

func (*Interface) SetMetricsExporter added in v0.6.94

func (i *Interface) SetMetricsExporter(exp MetricsExporter)

SetMetricsExporter sets exp as the default MetricsExporter of interface `i`. It is safe to use by multiple goroutines.

func (*Interface) String

func (i *Interface) String() string

type Listener added in v0.6.8

type Listener struct {
	// Source provider.
	Provider
	// contains filtered or unexported fields
}

func NewListener added in v0.6.8

func NewListener(c Config) *Listener

NewListener creates a new Listener with the provided storage, using as Provider the MergedProvider implementation.

func (*Listener) Poll added in v0.6.8

func (l *Listener) Poll(ctx context.Context) error

Poll queries the provider for a list of sources. It then inspect each new source, saving into the storage the sources that provide an active internet connection and removing the ones that are no longer available.

func (*Listener) Run added in v0.6.8

func (l *Listener) Run(ctx context.Context) error

Run is a blocking function which keeps on calling Poll and waiting PollInterval amount of time. This function will stop with an error only in case of a context cancelation and in case that the Poll function returns with a critical error.

func (*Listener) StoredSources added in v0.6.97

func (l *Listener) StoredSources() []core.Source

StoredSources returns the list of sources that are already inside the store.

type Local added in v0.6.8

type Local struct {
}

func (*Local) Check added in v0.6.8

func (l *Local) Check(ctx context.Context, ifi *Interface, level Confidence) error

func (*Local) Provide added in v0.6.8

func (l *Local) Provide(ctx context.Context, level Confidence) ([]*Interface, error)

type MergedProvider added in v0.6.8

type MergedProvider struct {
	// ControlInterface allows to make some final configurations
	// on an interface that has been found by the provider, before
	// it is hidden inside a core.Source.
	ControlInterface func(ifi *Interface)
	// contains filtered or unexported fields
}

Provider is a provider implementation which acts as a wrapper around many provider implementations.

func (*MergedProvider) Check added in v0.6.8

func (p *MergedProvider) Check(ctx context.Context, src core.Source, level Confidence) error

func (*MergedProvider) Provide added in v0.6.8

func (p *MergedProvider) Provide(ctx context.Context) ([]core.Source, error)

Provide returns the list of sources returned by each provider owned by merged. Currently only a local provider is queried.

type MetricsExporter added in v0.6.94

type MetricsExporter interface {
	SendDataFlow(labels map[string]string, data *DataFlow)
}

MetricsExporter is the entity used to send data tranmission information to an entity that is supposed to persist or handle the data accordingly.

type Provider added in v0.6.8

type Provider interface {
	Provide(context.Context) ([]core.Source, error)
	Check(context.Context, core.Source, Confidence) error
}

Provider describes a service that is capable of providing sources and checking their effective internet connection using a defined level of confidence.

type Store added in v0.6.8

type Store interface {
	Put(...core.Source)
	Del(...core.Source)
	Len() int
	Do(func(core.Source))
}

Store describes an entity that is able to store, delete and list sources.

Jump to

Keyboard shortcuts

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