Skip to content

Commit 2e21cf0

Browse files
committed
refactor: replace manual slice iteration with slices.Backward for improved readability
1 parent 7b27e09 commit 2e21cf0

4 files changed

Lines changed: 11 additions & 9 deletions

File tree

health/watch_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package health
22

33
import (
44
"context"
5+
"slices"
56
"sync"
67
"testing"
78
"time"
@@ -224,9 +225,9 @@ func (s *fakeStore) GetLatestResult(_ context.Context, _ string, checkID id.ID)
224225
s.mu.Lock()
225226
defer s.mu.Unlock()
226227

227-
for i := len(s.results) - 1; i >= 0; i-- {
228-
if s.results[i].CheckID == checkID {
229-
return s.results[i], nil
228+
for _, r := range slices.Backward(s.results) {
229+
if r.CheckID == checkID {
230+
return r, nil
230231
}
231232
}
232233

metrics/service_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *service) Track(instanceID id.ID) {
6565
s.rings[key] = newRingBuffer(s.cfg.RetentionCapacity)
6666
}
6767

68-
ctx, cancel := context.WithCancel(context.Background())
68+
ctx, cancel := context.WithCancel(context.Background()) //nolint:gosec // cancel is retained in s.pollers and invoked by Untrack
6969
s.pollers[key] = cancel
7070
s.mu.Unlock()
7171

store/memory/health.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package memory
33
import (
44
"context"
55
"fmt"
6+
"slices"
67

78
ctrlplane "github.com/xraph/ctrlplane"
89
"github.com/xraph/ctrlplane/health"
@@ -146,9 +147,9 @@ func (s *Store) GetLatestResult(_ context.Context, tenantID string, checkID id.I
146147
return nil, fmt.Errorf("%w: no results for check %s", ctrlplane.ErrNotFound, key)
147148
}
148149

149-
for i := len(results) - 1; i >= 0; i-- {
150-
if results[i].TenantID == tenantID {
151-
clone := results[i]
150+
for _, r := range slices.Backward(results) {
151+
if r.TenantID == tenantID {
152+
clone := r
152153

153154
return &clone, nil
154155
}

store/memory/telemetry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package memory
33
import (
44
"context"
55
"fmt"
6+
"slices"
67

78
ctrlplane "github.com/xraph/ctrlplane"
89
"github.com/xraph/ctrlplane/id"
@@ -160,8 +161,7 @@ func (s *Store) GetLatestResourceSnapshot(_ context.Context, tenantID string, in
160161

161162
instKey := idStr(instanceID)
162163

163-
for i := len(s.resourceSnapshots) - 1; i >= 0; i-- {
164-
snap := s.resourceSnapshots[i]
164+
for _, snap := range slices.Backward(s.resourceSnapshots) {
165165
if idStr(snap.InstanceID) == instKey && snap.TenantID == tenantID {
166166
return &snap, nil
167167
}

0 commit comments

Comments
 (0)