fix default selector

This commit is contained in:
ginuerzh
2022-09-02 21:53:50 +08:00
parent 00f7fa2997
commit 6546f4a905
6 changed files with 70 additions and 10 deletions

View File

@ -2,11 +2,14 @@ package selector
import (
"context"
"math/rand"
"sync"
"sync/atomic"
"time"
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 {
@ -77,3 +80,28 @@ func (s *fifoStrategy[T]) Apply(ctx context.Context, vs ...T) (v T) {
}
return vs[0]
}
type hashStrategy[T selector.Selectable] struct {
r *rand.Rand
mu sync.Mutex
}
func HashStrategy[T selector.Selectable]() selector.Strategy[T] {
return &hashStrategy[T]{
r: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
func (s *hashStrategy[T]) Apply(ctx context.Context, vs ...T) (v T) {
if len(vs) == 0 {
return
}
if h := sx.HashFromContext(ctx); h != nil {
return vs[h.Value%len(vs)]
}
s.mu.Lock()
defer s.mu.Unlock()
return vs[s.r.Intn(len(vs))]
}