mirror of https://github.com/docker/docs.git
improve locks in queue.go
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
parent
2999809358
commit
3a1df50ed5
|
@ -21,27 +21,23 @@ func NewQueue() *Queue {
|
||||||
|
|
||||||
// Add tries to Do the item, if it's not possible, add the item to the queue for future tries
|
// Add tries to Do the item, if it's not possible, add the item to the queue for future tries
|
||||||
func (q *Queue) Add(item Item) {
|
func (q *Queue) Add(item Item) {
|
||||||
q.Lock()
|
|
||||||
defer q.Unlock()
|
|
||||||
|
|
||||||
if !item.Do() {
|
if !item.Do() {
|
||||||
|
q.Lock()
|
||||||
q.items[item.ID()] = item
|
q.items[item.ID()] = item
|
||||||
|
q.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove an item from the queue
|
// Remove an item from the queue
|
||||||
func (q *Queue) Remove(items ...Item) {
|
func (q *Queue) Remove(items ...Item) {
|
||||||
q.Lock()
|
q.Lock()
|
||||||
defer q.Unlock()
|
|
||||||
|
|
||||||
q.remove(items...)
|
q.remove(items...)
|
||||||
|
q.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process tries to Do all the items in the queue and remove the items successfully done
|
// Process tries to Do all the items in the queue and remove the items successfully done
|
||||||
func (q *Queue) Process() {
|
func (q *Queue) Process() {
|
||||||
q.Lock()
|
q.Lock()
|
||||||
defer q.Unlock()
|
|
||||||
|
|
||||||
toRemove := []Item{}
|
toRemove := []Item{}
|
||||||
for _, item := range q.items {
|
for _, item := range q.items {
|
||||||
if item.Do() {
|
if item.Do() {
|
||||||
|
@ -50,6 +46,7 @@ func (q *Queue) Process() {
|
||||||
}
|
}
|
||||||
|
|
||||||
q.remove(toRemove...)
|
q.remove(toRemove...)
|
||||||
|
q.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queue) remove(items ...Item) {
|
func (q *Queue) remove(items ...Item) {
|
||||||
|
|
Loading…
Reference in New Issue