filer: avoid possible timeouts for updates and deletions

This commit is contained in:
Chris Lu
2020-02-20 15:44:17 -08:00
parent 45156cc2fe
commit 621cdbdf58
4 changed files with 95 additions and 27 deletions

View File

@@ -0,0 +1,45 @@
package util
import "sync"
type UnboundedQueue struct {
outbound []string
outboundLock sync.RWMutex
inbound []string
inboundLock sync.RWMutex
}
func NewUnboundedQueue() *UnboundedQueue {
q := &UnboundedQueue{}
return q
}
func (q *UnboundedQueue) EnQueue(items ...string) {
q.inboundLock.Lock()
defer q.inboundLock.Unlock()
q.outbound = append(q.outbound, items...)
}
func (q *UnboundedQueue) Consume(fn func([]string)) {
q.outboundLock.Lock()
defer q.outboundLock.Unlock()
if len(q.outbound) == 0 {
q.inboundLock.Lock()
inbountLen := len(q.inbound)
if inbountLen > 0 {
t := q.outbound
q.outbound = q.inbound
q.inbound = t
}
q.inboundLock.Unlock()
}
if len(q.outbound) > 0 {
fn(q.outbound)
q.outbound = q.outbound[:0]
}
}

View File

@@ -0,0 +1,25 @@
package util
import "testing"
func TestEnqueueAndConsume(t *testing.T) {
q := NewUnboundedQueue()
q.EnQueue("1", "2", "3")
f := func(items []string) {
for _, t := range items {
println(t)
}
println("-----------------------")
}
q.Consume(f)
q.Consume(f)
q.EnQueue("4", "5")
q.EnQueue("6", "7")
q.Consume(f)
}