lobby/server/discovery.go

234 lines
5.5 KiB
Go
Raw Normal View History

2021-08-31 14:26:09 +00:00
package server
import (
"encoding/json"
"fmt"
"strings"
2021-08-31 14:26:09 +00:00
"time"
)
// ----------------
// Discovery struct
// ----------------
const TimeToLife = 60 // when server won't occur in the discovery channel longer than this, it should be considered as not-alive
// Discovery contains information about a single server and is used for server discovery
type Discovery struct {
Hostname string `json:"hostname"`
Labels Labels `json:"labels"`
2021-08-31 14:26:09 +00:00
// 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
2021-08-31 14:26:09 +00:00
}
// Validate checks all values in the struct if the content is valid
func (d *Discovery) Validate() error {
// TODO: implement
return nil
}
// IsAlive return true if the server should be considered as alive
func (d *Discovery) IsAlive() bool {
if d.TTL == 0 {
d.TTL = TimeToLife
}
return time.Now().Unix()-d.LastCheck < int64(d.TTL)
2021-08-31 14:26:09 +00:00
}
func (d *Discovery) Bytes() ([]byte, error) {
data, err := json.Marshal(d)
return data, err
}
// FindLabels 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 (d *Discovery) FindLabels(prefix string) Labels {
labels := Labels{}
for _, label := range d.Labels {
if strings.HasPrefix(label.String(), prefix) {
labels = append(labels, label)
}
}
return labels
}
2021-08-31 14:26:09 +00:00
// -----------------
// Discovery storage
// -----------------
// Discoveries helps to store instances of Discovery struct and access them in thread safe mode
type Discoveries struct {
activeServers []Discovery
LogChannel chan string
TTL uint
}
func (d *Discoveries) hostnameIndex(hostname string) int {
for idx, discovery := range d.activeServers {
if discovery.Hostname == hostname {
return idx
}
}
return -1
2021-08-31 14:26:09 +00:00
}
// Add appends a new discovery/server to the storage
func (d *Discoveries) Add(discovery Discovery) {
if d.Exist(discovery.Hostname) {
d.Refresh(discovery.Hostname)
idx := d.hostnameIndex(discovery.Hostname)
if idx >= 0 {
d.activeServers[idx].Labels = discovery.Labels
}
2021-08-31 14:26:09 +00:00
return
}
discovery.LastCheck = time.Now().Unix()
d.activeServers = append(d.activeServers, discovery)
if d.LogChannel != nil {
d.LogChannel <- fmt.Sprintf("%s registered", discovery.Hostname)
}
}
// Refresh updates
func (d *Discoveries) Refresh(hostname string) {
idx := d.hostnameIndex(hostname)
if idx >= 0 {
d.activeServers[idx].LastCheck = time.Now().Unix()
2021-08-31 14:26:09 +00:00
}
}
// Delete removes server identified by hostname from the storage
func (d *Discoveries) Delete(hostname string) {
2021-09-02 00:13:12 +00:00
if !d.Exist(hostname) {
return
}
2021-08-31 14:26:09 +00:00
if d.LogChannel != nil {
d.LogChannel <- fmt.Sprintf("removing %s", hostname)
}
newSet := []Discovery{}
for _, server := range d.activeServers {
if server.Hostname != hostname {
newSet = append(newSet, server)
}
}
d.activeServers = newSet
}
// Exist returns true if server with given hostname exists
func (d *Discoveries) Exist(hostname string) bool {
for _, server := range d.activeServers {
if server.Hostname == hostname {
return true
}
}
return false
}
// 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 (d *Discoveries) Get(hostname string) Discovery {
for _, server := range d.activeServers {
if server.Hostname == hostname {
return server
}
}
return Discovery{}
}
// GetAll returns copy of the internal storage
func (d *Discoveries) GetAll() []Discovery {
return d.activeServers
}
// Filter returns list of discoveries based on given labels
func (d *Discoveries) Filter(labelsFilter []string) []Discovery {
newSet := []Discovery{}
var found bool
if len(labelsFilter) > 0 {
for _, discovery := range d.activeServers {
newDiscovery := discovery
newDiscovery.Labels = Labels{}
found = false
for _, label := range discovery.Labels {
for _, labelFilter := range labelsFilter {
if label.String() == labelFilter {
found = true
newDiscovery.Labels = append(newDiscovery.Labels, label)
break
}
}
}
if found {
newSet = append(newSet, newDiscovery)
}
}
}
return newSet
}
// Filter returns list of discoveries based on given label prefixes.
func (d *Discoveries) FilterPrefix(prefixes []string) []Discovery {
newSet := []Discovery{}
var found bool
if len(prefixes) > 0 {
for _, discovery := range d.activeServers {
newDiscovery := discovery
newDiscovery.Labels = Labels{}
found = false
if found {
newSet = append(newSet, newDiscovery)
}
for _, label := range discovery.Labels {
for _, prefix := range prefixes {
if strings.HasPrefix(label.String(), prefix) {
found = true
newDiscovery.Labels = append(newDiscovery.Labels, label)
break
}
}
}
if found {
newSet = append(newSet, newDiscovery)
}
}
}
return newSet
}
2021-08-31 14:26:09 +00:00
// Clean checks loops over last check values for each discovery object and removes it if it's passed
func (d *Discoveries) Clean() {
newSet := []Discovery{}
for _, server := range d.activeServers {
server.TTL = d.TTL
2021-08-31 14:26:09 +00:00
if server.IsAlive() {
newSet = append(newSet, server)
} else {
if d.LogChannel != nil {
d.LogChannel <- fmt.Sprintf("%s not alive anymore", server.Hostname)
}
}
}
d.activeServers = newSet
}