Refactor: queue use generics

This commit is contained in:
gVisor bot 2022-04-06 01:07:08 +08:00
parent 58c7157f71
commit 06b780f5cc
2 changed files with 22 additions and 21 deletions

View file

@ -21,7 +21,7 @@ var UnifiedDelay = atomic.NewBool(false)
type Proxy struct { type Proxy struct {
C.ProxyAdapter C.ProxyAdapter
history *queue.Queue history *queue.Queue[C.DelayHistory]
alive *atomic.Bool alive *atomic.Bool
} }
@ -67,7 +67,7 @@ func (p *Proxy) DelayHistory() []C.DelayHistory {
queue := p.history.Copy() queue := p.history.Copy()
histories := []C.DelayHistory{} histories := []C.DelayHistory{}
for _, item := range queue { for _, item := range queue {
histories = append(histories, item.(C.DelayHistory)) histories = append(histories, item)
} }
return histories return histories
} }
@ -80,11 +80,7 @@ func (p *Proxy) LastDelay() (delay uint16) {
return max return max
} }
last := p.history.Last() history := p.history.Last()
if last == nil {
return max
}
history := last.(C.DelayHistory)
if history.Delay == 0 { if history.Delay == 0 {
return max return max
} }
@ -178,7 +174,7 @@ func (p *Proxy) URLTest(ctx context.Context, url string) (t uint16, err error) {
} }
func NewProxy(adapter C.ProxyAdapter) *Proxy { func NewProxy(adapter C.ProxyAdapter) *Proxy {
return &Proxy{adapter, queue.New(10), atomic.NewBool(true)} return &Proxy{adapter, queue.New[C.DelayHistory](10), atomic.NewBool(true)}
} }
func urlToMetadata(rawURL string) (addr C.Metadata, err error) { func urlToMetadata(rawURL string) (addr C.Metadata, err error) {

View file

@ -5,13 +5,13 @@ import (
) )
// Queue is a simple concurrent safe queue // Queue is a simple concurrent safe queue
type Queue struct { type Queue[T any] struct {
items []any items []T
lock sync.RWMutex lock sync.RWMutex
} }
// Put add the item to the queue. // Put add the item to the queue.
func (q *Queue) Put(items ...any) { func (q *Queue[T]) Put(items ...T) {
if len(items) == 0 { if len(items) == 0 {
return return
} }
@ -22,9 +22,9 @@ func (q *Queue) Put(items ...any) {
} }
// Pop returns the head of items. // Pop returns the head of items.
func (q *Queue) Pop() any { func (q *Queue[T]) Pop() T {
if len(q.items) == 0 { if len(q.items) == 0 {
return nil return GetZero[T]()
} }
q.lock.Lock() q.lock.Lock()
@ -35,9 +35,9 @@ func (q *Queue) Pop() any {
} }
// Last returns the last of item. // Last returns the last of item.
func (q *Queue) Last() any { func (q *Queue[T]) Last() T {
if len(q.items) == 0 { if len(q.items) == 0 {
return nil return GetZero[T]()
} }
q.lock.RLock() q.lock.RLock()
@ -47,8 +47,8 @@ func (q *Queue) Last() any {
} }
// Copy get the copy of queue. // Copy get the copy of queue.
func (q *Queue) Copy() []any { func (q *Queue[T]) Copy() []T {
items := []any{} items := []T{}
q.lock.RLock() q.lock.RLock()
items = append(items, q.items...) items = append(items, q.items...)
q.lock.RUnlock() q.lock.RUnlock()
@ -56,7 +56,7 @@ func (q *Queue) Copy() []any {
} }
// Len returns the number of items in this queue. // Len returns the number of items in this queue.
func (q *Queue) Len() int64 { func (q *Queue[T]) Len() int64 {
q.lock.Lock() q.lock.Lock()
defer q.lock.Unlock() defer q.lock.Unlock()
@ -64,8 +64,13 @@ func (q *Queue) Len() int64 {
} }
// New is a constructor for a new concurrent safe queue. // New is a constructor for a new concurrent safe queue.
func New(hint int64) *Queue { func New[T any](hint int64) *Queue[T] {
return &Queue{ return &Queue[T]{
items: make([]any, 0, hint), items: make([]T, 0, hint),
} }
} }
func GetZero[T any]() T {
var result T
return result
}