mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
chore: refactor ring (#3739)
This commit is contained in:
parent
c220b5d886
commit
eb14d1347e
@ -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…
Reference in New Issue
Block a user