Skip to content

Commit 62b6e6d

Browse files
authored
Merge pull request #171 from extemporalgenome/go122-loops
use go1.22 loops
2 parents 95ef58a + 5468938 commit 62b6e6d

File tree

8 files changed

+46
-50
lines changed

8 files changed

+46
-50
lines changed

mutex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (m *Mutex) lockContext(ctx context.Context, tries int) error {
8383
}
8484

8585
var timer *time.Timer
86-
for i := 0; i < tries; i++ {
86+
for i := range tries {
8787
if i != 0 {
8888
if timer == nil {
8989
timer = time.NewTimer(m.delayFunc(i))

mutex_test.go

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ func TestMutex(t *testing.T) {
1818
t.Run(k, func(t *testing.T) {
1919
mutexes := newTestMutexes(v.pools, k+"-test-mutex", v.poolCount)
2020
eg := errgroup.Group{}
21-
for i, mutex := range mutexes {
22-
func(i int, mutex *Mutex) {
23-
eg.Go(func() error {
24-
err := mutex.Lock()
25-
if err != nil {
26-
return err
27-
}
28-
defer mutex.Unlock()
21+
for _, mutex := range mutexes {
22+
eg.Go(func() error {
23+
err := mutex.Lock()
24+
if err != nil {
25+
return err
26+
}
27+
defer mutex.Unlock()
2928

30-
if !isAcquired(ctx, v.pools, mutex) {
31-
return fmt.Errorf("Expected n >= %d, got %d", mutex.quorum, countAcquiredPools(ctx, v.pools, mutex))
32-
}
33-
return nil
34-
})
35-
}(i, mutex)
29+
if !isAcquired(ctx, v.pools, mutex) {
30+
return fmt.Errorf("Expected n >= %d, got %d", mutex.quorum, countAcquiredPools(ctx, v.pools, mutex))
31+
}
32+
return nil
33+
})
3634
}
3735
err := eg.Wait()
3836
if err != nil {
@@ -48,31 +46,29 @@ func TestTryLock(t *testing.T) {
4846
t.Run(k, func(t *testing.T) {
4947
mutexes := newTestMutexes(v.pools, k+"-test-trylock", v.poolCount)
5048
eg := errgroup.Group{}
51-
for i, mutex := range mutexes {
52-
func(i int, mutex *Mutex) {
53-
eg.Go(func() error {
54-
err := mutex.TryLockContext(context.Background())
55-
for {
56-
if err == nil {
57-
break
58-
}
59-
60-
time.Sleep(100 * time.Millisecond) // 1ms
49+
for _, mutex := range mutexes {
50+
eg.Go(func() error {
51+
err := mutex.TryLockContext(context.Background())
52+
for {
53+
if err == nil {
54+
break
6155
}
6256

63-
ok, err := mutex.Extend()
64-
if err != nil || !ok {
65-
t.Fatalf("extend failed: %v", err)
66-
}
57+
time.Sleep(100 * time.Millisecond) // 1ms
58+
}
59+
60+
ok, err := mutex.Extend()
61+
if err != nil || !ok {
62+
t.Fatalf("extend failed: %v", err)
63+
}
6764

68-
defer mutex.Unlock()
65+
defer mutex.Unlock()
6966

70-
if !isAcquired(ctx, v.pools, mutex) {
71-
return fmt.Errorf("Expected n >= %d, got %d", mutex.quorum, countAcquiredPools(ctx, v.pools, mutex))
72-
}
73-
return nil
74-
})
75-
}(i, mutex)
67+
if !isAcquired(ctx, v.pools, mutex) {
68+
return fmt.Errorf("Expected n >= %d, got %d", mutex.quorum, countAcquiredPools(ctx, v.pools, mutex))
69+
}
70+
return nil
71+
})
7672
}
7773
err := eg.Wait()
7874
if err != nil {
@@ -250,7 +246,7 @@ func TestMutexQuorum(t *testing.T) {
250246
ctx := context.Background()
251247
for k, v := range makeCases(4) {
252248
t.Run(k, func(t *testing.T) {
253-
for mask := 0; mask < 1<<uint(len(v.pools)); mask++ {
249+
for mask := range 1 << len(v.pools) {
254250
mutexes := newTestMutexes(v.pools, k+"-test-mutex-partial-"+strconv.Itoa(mask), 1)
255251
mutex := mutexes[0]
256252
mutex.tries = 1
@@ -387,7 +383,7 @@ func clogPools(pools []redis.Pool, mask int, mutex *Mutex) int {
387383

388384
func newTestMutexes(pools []redis.Pool, name string, n int) []*Mutex {
389385
mutexes := make([]*Mutex, n)
390-
for i := 0; i < n; i++ {
386+
for i := range n {
391387
mutexes[i] = &Mutex{
392388
name: name + "-" + strconv.Itoa(i),
393389
expiry: 8 * time.Second,

redis/goredis/goredis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *conn) Eval(script *redsyncredis.Script, keysAndArgs ...interface{}) (in
5555
args := keysAndArgs
5656

5757
if script.KeyCount > 0 {
58-
for i := 0; i < script.KeyCount; i++ {
58+
for i := range script.KeyCount {
5959
keys[i] = keysAndArgs[i].(string)
6060
}
6161

redis/goredis/v7/goredis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *conn) Eval(script *redsyncredis.Script, keysAndArgs ...interface{}) (in
5858
args := keysAndArgs
5959

6060
if script.KeyCount > 0 {
61-
for i := 0; i < script.KeyCount; i++ {
61+
for i := range script.KeyCount {
6262
keys[i] = keysAndArgs[i].(string)
6363
}
6464
args = keysAndArgs[script.KeyCount:]

redis/goredis/v8/goredis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (c *conn) Eval(script *redsyncredis.Script, keysAndArgs ...interface{}) (in
5353
args := keysAndArgs
5454

5555
if script.KeyCount > 0 {
56-
for i := 0; i < script.KeyCount; i++ {
56+
for i := range script.KeyCount {
5757
keys[i] = keysAndArgs[i].(string)
5858
}
5959
args = keysAndArgs[script.KeyCount:]

redis/goredis/v9/goredis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (c *conn) Eval(script *redsyncredis.Script, keysAndArgs ...interface{}) (in
5353
args := keysAndArgs
5454

5555
if script.KeyCount > 0 {
56-
for i := 0; i < script.KeyCount; i++ {
56+
for i := range script.KeyCount {
5757
keys[i] = keysAndArgs[i].(string)
5858
}
5959
args = keysAndArgs[script.KeyCount:]

redis/rueidis/rueidis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *conn) Eval(script *redsyncredis.Script, keysAndArgs ...interface{}) (in
5555
args := keysAndArgs
5656

5757
if script.KeyCount > 0 {
58-
for i := 0; i < script.KeyCount; i++ {
58+
for i := range script.KeyCount {
5959
keys[i] = keysAndArgs[i].(string)
6060
}
6161
args = keysAndArgs[script.KeyCount:]

redsync_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const (
7373
)
7474

7575
func TestMain(m *testing.M) {
76-
for i := 0; i < ServerPoolSize*ServerPools; i++ {
76+
for i := range ServerPoolSize * ServerPools {
7777
server, err := tempredis.Start(tempredis.Config{
7878
"port": strconv.Itoa(51200 + i),
7979
})
@@ -107,7 +107,7 @@ func newMockPoolsRedigo(n int) []redis.Pool {
107107

108108
offset := RedigoBlock * ServerPoolSize
109109

110-
for i := 0; i < n; i++ {
110+
for i := range n {
111111
server := servers[i+offset]
112112
pools[i] = redigo.NewPool(&redigolib.Pool{
113113
MaxIdle: 3,
@@ -129,7 +129,7 @@ func newMockPoolsGoredis(n int) []redis.Pool {
129129

130130
offset := GoredisBlock * ServerPoolSize
131131

132-
for i := 0; i < n; i++ {
132+
for i := range n {
133133
client := goredislib.NewClient(&goredislib.Options{
134134
Network: "unix",
135135
Addr: servers[i+offset].Socket(),
@@ -144,7 +144,7 @@ func newMockPoolsGoredisV7(n int) []redis.Pool {
144144

145145
offset := GoredisV7Block * ServerPoolSize
146146

147-
for i := 0; i < n; i++ {
147+
for i := range n {
148148
client := goredislib_v7.NewClient(&goredislib_v7.Options{
149149
Network: "unix",
150150
Addr: servers[i+offset].Socket(),
@@ -159,7 +159,7 @@ func newMockPoolsGoredisV8(n int) []redis.Pool {
159159

160160
offset := GoredisV8Block * ServerPoolSize
161161

162-
for i := 0; i < n; i++ {
162+
for i := range n {
163163
client := goredislib_v8.NewClient(&goredislib_v8.Options{
164164
Network: "unix",
165165
Addr: servers[i+offset].Socket(),
@@ -174,7 +174,7 @@ func newMockPoolsGoredisV9(n int) []redis.Pool {
174174

175175
offset := GoredisV9Block * ServerPoolSize
176176

177-
for i := 0; i < n; i++ {
177+
for i := range n {
178178
client := goredislib_v9.NewClient(&goredislib_v9.Options{
179179
Network: "unix",
180180
Addr: servers[i+offset].Socket(),
@@ -189,7 +189,7 @@ func newMockPoolsRueidis(n int) []redis.Pool {
189189

190190
offset := RueidisBlock * ServerPoolSize
191191

192-
for i := 0; i < n; i++ {
192+
for i := range n {
193193
client, err := rueidislib.NewClient(rueidislib.ClientOption{
194194
InitAddress: []string{"127.0.0.1:" + strconv.Itoa(51200+i+offset)},
195195
})

0 commit comments

Comments
 (0)