store

package
v0.6.991 Latest Latest
Warning

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

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

Documentation

Overview

Package store is the main component that orchestrates sources. It provides a `SourceStore` that has to be configured with `New`, providing the internal store that the `SourceStore` will use to save the sources that it receives. The behaviour that `SourceStore` uses to retrieve sources from its protected storage can be manipulated adding and removing policies to and from it.

Index

Constants

View Source
const (
	PolicyCodeBlock int = iota + 1
	PolicyCodeReserve
	PolicyCodeStick
	PolicyCodeAvoid
)

Policy codes, different for each `Policy` created.

Variables

This section is empty.

Functions

func LookupAddress added in v0.6.99

func LookupAddress(address string) []string

LookupAddress finds the addresses associated with `address`. If it is not able to lookup, it just returns `address` wrapped into a list.

func TrimPort added in v0.6.99

func TrimPort(address string) string

TrimPort removes port information from `address`.

Types

type AvoidPolicy added in v0.6.98

type AvoidPolicy struct {
	SourceID string `json:"avoid_source_id"`
	Address  string `json:"address"`
	// contains filtered or unexported fields
}

AvoidPolicy is a Policy implementation. It is used to avoid giving connection to `Address` to `SourceID`.

func NewAvoidPolicy added in v0.6.98

func NewAvoidPolicy(issuer, sourceID, address string) *AvoidPolicy

func (*AvoidPolicy) Accept added in v0.6.98

func (p *AvoidPolicy) Accept(id, address string) bool

Accept implements Policy.

func (AvoidPolicy) ID added in v0.6.98

func (p AvoidPolicy) ID() string

type BlockPolicy added in v0.6.98

type BlockPolicy struct {

	// Source that should be always refuted.
	SourceID string `json:"-"`
	// contains filtered or unexported fields
}

BlockPolicy blocks `SourceID`.

func NewBlockPolicy added in v0.6.98

func NewBlockPolicy(issuer, sourceID string) *BlockPolicy

func (*BlockPolicy) Accept added in v0.6.98

func (p *BlockPolicy) Accept(id, address string) bool

Accept implements Policy.

func (BlockPolicy) ID added in v0.6.98

func (p BlockPolicy) ID() string

type DummySource

type DummySource struct {
	ID string `json:"name"`
}

DummySource is a representation of a source, suitable when other components need information about the sources stored, but should not be able to mess with it's actual content.

type GenPolicy added in v0.6.98

type GenPolicy struct {
	Name string `json:"name"`

	// AcceptFunc is used as implementation
	// of Accept.
	AcceptFunc func(id, address string) bool `json:"-"`
	// contains filtered or unexported fields
}

GenPolicy is a general purpose policy that allows to configure the behaviour of the Accept function setting its AcceptFunc field.

Used mainly in tests.

func (*GenPolicy) Accept added in v0.6.98

func (p *GenPolicy) Accept(id, address string) bool

Accept implements Policy.

func (*GenPolicy) ID added in v0.6.98

func (p *GenPolicy) ID() string

type HistoryQueryFunc added in v0.6.98

type HistoryQueryFunc func(string) (string, bool)

HistoryQueryFunc describes the function that is used to query the bind history of an entity. It is called passing the connection address in question, and it returns the source identifier that is associated to it and true, otherwise false if none is found.

type HostResolver added in v0.6.99

type HostResolver interface {
	// Returns all ip addresses associated with `host`
	LookupHost(ctx context.Context, host string) (addrs []string, err error)
	// Returns at least an host associated with `addr`
	LookupAddr(ctx context.Context, addr string) (hosts []string, err error)
}
var Resolver HostResolver = &net.Resolver{}

type Policy

type Policy interface {
	ID() string
	Accept(id, address string) bool
}

A Policy defines wether a connection to `address` should be accepted by source `id`.

type ReservedPolicy added in v0.6.98

type ReservedPolicy struct {
	SourceID string `json:"reserved_source_id"`
	// contains filtered or unexported fields
}

ReservedPolicy is a Policy implementation. It is used to reserve a source to be used only for connections to a defined list of addresses, and those connections will not be assigned to any other source.

func NewReservedPolicy added in v0.6.98

func NewReservedPolicy(issuer, sourceID string, hosts ...string) *ReservedPolicy

func (*ReservedPolicy) Accept added in v0.6.98

func (p *ReservedPolicy) Accept(id, address string) bool

Accept implements Policy.

func (ReservedPolicy) ID added in v0.6.98

func (p ReservedPolicy) ID() string

