Skip to content

Commit

Permalink
fix: remove iterator mutex as it's not needed
Browse files Browse the repository at this point in the history
It's not safe to concurrently use an iterator even with a mutex.
  • Loading branch information
ash2k committed Aug 13, 2022
1 parent 9be4a9e commit a10bc3e
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@ package redis

import (
"context"
"sync"
)

// ScanIterator is used to incrementally iterate over a collection of elements.
// It's safe for concurrent use by multiple goroutines.
type ScanIterator struct {
mu sync.Mutex // protects Scanner and pos
cmd *ScanCmd
pos int
}

// Err returns the last iterator error, if any.
func (it *ScanIterator) Err() error {
it.mu.Lock()
err := it.cmd.Err()
it.mu.Unlock()
return err
return it.cmd.Err()
}

// Next advances the cursor and returns true if more values can be read.
func (it *ScanIterator) Next(ctx context.Context) bool {
it.mu.Lock()
defer it.mu.Unlock()

// Instantly return on errors.
if it.cmd.Err() != nil {
return false
Expand Down Expand Up @@ -68,10 +59,8 @@ func (it *ScanIterator) Next(ctx context.Context) bool {
// Val returns the key/field at the current cursor position.
func (it *ScanIterator) Val() string {
var v string
it.mu.Lock()
if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) {
v = it.cmd.page[it.pos-1]
}
it.mu.Unlock()
return v
}

0 comments on commit a10bc3e

Please sign in to comment.