chore: refactor ring (#3739)

master
Kevin Wan 11 months ago committed by GitHub
parent c220b5d886
commit eb14d1347e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,14 +27,13 @@ func (r *Ring) Add(v any) {
r.lock.Lock()
defer r.lock.Unlock()
ringLength := len(r.elements)
r.elements[r.index%ringLength] = v
rlen := len(r.elements)
r.elements[r.index%rlen] = v
r.index++
// prevent ring index overflow
if r.index/ringLength >= 2 {
r.index = r.index - ringLength
if r.index >= rlen<<1 {
r.index -= rlen
}
}
@ -45,18 +44,18 @@ func (r *Ring) Take() []any {
var size int
var start int
ringLength := len(r.elements)
rlen := len(r.elements)
if r.index > ringLength {
size = ringLength
start = r.index % ringLength
if r.index > rlen {
size = rlen
start = r.index % rlen
} else {
size = r.index
}
elements := make([]any, size)
for i := 0; i < size; i++ {
elements[i] = r.elements[(start+i)%ringLength]
elements[i] = r.elements[(start+i)%rlen]
}
return elements

Loading…
Cancel
Save