forked from mirror/redis
fix: remove iterator mutex as it's not needed
It's not safe to concurrently use an iterator even with a mutex.
This commit is contained in:
parent
9be4a9e6d3
commit
a10bc3e21c
13
iterator.go
13
iterator.go
|
@ -2,30 +2,21 @@ package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ScanIterator is used to incrementally iterate over a collection of elements.
|
// ScanIterator is used to incrementally iterate over a collection of elements.
|
||||||
// It's safe for concurrent use by multiple goroutines.
|
|
||||||
type ScanIterator struct {
|
type ScanIterator struct {
|
||||||
mu sync.Mutex // protects Scanner and pos
|
|
||||||
cmd *ScanCmd
|
cmd *ScanCmd
|
||||||
pos int
|
pos int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err returns the last iterator error, if any.
|
// Err returns the last iterator error, if any.
|
||||||
func (it *ScanIterator) Err() error {
|
func (it *ScanIterator) Err() error {
|
||||||
it.mu.Lock()
|
return it.cmd.Err()
|
||||||
err := it.cmd.Err()
|
|
||||||
it.mu.Unlock()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next advances the cursor and returns true if more values can be read.
|
// Next advances the cursor and returns true if more values can be read.
|
||||||
func (it *ScanIterator) Next(ctx context.Context) bool {
|
func (it *ScanIterator) Next(ctx context.Context) bool {
|
||||||
it.mu.Lock()
|
|
||||||
defer it.mu.Unlock()
|
|
||||||
|
|
||||||
// Instantly return on errors.
|
// Instantly return on errors.
|
||||||
if it.cmd.Err() != nil {
|
if it.cmd.Err() != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -68,10 +59,8 @@ func (it *ScanIterator) Next(ctx context.Context) bool {
|
||||||
// Val returns the key/field at the current cursor position.
|
// Val returns the key/field at the current cursor position.
|
||||||
func (it *ScanIterator) Val() string {
|
func (it *ScanIterator) Val() string {
|
||||||
var v string
|
var v string
|
||||||
it.mu.Lock()
|
|
||||||
if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) {
|
if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) {
|
||||||
v = it.cmd.page[it.pos-1]
|
v = it.cmd.page[it.pos-1]
|
||||||
}
|
}
|
||||||
it.mu.Unlock()
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue