Skip to content

Commit bdf4dfd

Browse files
author
Evsyukov Denis
committed
refactor: standardize release list cache key usage and improve type handling
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
1 parent 27238f5 commit bdf4dfd

File tree

6 files changed

+26
-32
lines changed

6 files changed

+26
-32
lines changed

pkg/helm/nelm/cache.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"time"
66
)
77

8+
const (
9+
releaseListCacheKey = "release-list"
10+
)
11+
812
// cacheEntry represents a cached value with TTL
913
type cacheEntry struct {
1014
value any
@@ -166,11 +170,11 @@ func (c *releaseStatusCache) setLabels(key string, labels map[string]string) {
166170
}
167171

168172
// getReleaseList retrieves cached list of release names if it exists and hasn't expired
169-
func (c *releaseStatusCache) getReleaseList(key string) ([]string, bool) {
173+
func (c *releaseStatusCache) getReleaseList() ([]string, bool) {
170174
c.mutex.RLock()
171175
defer c.mutex.RUnlock()
172176

173-
entry, exists := c.entries[key]
177+
entry, exists := c.entries[releaseListCacheKey]
174178
if !exists || entry.isExpired() {
175179
return nil, false
176180
}
@@ -182,11 +186,11 @@ func (c *releaseStatusCache) getReleaseList(key string) ([]string, bool) {
182186
}
183187

184188
// setReleaseList stores list of release names in cache with TTL
185-
func (c *releaseStatusCache) setReleaseList(key string, names []string) {
189+
func (c *releaseStatusCache) setReleaseList(names []string) {
186190
c.mutex.Lock()
187191
defer c.mutex.Unlock()
188192

189-
c.entries[key] = &cacheEntry{
193+
c.entries[releaseListCacheKey] = &cacheEntry{
190194
value: &releaseList{names: names},
191195
expiresAt: time.Now().Add(c.ttl),
192196
}

pkg/helm/nelm/cache_release_list_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ func TestReleaseListCache(t *testing.T) {
99
cache := newReleaseStatusCache(1 * time.Second)
1010

1111
// Test cache miss
12-
_, found := cache.getReleaseList("release-list")
12+
_, found := cache.getReleaseList()
1313
if found {
1414
t.Error("Expected cache miss for release list")
1515
}
1616

1717
// Test cache set and hit
1818
expectedList := []string{"release1", "release2", "release3"}
19-
cache.setReleaseList("release-list", expectedList)
19+
cache.setReleaseList(expectedList)
2020

21-
cachedList, found := cache.getReleaseList("release-list")
21+
cachedList, found := cache.getReleaseList()
2222
if !found {
2323
t.Error("Expected cache hit for release list")
2424
}
@@ -33,20 +33,20 @@ func TestReleaseListCache(t *testing.T) {
3333

3434
// Test cache expiration
3535
time.Sleep(1100 * time.Millisecond)
36-
_, found = cache.getReleaseList("release-list")
36+
_, found = cache.getReleaseList()
3737
if found {
3838
t.Error("Expected cache miss after expiration")
3939
}
4040

4141
// Test invalidation
42-
cache.setReleaseList("release-list", expectedList)
43-
_, found = cache.getReleaseList("release-list")
42+
cache.setReleaseList(expectedList)
43+
_, found = cache.getReleaseList()
4444
if !found {
4545
t.Error("Expected cache hit before invalidation")
4646
}
4747

4848
cache.invalidate("some-release")
49-
_, found = cache.getReleaseList("release-list")
49+
_, found = cache.getReleaseList()
5050
if found {
5151
t.Error("Expected cache miss after invalidation")
5252
}

pkg/helm/nelm/nelm.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ func (c *NelmClient) GetReleaseLabels(releaseName, labelName string) (string, er
141141
// In nelm, labels are stored as annotations - cache all of them
142142
allLabels := make(map[string]string)
143143
if releaseGetResult.Release != nil && releaseGetResult.Release.Annotations != nil {
144-
for k, v := range releaseGetResult.Release.Annotations {
145-
allLabels[k] = v
146-
}
144+
maps.Copy(allLabels, releaseGetResult.Release.Annotations)
147145
}
148146

149147
// Cache all labels for future use
@@ -312,7 +310,7 @@ func (c *NelmClient) GetReleaseValues(releaseName string) (utils.Values, error)
312310
}
313311

314312
// Cache the result
315-
c.statusCache.setValues(valuesKey, map[string]interface{}(result))
313+
c.statusCache.setValues(valuesKey, map[string]any(result))
316314
c.logger.Debug("Fetched and cached release values", slog.String("release", releaseName))
317315

318316
return result, nil
@@ -404,8 +402,7 @@ func (c *NelmClient) IsReleaseExists(releaseName string) (bool, error) {
404402

405403
func (c *NelmClient) ListReleasesNames() ([]string, error) {
406404
// Check cache first
407-
cacheKey := "release-list"
408-
if cachedList, found := c.statusCache.getReleaseList(cacheKey); found {
405+
if cachedList, found := c.statusCache.getReleaseList(); found {
409406
c.logger.Debug("Using cached release list", slog.Int("count", len(cachedList)))
410407
return cachedList, nil
411408
}
@@ -437,7 +434,7 @@ func (c *NelmClient) ListReleasesNames() ([]string, error) {
437434
sort.Strings(releaseNames)
438435

439436
// Cache the result
440-
c.statusCache.setReleaseList(cacheKey, releaseNames)
437+
c.statusCache.setReleaseList(releaseNames)
441438
c.logger.Debug("Fetched and cached release list", slog.Int("count", len(releaseNames)))
442439

443440
return releaseNames, nil

pkg/task/batch/batch.go

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

33
import (
44
"context"
5+
"maps"
56
"sort"
67
"sync"
78
"time"
@@ -16,7 +17,7 @@ import (
1617
type ModuleManagerInterface interface {
1718
GetFunctionalDependencies() map[string][]string
1819
GetCritical(moduleName string) bool
19-
GetModule(name string) interface{}
20+
GetModule(name string) any
2021
}
2122

2223
// BatchConfig defines configuration for batching operations
@@ -401,9 +402,7 @@ func (bs *BatchScheduler) GetActiveBatches() map[string]*ModuleBatch {
401402
defer bs.mu.RUnlock()
402403

403404
result := make(map[string]*ModuleBatch, len(bs.activeBatches))
404-
for id, batch := range bs.activeBatches {
405-
result[id] = batch
406-
}
405+
maps.Copy(result, bs.activeBatches)
407406

408407
return result
409408
}

pkg/task/batch/integration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ModuleManagerAdapter struct {
1717
*module_manager.ModuleManager
1818
}
1919

20-
func (a *ModuleManagerAdapter) GetModule(name string) interface{} {
20+
func (a *ModuleManagerAdapter) GetModule(name string) any {
2121
return a.ModuleManager.GetModule(name)
2222
}
2323

pkg/task/batch/optimizer.go

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

33
import (
44
"context"
5+
"slices"
56
"sync"
67
"time"
78

@@ -175,9 +176,7 @@ func (bo *BatchOptimizer) GetOptimalBatchSize(baseSize int) int {
175176
optimalSize := int(float64(baseSize) * loadFactor)
176177

177178
// Ensure reasonable bounds
178-
if optimalSize < 1 {
179-
optimalSize = 1
180-
}
179+
optimalSize = max(optimalSize, 1)
181180
if optimalSize > 10 {
182181
optimalSize = 10
183182
}
@@ -304,12 +303,7 @@ func (r *ConflictAvoidanceRule) Apply(_ context.Context, batch *ModuleBatch, sta
304303
hasConflict := false
305304
for addedModule := range addedModules {
306305
if moduleConflicts, exists := conflicts[module]; exists {
307-
for _, conflictModule := range moduleConflicts {
308-
if conflictModule == addedModule {
309-
hasConflict = true
310-
break
311-
}
312-
}
306+
hasConflict = slices.Contains(moduleConflicts, addedModule)
313307
}
314308
if hasConflict {
315309
break

0 commit comments

Comments
 (0)