-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/fetch p2p network info dfdaemon #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/antsystem
Are you sure you want to change the base?
Changes from 37 commits
9583a83
db3ea61
a314b08
13601d0
aa981f7
ff6a4b0
e16349c
a49b32a
22dba5c
7dd3f1d
1854120
d395916
4cbd748
8e298dc
078fad0
4d932e3
c260388
e9a01f9
f2bbd20
9ed43a3
7db37c0
d2f0125
66fe556
2518240
3bbfb37
5b4b22f
92417c8
2cc1d53
2e058b9
d9ff274
a8d1401
6bebf54
64870ef
5431aa7
1086c73
6e82829
91bb821
f241659
5b9b471
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package localManager | ||
|
|
||
| import "github.com/dragonflyoss/Dragonfly/pkg/syncmap" | ||
|
|
||
| type cache struct { | ||
|
|
||
| } | ||
|
|
||
| type cacheManager struct { | ||
| // key is taskID, value is the | ||
| taskContainer *syncmap.SyncMap | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| package localManager | ||
|
|
||
| import ( | ||
| "github.com/dragonflyoss/Dragonfly/pkg/errortypes" | ||
| "sync" | ||
| ) | ||
|
|
||
| // finiteQueue provides a circle queue with capacity, and it will weed out the earlier item if full | ||
| type finiteQueue struct { | ||
| sync.Mutex | ||
| capacity int | ||
| head *itemNode | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是否可以考虑使用
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| tail *itemNode | ||
|
|
||
| itemMap map[string]*itemNode | ||
| } | ||
|
|
||
| type itemNode struct { | ||
| prv *itemNode | ||
| next *itemNode | ||
| data interface{} | ||
| key string | ||
| } | ||
|
|
||
| func newFiniteQueue(capacity int) *finiteQueue { | ||
| return &finiteQueue{ | ||
| capacity: capacity, | ||
| head: nil, | ||
| tail: nil, | ||
| itemMap: make(map[string]*itemNode, capacity), | ||
| } | ||
| } | ||
|
|
||
| // put item to front | ||
| func (q *finiteQueue) putFront(key string, data interface{}) { | ||
| q.Lock() | ||
| defer q.Unlock() | ||
|
|
||
| if i, ok := q.itemMap[key]; ok { | ||
| //todo: update data | ||
| i.data = data | ||
| q.internalPutFront(i) | ||
| return | ||
| } | ||
|
|
||
| if len(q.itemMap) >= q.capacity { | ||
| // remove the earliest item | ||
| i := q.internalRemoveTail() | ||
| if i != nil { | ||
| delete(q.itemMap, i.key) | ||
| } | ||
| } | ||
|
|
||
| node := &itemNode{ | ||
| key: key, | ||
| data: data, | ||
| } | ||
|
|
||
| q.itemMap[key] = node | ||
| q.internalPutFront(node) | ||
| } | ||
|
|
||
| // getFront will get several item from front and not poll out them. | ||
| func (q *finiteQueue) getFront(count int) []interface{} { | ||
| q.Lock() | ||
| defer q.Unlock() | ||
|
|
||
| if q.head == nil { | ||
| return nil | ||
| } | ||
|
|
||
| result := make([]interface{}, count) | ||
| item := q.head | ||
| index := 0 | ||
| for{ | ||
| result[index] = item.data | ||
| index ++ | ||
| if index >= count { | ||
| break | ||
| } | ||
| if item == q.tail { | ||
| break | ||
| } | ||
| item = item.next | ||
| } | ||
|
|
||
| return result[:index] | ||
| } | ||
|
|
||
| func (q *finiteQueue) getItemByKey(key string) (interface{}, error) { | ||
| q.Lock() | ||
| defer q.Unlock() | ||
|
|
||
| if data, exist := q.itemMap[key]; exist { | ||
| return data.data, nil | ||
| } | ||
|
|
||
| return nil, errortypes.ErrDataNotFound | ||
| } | ||
|
|
||
| func (q *finiteQueue) internalPutFront(i *itemNode) { | ||
| if q.head == i { | ||
| return | ||
| } | ||
|
|
||
| if q.head == nil { | ||
| q.head = i | ||
| q.tail = i | ||
| i.prv = i | ||
| i.next = i | ||
| return | ||
| } | ||
|
|
||
| if q.tail == i { | ||
| q.tail = i.prv | ||
| } | ||
|
|
||
| if i.prv != nil && i.next != nil { | ||
| i.prv.next = i.next | ||
| i.next.prv = i.prv | ||
| } | ||
|
|
||
| q.tail.next = i | ||
| q.head.prv = i | ||
| i.prv = q.tail | ||
| i.next = q.head | ||
|
|
||
| q.head = i | ||
| } | ||
|
|
||
| func (q *finiteQueue) internalRemoveTail() *itemNode { | ||
| if q.tail == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if q.head == q.tail { | ||
| q.head = nil | ||
| q.tail = nil | ||
| return q.tail | ||
| } | ||
|
|
||
| result := q.tail | ||
| q.tail.prv.next = q.head | ||
| q.head.prv = q.tail.prv | ||
| q.tail = q.tail.prv | ||
|
|
||
| return result | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/queue下面有个有限队列的实现,跟这个区别在于队列满时会阻塞。可否考虑将本实现也放到pkg/queue下面。另外我觉得应该叫循环队列,但是我从实现上看更像个cache。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯 这是一个有限长度的循环队列。已放在pkg/queue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done