server

package
v0.0.0-...-c2ed658 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const TimeToLife = 60 // when server won't occur in the discovery channel longer than this, it should be considered as not-alive

Variables

This section is empty.

Functions

func AddRuntimeLabel

func AddRuntimeLabel(label Label) error

func Compare

func Compare(discoveryA, discoveryB Discovery) bool

Compare compares discovery A and B and returns true if those two are different.

func RemoveRuntimeLabel

func RemoveRuntimeLabel(label Label) error

Types

type Discoveries

type Discoveries struct {
	LogChannel chan string
	TTL        uint
	// contains filtered or unexported fields
}

Discoveries helps to store instances of Discovery struct and access them in thread safe mode

func (*Discoveries) Add

func (d *Discoveries) Add(discovery Discovery)

Add appends a new discovery/server to the storage

func (*Discoveries) Clean

func (d *Discoveries) Clean()

Clean checks loops over last check values for each discovery object and removes it if it's passed

func (*Discoveries) Delete

func (d *Discoveries) Delete(hostname string)

Delete removes server identified by hostname from the storage

func (*Discoveries) Exist

func (d *Discoveries) Exist(hostname string) bool

Exist returns true if server with given hostname exists

func (*Discoveries) Filter

func (d *Discoveries) Filter(labelsFilter []string) []Discovery

Filter returns list of discoveries based on given labels

func (*Discoveries) FilterPrefix

func (d *Discoveries) FilterPrefix(prefixes []string) []Discovery

Filter returns list of discoveries based on given label prefixes.

func (*Discoveries) Get

func (d *Discoveries) Get(hostname string) Discovery

Get returns Discovery struct with the given hostname but it can be also an empty struct if it's not found. Check if hostname is empty or use Exist first to be sure.

func (*Discoveries) GetAll

func (d *Discoveries) GetAll() []Discovery

GetAll returns copy of the internal storage

func (*Discoveries) Refresh

func (d *Discoveries) Refresh(hostname string)

Refresh updates

type Discovery

type Discovery struct {
	Hostname string `json:"hostname"`
	Labels   Labels `json:"labels"`

	// For internal use to check if the server is still alive.
	// Contains timestamp of the last check.
	LastCheck int64 `json:"last_check"`

	TTL uint `json:"-"` // after how many second consider the server to be off, if 0 then 60 secs is used
}

Discovery contains information about a single server and is used for server discovery

func (*Discovery) Bytes

func (d *Discovery) Bytes() ([]byte, error)

func (*Discovery) FindLabelsByPrefix

func (d *Discovery) FindLabelsByPrefix(prefix string) Labels

FindLabelsByPrefix returns list of labels with given prefix. For example "service:ns" has prefix "service" or "service:". It doesn't have to be prefix, but for example "service:test" will match "service:test" and also "service:test2".

func (*Discovery) IsAlive

func (d *Discovery) IsAlive() bool

IsAlive return true if the server should be considered as alive

func (*Discovery) SortLabels

func (d *Discovery) SortLabels()

func (*Discovery) Validate

func (d *Discovery) Validate() error

Validate checks all values in the struct if the content is valid

type Label

type Label string

Label keeps one piece of information about a single server

func (Label) GetPart

func (l Label) GetPart(idx int) string

GetPart returns specific part of the label, if part index is higher than last available index it returns empty string.

func (Label) GetPart1

func (l Label) GetPart1(idx int) string

GetPart1 is exactly same as GetPart but it splits the label only once, this is good for IPv6 addresses

func (Label) String

func (l Label) String() string

type Labels

type Labels []Label

Labels stores multiple Label records

func (*Labels) StringSlice

func (l *Labels) StringSlice() []string

StringSlice return slice of Label as strings

type LocalHost

type LocalHost struct {
	LabelsPath            string // Where labels are stored
	RuntimeLabelsFilename string // Filename under which are runtime labels saved in LabelsPath
	InitialLabels         Labels // this usually coming from the config
	HostnameOverride      string // if not empty string hostname in the discovery packet will be replaced by this
}

func (*LocalHost) AddLabels

func (l *LocalHost) AddLabels(labels Labels) error

AddLabel adds runtime label into the LabelsPath directory

func (*LocalHost) DeleteLabels

func (l *LocalHost) DeleteLabels(labels Labels) error

DeleteLabels removed labels from LabelsPath directory. Only labels added this way can be deleted.

func (*LocalHost) GetIdentification

func (l *LocalHost) GetIdentification() (Discovery, error)

GetIdentification assembles the discovery packet that contains hotname and set of labels describing a single server, in this case the local server. Parameter initialLabels usually coming from configuration of the app. If hostname is empty it will be discovered automatically.

Jump to

Keyboard shortcuts

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