type SourceStore

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

A SourceStore is able to keep sources under a set of policies, or rules. When it is asked to store a value, it performs the policy checks on it, and eventually the request is forwarded to the protected store.

func New

func New(store Store) *SourceStore

New creates a New instance of SourceStore, using interally `store` as the protected storage.

func (*SourceStore) AppendPolicy added in v0.6.97

func (ss *SourceStore) AppendPolicy(p Policy) error

AppendPolicy appends `p` to the end of the list of policies.

func (*SourceStore) Del

func (ss *SourceStore) Del(sources ...core.Source)

Del removes `sources` from the protected storage.

func (*SourceStore) DelPolicy

func (ss *SourceStore) DelPolicy(id string) error

DelPolicy removes the policy with identifier `id` from the storage.

func (*SourceStore) Do added in v0.6.97

func (ss *SourceStore) Do(f func(core.Source))

Do executes `f` on each source of the protected storage.

func (*SourceStore) Get added in v0.6.97

func (ss *SourceStore) Get(ctx context.Context, address string, blacklisted ...core.Source) (core.Source, error)

Get is an implementation of booster.Balancer. It provides a source, avoiding the ones `blacklisted`. The `blacklisted` list is populated with the sources that cannot be accepted due to policy restrictions. The source is then retriven from the protected storage. If `bindHistory.record == true`, the source identifier returned for this address is saved into `bindHistory.val`.

func (*SourceStore) GetPoliciesSnapshot

func (ss *SourceStore) GetPoliciesSnapshot() []Policy

GetPoliciesSnapshot returns a copy of the current policies active in the store.

func (*SourceStore) GetSourcesSnapshot

func (ss *SourceStore) GetSourcesSnapshot() []*DummySource

GetSourcesSnapshot returns nothing more then a copy of the list of sources that the storage is holding.

func (*SourceStore) Len added in v0.6.97

func (ss *SourceStore) Len() int

Len returns the number of sources available to the store.

func (*SourceStore) MakeBlacklist added in v0.6.97

func (ss *SourceStore) MakeBlacklist(address string) []core.Source

MakeBlacklist computes the list of blacklisted sources for `address`, i.e. the sources that should not be used to perform a request to `address`, because there is one or more policies that do not accept them.

func (*SourceStore) Put

func (ss *SourceStore) Put(sources ...core.Source)

Put adds `sources` to the protected storage.

func (*SourceStore) QueryBindHistory added in v0.6.98

func (ss *SourceStore) QueryBindHistory(address string) (src string, ok bool)

QueryBindHistory queries the bindHistory for address.

func (*SourceStore) RecordBindHistory added in v0.6.98

func (ss *SourceStore) RecordBindHistory()

RecordBindHistory makes the store keep track of which source is assigned to which address.

func (*SourceStore) SaveBindHistory added in v0.6.99

func (ss *SourceStore) SaveBindHistory(ctx context.Context, id, address string)

SaveBindHistory saves the association of an address with a source. It performs the operation only if it is required, as this is a time consuming operation (potentially, due to DNS lookup).

func (*SourceStore) ShouldAccept added in v0.6.97

func (ss *SourceStore) ShouldAccept(id, address string) (bool, Policy)

ShouldAccept takes `id` and `address`, iterates through the list of policies and returns false if the two inputs are not accepted by one of them. The offending policy is also returned. Returns true if no policy blocks `id` and `address`.

func (*SourceStore) StopRecordingBindHistory added in v0.6.98

func (ss *SourceStore) StopRecordingBindHistory()

StopRecordingBindHistory makes the store stop tracking which source is assigned to which address. The old history, if any, is discarded.

type StickyPolicy added in v0.6.98

type StickyPolicy struct {
	BindHistory HistoryQueryFunc `json:"-"`
	// contains filtered or unexported fields
}

StickyPolicy is a Policy implementation. It is used to make connections to some address be always bound with the same source.

func NewStickyPolicy added in v0.6.98

func NewStickyPolicy(issuer string, f HistoryQueryFunc) *StickyPolicy

func (*StickyPolicy) Accept added in v0.6.98

func (p *StickyPolicy) Accept(id, address string) bool

Accept implements Policy.

func (StickyPolicy) ID added in v0.6.98

func (p StickyPolicy) ID() string

type Store

type Store interface {
	Put(...core.Source)
	Del(...core.Source)
	Get(context.Context, ...core.Source) (core.Source, error)

	Len() int
	Do(func(core.Source))
}

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

Jump to

Keyboard shortcuts

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