From 0c65f6962adea811d0f07bf554e23826687fd607 Mon Sep 17 00:00:00 2001 From: yaling888 <73897884+yaling888@users.noreply.github.com> Date: Wed, 6 Apr 2022 01:07:08 +0800 Subject: [PATCH] Refactor: queue use generics --- adapter/adapter.go | 12 ++++-------- common/queue/queue.go | 31 ++++++++++++++++++------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/adapter/adapter.go b/adapter/adapter.go index dbb31bf0..1fd9bc11 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -21,7 +21,7 @@ var UnifiedDelay = atomic.NewBool(false) type Proxy struct { C.ProxyAdapter - history *queue.Queue + history *queue.Queue[C.DelayHistory] alive *atomic.Bool } @@ -67,7 +67,7 @@ func (p *Proxy) DelayHistory() []C.DelayHistory { queue := p.history.Copy() histories := []C.DelayHistory{} for _, item := range queue { - histories = append(histories, item.(C.DelayHistory)) + histories = append(histories, item) } return histories } @@ -80,11 +80,7 @@ func (p *Proxy) LastDelay() (delay uint16) { return max } - last := p.history.Last() - if last == nil { - return max - } - history := last.(C.DelayHistory) + history := p.history.Last() if history.Delay == 0 { 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 { - 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) { diff --git a/common/queue/queue.go b/common/queue/queue.go index 60257f56..4755cb35 100644 --- a/common/queue/queue.go +++ b/common/queue/queue.go @@ -5,13 +5,13 @@ import ( ) // Queue is a simple concurrent safe queue -type Queue struct { - items []any +type Queue[T any] struct { + items []T lock sync.RWMutex } // Put add the item to the queue. -func (q *Queue) Put(items ...any) { +func (q *Queue[T]) Put(items ...T) { if len(items) == 0 { return } @@ -22,9 +22,9 @@ func (q *Queue) Put(items ...any) { } // Pop returns the head of items. -func (q *Queue) Pop() any { +func (q *Queue[T]) Pop() T { if len(q.items) == 0 { - return nil + return GetZero[T]() } q.lock.Lock() @@ -35,9 +35,9 @@ func (q *Queue) Pop() any { } // Last returns the last of item. -func (q *Queue) Last() any { +func (q *Queue[T]) Last() T { if len(q.items) == 0 { - return nil + return GetZero[T]() } q.lock.RLock() @@ -47,8 +47,8 @@ func (q *Queue) Last() any { } // Copy get the copy of queue. -func (q *Queue) Copy() []any { - items := []any{} +func (q *Queue[T]) Copy() []T { + items := []T{} q.lock.RLock() items = append(items, q.items...) q.lock.RUnlock() @@ -56,7 +56,7 @@ func (q *Queue) Copy() []any { } // Len returns the number of items in this queue. -func (q *Queue) Len() int64 { +func (q *Queue[T]) Len() int64 { q.lock.Lock() defer q.lock.Unlock() @@ -64,8 +64,13 @@ func (q *Queue) Len() int64 { } // New is a constructor for a new concurrent safe queue. -func New(hint int64) *Queue { - return &Queue{ - items: make([]any, 0, hint), +func New[T any](hint int64) *Queue[T] { + return &Queue[T]{ + items: make([]T, 0, hint), } } + +func GetZero[T any]() T { + var result T + return result +}