Skip to content

Commit

Permalink
feat: replace sortedList with sortedUniqueList (dragonflyoss#793)
Browse files Browse the repository at this point in the history
* feat: replace sortedList with sortedUniqueList

Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Nov 15, 2021
1 parent 10ff474 commit d389ce2
Show file tree
Hide file tree
Showing 22 changed files with 2,715 additions and 555 deletions.
2 changes: 1 addition & 1 deletion manager/types/cdn_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ type GetCDNClustersQuery struct {
}

type CDNClusterConfig struct {
LoadLimit uint `yaml:"loadLimit" mapstructure:"loadLimit" json:"load_limit" binding:"omitempty,gte=1"`
LoadLimit uint `yaml:"loadLimit" mapstructure:"loadLimit" json:"load_limit" binding:"omitempty,gte=1,lte=5000"`
NetTopology string `yaml:"netTopology" mapstructure:"netTopology" json:"net_topology"`
}
2 changes: 1 addition & 1 deletion manager/types/scheduler_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type SchedulerClusterConfig struct {
}

type SchedulerClusterClientConfig struct {
LoadLimit uint `yaml:"loadLimit" mapstructure:"loadLimit" json:"load_limit" binding:"omitempty,gte=1"`
LoadLimit uint `yaml:"loadLimit" mapstructure:"loadLimit" json:"load_limit" binding:"omitempty,gte=1,lte=5000"`
}

type SchedulerClusterScopes struct {
Expand Down
48 changes: 48 additions & 0 deletions pkg/container/list/mocks/list_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 137 additions & 0 deletions pkg/container/list/sorted_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2020 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//go:generate mockgen -destination ./mocks/list_mock.go -package mocks d7y.io/dragonfly/v2/pkg/container/list Item

package list

import (
"container/list"
"sync"
)

type Item interface {
SortedValue() int
}

type SortedList interface {
Len() int
Insert(Item)
Remove(Item)
Contains(Item) bool
Range(func(Item) bool)
ReverseRange(fn func(Item) bool)
}

type sortedList struct {
mu *sync.RWMutex
container *list.List
}

func NewSortedList() SortedList {
return &sortedList{
mu: &sync.RWMutex{},
container: list.New(),
}
}

func (l *sortedList) Len() int {
l.mu.RLock()
defer l.mu.RUnlock()

return l.container.Len()
}

func (l *sortedList) Insert(item Item) {
l.mu.Lock()
defer l.mu.Unlock()

for e := l.container.Front(); e != nil; e = e.Next() {
v, ok := e.Value.(Item)
if !ok {
continue
}

if v.SortedValue() >= item.SortedValue() {
l.container.InsertBefore(item, e)
return
}
}

l.container.PushBack(item)
}

func (l *sortedList) Remove(item Item) {
l.mu.Lock()
defer l.mu.Unlock()

for e := l.container.Front(); e != nil; e = e.Next() {
v, ok := e.Value.(Item)
if !ok {
continue
}

if v == item {
l.container.Remove(e)
return
}
}
}

func (l *sortedList) Contains(item Item) bool {
l.mu.RLock()
defer l.mu.RUnlock()

for e := l.container.Front(); e != nil; e = e.Next() {
if v, ok := e.Value.(Item); ok && v == item {
return true
}
}

return false
}

func (l *sortedList) Range(fn func(Item) bool) {
l.mu.RLock()
defer l.mu.RUnlock()

for e := l.container.Front(); e != nil; e = e.Next() {
v, ok := e.Value.(Item)
if !ok {
continue
}

if !fn(v) {
return
}
}
}

func (l *sortedList) ReverseRange(fn func(Item) bool) {
l.mu.RLock()
defer l.mu.RUnlock()

for e := l.container.Back(); e != nil; e = e.Prev() {
v, ok := e.Value.(Item)
if !ok {
continue
}

if !fn(v) {
return
}
}
}
Loading

0 comments on commit d389ce2

Please sign in to comment.