update selector
This commit is contained in:
@ -4,18 +4,19 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
"github.com/go-gost/core/selector"
|
||||
)
|
||||
|
||||
type failFilter[T selector.Selectable] struct {
|
||||
type failFilter[T any] struct {
|
||||
maxFails int
|
||||
failTimeout time.Duration
|
||||
}
|
||||
|
||||
// FailFilter filters the dead objects.
|
||||
// An object is marked as dead if its failed count is greater than MaxFails.
|
||||
func FailFilter[T selector.Selectable](maxFails int, timeout time.Duration) selector.Filter[T] {
|
||||
func FailFilter[T any](maxFails int, timeout time.Duration) selector.Filter[T] {
|
||||
return &failFilter[T]{
|
||||
maxFails: maxFails,
|
||||
failTimeout: timeout,
|
||||
@ -31,12 +32,14 @@ func (f *failFilter[T]) Filter(ctx context.Context, vs ...T) []T {
|
||||
for _, v := range vs {
|
||||
maxFails := f.maxFails
|
||||
failTimeout := f.failTimeout
|
||||
if md := v.Metadata(); md != nil {
|
||||
if md.IsExists(labelMaxFails) {
|
||||
maxFails = mdutil.GetInt(md, labelMaxFails)
|
||||
}
|
||||
if md.IsExists(labelFailTimeout) {
|
||||
failTimeout = mdutil.GetDuration(md, labelFailTimeout)
|
||||
if mi, _ := any(v).(metadata.Metadatable); mi != nil {
|
||||
if md := mi.Metadata(); md != nil {
|
||||
if md.IsExists(labelMaxFails) {
|
||||
maxFails = mdutil.GetInt(md, labelMaxFails)
|
||||
}
|
||||
if md.IsExists(labelFailTimeout) {
|
||||
failTimeout = mdutil.GetDuration(md, labelFailTimeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
if maxFails <= 0 {
|
||||
@ -46,23 +49,25 @@ func (f *failFilter[T]) Filter(ctx context.Context, vs ...T) []T {
|
||||
failTimeout = DefaultFailTimeout
|
||||
}
|
||||
|
||||
if marker := v.Marker(); marker != nil {
|
||||
if marker.Count() < int64(maxFails) ||
|
||||
time.Since(marker.Time()) >= failTimeout {
|
||||
l = append(l, v)
|
||||
if mi, _ := any(v).(selector.Markable); mi != nil {
|
||||
if marker := mi.Marker(); marker != nil {
|
||||
if marker.Count() < int64(maxFails) ||
|
||||
time.Since(marker.Time()) >= failTimeout {
|
||||
l = append(l, v)
|
||||
}
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
l = append(l, v)
|
||||
}
|
||||
l = append(l, v)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
type backupFilter[T selector.Selectable] struct{}
|
||||
type backupFilter[T any] struct{}
|
||||
|
||||
// BackupFilter filters the backup objects.
|
||||
// An object is marked as backup if its metadata has backup flag.
|
||||
func BackupFilter[T selector.Selectable]() selector.Filter[T] {
|
||||
func BackupFilter[T any]() selector.Filter[T] {
|
||||
return &backupFilter[T]{}
|
||||
}
|
||||
|
||||
@ -74,11 +79,13 @@ func (f *backupFilter[T]) Filter(ctx context.Context, vs ...T) []T {
|
||||
|
||||
var l, backups []T
|
||||
for _, v := range vs {
|
||||
if mdutil.GetBool(v.Metadata(), labelBackup) {
|
||||
backups = append(backups, v)
|
||||
} else {
|
||||
l = append(l, v)
|
||||
if mi, _ := any(v).(metadata.Metadatable); mi != nil {
|
||||
if mdutil.GetBool(mi.Metadata(), labelBackup) {
|
||||
backups = append(backups, v)
|
||||
continue
|
||||
}
|
||||
}
|
||||
l = append(l, v)
|
||||
}
|
||||
|
||||
if len(l) == 0 {
|
||||
|
@ -20,12 +20,12 @@ const (
|
||||
labelFailTimeout = "failTimeout"
|
||||
)
|
||||
|
||||
type defaultSelector[T selector.Selectable] struct {
|
||||
type defaultSelector[T any] struct {
|
||||
strategy selector.Strategy[T]
|
||||
filters []selector.Filter[T]
|
||||
}
|
||||
|
||||
func NewSelector[T selector.Selectable](strategy selector.Strategy[T], filters ...selector.Filter[T]) selector.Selector[T] {
|
||||
func NewSelector[T any](strategy selector.Strategy[T], filters ...selector.Filter[T]) selector.Selector[T] {
|
||||
return &defaultSelector[T]{
|
||||
filters: filters,
|
||||
strategy: strategy,
|
||||
|
@ -7,18 +7,19 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
"github.com/go-gost/core/selector"
|
||||
sx "github.com/go-gost/x/internal/util/selector"
|
||||
)
|
||||
|
||||
type roundRobinStrategy[T selector.Selectable] struct {
|
||||
type roundRobinStrategy[T any] struct {
|
||||
counter uint64
|
||||
}
|
||||
|
||||
// RoundRobinStrategy is a strategy for node selector.
|
||||
// The node will be selected by round-robin algorithm.
|
||||
func RoundRobinStrategy[T selector.Selectable]() selector.Strategy[T] {
|
||||
func RoundRobinStrategy[T any]() selector.Strategy[T] {
|
||||
return &roundRobinStrategy[T]{}
|
||||
}
|
||||
|
||||
@ -31,14 +32,14 @@ func (s *roundRobinStrategy[T]) Apply(ctx context.Context, vs ...T) (v T) {
|
||||
return vs[int(n%uint64(len(vs)))]
|
||||
}
|
||||
|
||||
type randomStrategy[T selector.Selectable] struct {
|
||||
type randomStrategy[T any] struct {
|
||||
rw *randomWeighted[T]
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// RandomStrategy is a strategy for node selector.
|
||||
// The node will be selected randomly.
|
||||
func RandomStrategy[T selector.Selectable]() selector.Strategy[T] {
|
||||
func RandomStrategy[T any]() selector.Strategy[T] {
|
||||
return &randomStrategy[T]{
|
||||
rw: newRandomWeighted[T](),
|
||||
}
|
||||
@ -54,7 +55,10 @@ func (s *randomStrategy[T]) Apply(ctx context.Context, vs ...T) (v T) {
|
||||
|
||||
s.rw.Reset()
|
||||
for i := range vs {
|
||||
weight := mdutil.GetInt(vs[i].Metadata(), labelWeight)
|
||||
weight := 0
|
||||
if md, _ := any(vs[i]).(metadata.Metadatable); md != nil {
|
||||
weight = mdutil.GetInt(md.Metadata(), labelWeight)
|
||||
}
|
||||
if weight <= 0 {
|
||||
weight = 1
|
||||
}
|
||||
@ -64,12 +68,12 @@ func (s *randomStrategy[T]) Apply(ctx context.Context, vs ...T) (v T) {
|
||||
return s.rw.Next()
|
||||
}
|
||||
|
||||
type fifoStrategy[T selector.Selectable] struct{}
|
||||
type fifoStrategy[T any] struct{}
|
||||
|
||||
// FIFOStrategy is a strategy for node selector.
|
||||
// The node will be selected from first to last,
|
||||
// and will stick to the selected node until it is failed.
|
||||
func FIFOStrategy[T selector.Selectable]() selector.Strategy[T] {
|
||||
func FIFOStrategy[T any]() selector.Strategy[T] {
|
||||
return &fifoStrategy[T]{}
|
||||
}
|
||||
|
||||
@ -81,12 +85,12 @@ func (s *fifoStrategy[T]) Apply(ctx context.Context, vs ...T) (v T) {
|
||||
return vs[0]
|
||||
}
|
||||
|
||||
type hashStrategy[T selector.Selectable] struct {
|
||||
type hashStrategy[T any] struct {
|
||||
r *rand.Rand
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func HashStrategy[T selector.Selectable]() selector.Strategy[T] {
|
||||
func HashStrategy[T any]() selector.Strategy[T] {
|
||||
return &hashStrategy[T]{
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
}
|
||||
|
@ -3,22 +3,20 @@ package selector
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/selector"
|
||||
)
|
||||
|
||||
type randomWeightedItem[T selector.Selectable] struct {
|
||||
type randomWeightedItem[T any] struct {
|
||||
item T
|
||||
weight int
|
||||
}
|
||||
|
||||
type randomWeighted[T selector.Selectable] struct {
|
||||
type randomWeighted[T any] struct {
|
||||
items []*randomWeightedItem[T]
|
||||
sum int
|
||||
r *rand.Rand
|
||||
}
|
||||
|
||||
func newRandomWeighted[T selector.Selectable]() *randomWeighted[T] {
|
||||
func newRandomWeighted[T any]() *randomWeighted[T] {
|
||||
return &randomWeighted[T]{
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
}
|
||||
|
Reference in New Issue
Block a user