2021-08-31 14:26:09 +00:00
package server
import (
"encoding/json"
"fmt"
2021-09-06 23:18:17 +00:00
"sort"
2021-09-01 21:18:52 +00:00
"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 {
2021-09-04 12:16:14 +00:00
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" `
2021-09-01 21:18:52 +00:00
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 {
2021-09-01 21:18:52 +00:00
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
}
2021-09-04 12:16:14 +00:00
// 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 { }
2021-09-01 21:18:52 +00:00
for _ , label := range d . Labels {
2021-09-04 12:16:14 +00:00
if strings . HasPrefix ( label . String ( ) , prefix ) {
2021-09-01 21:18:52 +00:00
labels = append ( labels , label )
}
}
return labels
}
2021-09-06 23:18:17 +00:00
func ( d * Discovery ) SortLabels ( ) {
labelStrings := d . Labels . StringSlice ( )
sort . Strings ( labelStrings )
labels := Labels { }
for _ , label := range labelStrings {
labels = append ( labels , Label ( label ) )
}
d . Labels = 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
2021-09-01 21:18:52 +00:00
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 )
2021-09-01 21:18:52 +00:00
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 ) {
2021-09-01 21:18:52 +00:00
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
}
2021-09-05 15:30:21 +00:00
// Filter returns list of discoveries based on given labels
2021-09-02 21:05:57 +00:00
func ( d * Discoveries ) Filter ( labelsFilter [ ] string ) [ ] Discovery {
2021-09-01 21:18:52 +00:00
newSet := [ ] Discovery { }
2021-09-02 21:05:57 +00:00
var found bool
if len ( labelsFilter ) > 0 {
2021-09-01 21:18:52 +00:00
for _ , discovery := range d . activeServers {
2021-09-05 20:00:08 +00:00
newDiscovery := discovery
newDiscovery . Labels = Labels { }
2021-09-02 21:05:57 +00:00
found = false
2021-09-01 21:18:52 +00:00
for _ , label := range discovery . Labels {
2021-09-02 21:05:57 +00:00
for _ , labelFilter := range labelsFilter {
2021-09-04 12:16:14 +00:00
if label . String ( ) == labelFilter {
2021-09-02 21:05:57 +00:00
found = true
2021-09-05 20:00:08 +00:00
newDiscovery . Labels = append ( newDiscovery . Labels , label )
2021-09-02 21:05:57 +00:00
break
}
}
2021-09-01 21:18:52 +00:00
}
2021-09-05 20:00:08 +00:00
if found {
newSet = append ( newSet , newDiscovery )
}
2021-09-01 21:18:52 +00:00
}
}
return newSet
}
2021-09-05 15:30:21 +00:00
// 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 {
2021-09-05 20:00:08 +00:00
newDiscovery := discovery
newDiscovery . Labels = Labels { }
2021-09-05 15:30:21 +00:00
found = false
2021-09-05 20:00:08 +00:00
if found {
newSet = append ( newSet , newDiscovery )
}
2021-09-05 15:30:21 +00:00
for _ , label := range discovery . Labels {
for _ , prefix := range prefixes {
if strings . HasPrefix ( label . String ( ) , prefix ) {
found = true
2021-09-05 20:00:08 +00:00
newDiscovery . Labels = append ( newDiscovery . Labels , label )
2021-09-05 15:30:21 +00:00
break
}
}
2021-09-05 20:00:08 +00:00
}
if found {
newSet = append ( newSet , newDiscovery )
2021-09-05 15:30:21 +00:00
}
}
}
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 {
2021-09-01 21:18:52 +00:00
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
}