Chore: use generics as possible
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
package strmatcher
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"github.com/Dreamacro/clash/common/generics/list"
|
||||
)
|
||||
|
||||
const validCharCount = 53
|
||||
@ -190,7 +190,7 @@ func (ac *ACAutomaton) Add(domain string, t Type) {
|
||||
}
|
||||
|
||||
func (ac *ACAutomaton) Build() {
|
||||
queue := list.New()
|
||||
queue := list.New[Edge]()
|
||||
for i := 0; i < validCharCount; i++ {
|
||||
if ac.trie[0][i].nextNode != 0 {
|
||||
queue.PushBack(ac.trie[0][i])
|
||||
@ -201,7 +201,7 @@ func (ac *ACAutomaton) Build() {
|
||||
if front == nil {
|
||||
break
|
||||
} else {
|
||||
node := front.Value.(Edge).nextNode
|
||||
node := front.Value.nextNode
|
||||
queue.Remove(front)
|
||||
for i := 0; i < validCharCount; i++ {
|
||||
if ac.trie[node][i].nextNode != 0 {
|
||||
|
@ -21,10 +21,10 @@ var (
|
||||
ErrAddrNotFound = errors.New("addr not found")
|
||||
)
|
||||
|
||||
var interfaces = singledo.NewSingle(time.Second * 20)
|
||||
var interfaces = singledo.NewSingle[map[string]*Interface](time.Second * 20)
|
||||
|
||||
func ResolveInterface(name string) (*Interface, error) {
|
||||
value, err, _ := interfaces.Do(func() (any, error) {
|
||||
value, err, _ := interfaces.Do(func() (map[string]*Interface, error) {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -66,7 +66,7 @@ func ResolveInterface(name string) (*Interface, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ifaces := value.(map[string]*Interface)
|
||||
ifaces := value
|
||||
iface, ok := ifaces[name]
|
||||
if !ok {
|
||||
return nil, ErrIfaceNotFound
|
||||
|
@ -6,55 +6,55 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Factory = func(context.Context) (any, error)
|
||||
type Factory[T any] func(context.Context) (T, error)
|
||||
|
||||
type entry struct {
|
||||
elm any
|
||||
type entry[T any] struct {
|
||||
elm T
|
||||
time time.Time
|
||||
}
|
||||
|
||||
type Option func(*pool)
|
||||
type Option[T any] func(*pool[T])
|
||||
|
||||
// WithEvict set the evict callback
|
||||
func WithEvict(cb func(any)) Option {
|
||||
return func(p *pool) {
|
||||
func WithEvict[T any](cb func(T)) Option[T] {
|
||||
return func(p *pool[T]) {
|
||||
p.evict = cb
|
||||
}
|
||||
}
|
||||
|
||||
// WithAge defined element max age (millisecond)
|
||||
func WithAge(maxAge int64) Option {
|
||||
return func(p *pool) {
|
||||
func WithAge[T any](maxAge int64) Option[T] {
|
||||
return func(p *pool[T]) {
|
||||
p.maxAge = maxAge
|
||||
}
|
||||
}
|
||||
|
||||
// WithSize defined max size of Pool
|
||||
func WithSize(maxSize int) Option {
|
||||
return func(p *pool) {
|
||||
p.ch = make(chan any, maxSize)
|
||||
func WithSize[T any](maxSize int) Option[T] {
|
||||
return func(p *pool[T]) {
|
||||
p.ch = make(chan *entry[T], maxSize)
|
||||
}
|
||||
}
|
||||
|
||||
// Pool is for GC, see New for detail
|
||||
type Pool struct {
|
||||
*pool
|
||||
type Pool[T any] struct {
|
||||
*pool[T]
|
||||
}
|
||||
|
||||
type pool struct {
|
||||
ch chan any
|
||||
factory Factory
|
||||
evict func(any)
|
||||
type pool[T any] struct {
|
||||
ch chan *entry[T]
|
||||
factory Factory[T]
|
||||
evict func(T)
|
||||
maxAge int64
|
||||
}
|
||||
|
||||
func (p *pool) GetContext(ctx context.Context) (any, error) {
|
||||
func (p *pool[T]) GetContext(ctx context.Context) (T, error) {
|
||||
now := time.Now()
|
||||
for {
|
||||
select {
|
||||
case item := <-p.ch:
|
||||
elm := item.(*entry)
|
||||
if p.maxAge != 0 && now.Sub(item.(*entry).time).Milliseconds() > p.maxAge {
|
||||
elm := item
|
||||
if p.maxAge != 0 && now.Sub(item.time).Milliseconds() > p.maxAge {
|
||||
if p.evict != nil {
|
||||
p.evict(elm.elm)
|
||||
}
|
||||
@ -68,12 +68,12 @@ func (p *pool) GetContext(ctx context.Context) (any, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pool) Get() (any, error) {
|
||||
func (p *pool[T]) Get() (T, error) {
|
||||
return p.GetContext(context.Background())
|
||||
}
|
||||
|
||||
func (p *pool) Put(item any) {
|
||||
e := &entry{
|
||||
func (p *pool[T]) Put(item T) {
|
||||
e := &entry[T]{
|
||||
elm: item,
|
||||
time: time.Now(),
|
||||
}
|
||||
@ -90,17 +90,17 @@ func (p *pool) Put(item any) {
|
||||
}
|
||||
}
|
||||
|
||||
func recycle(p *Pool) {
|
||||
func recycle[T any](p *Pool[T]) {
|
||||
for item := range p.pool.ch {
|
||||
if p.pool.evict != nil {
|
||||
p.pool.evict(item.(*entry).elm)
|
||||
p.pool.evict(item.elm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func New(factory Factory, options ...Option) *Pool {
|
||||
p := &pool{
|
||||
ch: make(chan any, 10),
|
||||
func New[T any](factory Factory[T], options ...Option[T]) *Pool[T] {
|
||||
p := &pool[T]{
|
||||
ch: make(chan *entry[T], 10),
|
||||
factory: factory,
|
||||
}
|
||||
|
||||
@ -108,7 +108,7 @@ func New(factory Factory, options ...Option) *Pool {
|
||||
option(p)
|
||||
}
|
||||
|
||||
P := &Pool{p}
|
||||
runtime.SetFinalizer(P, recycle)
|
||||
P := &Pool[T]{p}
|
||||
runtime.SetFinalizer(P, recycle[T])
|
||||
return P
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func lg() Factory {
|
||||
func lg() Factory[int] {
|
||||
initial := -1
|
||||
return func(context.Context) (any, error) {
|
||||
return func(context.Context) (int, error) {
|
||||
initial++
|
||||
return initial, nil
|
||||
}
|
||||
@ -18,23 +18,23 @@ func lg() Factory {
|
||||
|
||||
func TestPool_Basic(t *testing.T) {
|
||||
g := lg()
|
||||
pool := New(g)
|
||||
pool := New[int](g)
|
||||
|
||||
elm, _ := pool.Get()
|
||||
assert.Equal(t, 0, elm.(int))
|
||||
assert.Equal(t, 0, elm)
|
||||
pool.Put(elm)
|
||||
elm, _ = pool.Get()
|
||||
assert.Equal(t, 0, elm.(int))
|
||||
assert.Equal(t, 0, elm)
|
||||
elm, _ = pool.Get()
|
||||
assert.Equal(t, 1, elm.(int))
|
||||
assert.Equal(t, 1, elm)
|
||||
}
|
||||
|
||||
func TestPool_MaxSize(t *testing.T) {
|
||||
g := lg()
|
||||
size := 5
|
||||
pool := New(g, WithSize(size))
|
||||
pool := New[int](g, WithSize[int](size))
|
||||
|
||||
var items []any
|
||||
var items []int
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
item, _ := pool.Get()
|
||||
@ -42,7 +42,7 @@ func TestPool_MaxSize(t *testing.T) {
|
||||
}
|
||||
|
||||
extra, _ := pool.Get()
|
||||
assert.Equal(t, size, extra.(int))
|
||||
assert.Equal(t, size, extra)
|
||||
|
||||
for _, item := range items {
|
||||
pool.Put(item)
|
||||
@ -52,22 +52,22 @@ func TestPool_MaxSize(t *testing.T) {
|
||||
|
||||
for _, item := range items {
|
||||
elm, _ := pool.Get()
|
||||
assert.Equal(t, item.(int), elm.(int))
|
||||
assert.Equal(t, item, elm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPool_MaxAge(t *testing.T) {
|
||||
g := lg()
|
||||
pool := New(g, WithAge(20))
|
||||
pool := New[int](g, WithAge[int](20))
|
||||
|
||||
elm, _ := pool.Get()
|
||||
pool.Put(elm)
|
||||
|
||||
elm, _ = pool.Get()
|
||||
assert.Equal(t, 0, elm.(int))
|
||||
assert.Equal(t, 0, elm)
|
||||
pool.Put(elm)
|
||||
|
||||
time.Sleep(time.Millisecond * 22)
|
||||
elm, _ = pool.Get()
|
||||
assert.Equal(t, 1, elm.(int))
|
||||
assert.Equal(t, 1, elm)
|
||||
}
|
||||
|
Reference in New Issue
Block a user