Skip to content

Commit c86b5e3

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
lint and modernize
1 parent 64f32d4 commit c86b5e3

File tree

12 files changed

+93
-106
lines changed

12 files changed

+93
-106
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ linters:
186186
arguments: [10]
187187
- name: flag-parameter # fixes are difficult
188188
disabled: true
189-
- name: confusing-naming # false positives
189+
- name: bare-return
190190
disabled: true
191+
- name: confusing-naming
192+
disabled: true # cache libraries naturally use same method names (Get/Set) across types
191193

192194
rowserrcheck:
193195
# database/sql is always checked.

.yamllint

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
extends: default
3+
4+
rules:
5+
braces:
6+
max-spaces-inside: 1
7+
brackets:
8+
max-spaces-inside: 1
9+
comments: disable
10+
comments-indentation: disable
11+
document-start: disable
12+
line-length:
13+
level: warning
14+
max: 160
15+
allow-non-breakable-inline-mappings: true
16+
truthy: disable

Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ LINTERS :=
9898
FIXERS :=
9999

100100
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
101-
GOLANGCI_LINT_VERSION ?= v2.5.0
101+
GOLANGCI_LINT_VERSION ?= v2.7.2
102102
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
103103
$(GOLANGCI_LINT_BIN):
104104
mkdir -p $(LINT_ROOT)/out/linters
@@ -114,6 +114,19 @@ FIXERS += golangci-lint-fix
114114
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
115115
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" --fix \;
116116

117+
YAMLLINT_VERSION ?= 1.37.1
118+
YAMLLINT_ROOT := $(LINT_ROOT)/out/linters/yamllint-$(YAMLLINT_VERSION)
119+
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
120+
$(YAMLLINT_BIN):
121+
mkdir -p $(LINT_ROOT)/out/linters
122+
rm -rf $(LINT_ROOT)/out/linters/yamllint-*
123+
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C $(LINT_ROOT)/out/linters -zxf -
124+
cd $(YAMLLINT_ROOT) && pip3 install --target dist . || pip install --target dist .
125+
126+
LINTERS += yamllint-lint
127+
yamllint-lint: $(YAMLLINT_BIN)
128+
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
129+
117130
.PHONY: _lint $(LINTERS)
118131
_lint:
119132
@exit_code=0; \

benchmarks/benchmark_test.go

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,7 @@ func hitRateTinyLFU(keys []int, cacheSize int) float64 {
292292
}
293293

294294
func hitRateFreecache(keys []int, cacheSize int) float64 {
295-
cacheBytes := cacheSize * 24
296-
if cacheBytes < 512*1024 {
297-
cacheBytes = 512 * 1024
298-
}
295+
cacheBytes := max(cacheSize*24, 512*1024)
299296
cache := freecache.NewCache(cacheBytes)
300297
// Pre-compute keys and values to avoid conversion overhead affecting hit rate measurement
301298
precomputedKeys := make([][]byte, hitRateKeySpace)
@@ -755,9 +752,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
755752
cache.Set(i, i)
756753
}
757754
for range threads {
758-
wg.Add(1)
759-
go func() {
760-
defer wg.Done()
755+
wg.Go(func() {
761756
for i := 0; ; {
762757
for range opsBatchSize {
763758
key := keys[i%workloadLen]
@@ -773,7 +768,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
773768
return
774769
}
775770
}
776-
}()
771+
})
777772
}
778773

779774
case "otter":
@@ -782,9 +777,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
782777
cache.Set(i, i)
783778
}
784779
for range threads {
785-
wg.Add(1)
786-
go func() {
787-
defer wg.Done()
780+
wg.Go(func() {
788781
for i := 0; ; {
789782
for range opsBatchSize {
790783
key := keys[i%workloadLen]
@@ -800,7 +793,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
800793
return
801794
}
802795
}
803-
}()
796+
})
804797
}
805798

806799
case "ristretto":
@@ -814,9 +807,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
814807
}
815808
ristrettoCache.Wait()
816809
for range threads {
817-
wg.Add(1)
818-
go func() {
819-
defer wg.Done()
810+
wg.Go(func() {
820811
for i := 0; ; {
821812
for range opsBatchSize {
822813
key := keys[i%workloadLen]
@@ -832,7 +823,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
832823
return
833824
}
834825
}
835-
}()
826+
})
836827
}
837828

838829
case "lru":
@@ -841,9 +832,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
841832
cache.Add(i, i)
842833
}
843834
for range threads {
844-
wg.Add(1)
845-
go func() {
846-
defer wg.Done()
835+
wg.Go(func() {
847836
for i := 0; ; {
848837
for range opsBatchSize {
849838
key := keys[i%workloadLen]
@@ -859,7 +848,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
859848
return
860849
}
861850
}
862-
}()
851+
})
863852
}
864853

865854
case "tinylfu":
@@ -870,9 +859,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
870859
cache.Set(&tinylfu.Item{Key: keysAsStrings[i], Value: i})
871860
}
872861
for range threads {
873-
wg.Add(1)
874-
go func() {
875-
defer wg.Done()
862+
wg.Go(func() {
876863
for i := 0; ; {
877864
for range opsBatchSize {
878865
key := keys[i%workloadLen]
@@ -888,7 +875,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
888875
return
889876
}
890877
}
891-
}()
878+
})
892879
}
893880

894881
case "freecache":
@@ -902,9 +889,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
902889
cache.Set(keysAsBytes[i], vals[i], 0)
903890
}
904891
for range threads {
905-
wg.Add(1)
906-
go func() {
907-
defer wg.Done()
892+
wg.Go(func() {
908893
for i := 0; ; {
909894
for range opsBatchSize {
910895
key := keys[i%workloadLen]
@@ -920,7 +905,7 @@ func measureZipfQPS(cacheName string, threads int, keys []int) float64 {
920905
return
921906
}
922907
}
923-
}()
908+
})
924909
}
925910
}
926911

benchmarks/meta_trace_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,7 @@ func runMetaTraceTinyLFU(ops []traceOp, cacheSize int) float64 {
319319

320320
func runMetaTraceFreecache(ops []traceOp, cacheSize int) float64 {
321321
// freecache uses bytes, estimate ~32 bytes per entry (key + value + overhead)
322-
cacheBytes := cacheSize * 32
323-
if cacheBytes < 512*1024 {
324-
cacheBytes = 512 * 1024 // minimum 512KB
325-
}
322+
cacheBytes := max(cacheSize*32, 512*1024) // minimum 512KB
326323
cache := freecache.NewCache(cacheBytes)
327324

328325
var hits, misses int64

bloom.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,12 @@ func newBloomFilter(capacity int, fpRate float64) *bloomFilter {
2626
m := float64(capacity) * -math.Log(fpRate) / (ln2 * ln2)
2727

2828
// Round m up to nearest power of 2 for fast modulo
29-
mInt := nextPowerOf2(uint64(m))
30-
if mInt < 64 {
31-
mInt = 64
32-
}
29+
mInt := max(nextPowerOf2(uint64(m)), 64)
3330

3431
// Calculate k
3532
// k = (m / n) * ln(2)
36-
k := int(float64(mInt) / float64(capacity) * ln2)
37-
if k < 1 {
38-
k = 1
39-
}
40-
if k > 16 {
41-
k = 16 // Cap k to avoid too many memory accesses
42-
}
33+
// Clamp k between 1 and 16 (cap to avoid too many memory accesses)
34+
k := min(max(int(float64(mInt)/float64(capacity)*ln2), 1), 16)
4335

4436
return &bloomFilter{
4537
data: make([]uint64, mInt/64),

memory_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,11 @@ func TestMemoryCache_Concurrent(t *testing.T) {
103103

104104
// Concurrent readers
105105
for range 10 {
106-
wg.Add(1)
107-
go func() {
108-
defer wg.Done()
106+
wg.Go(func() {
109107
for j := range 100 {
110108
cache.Get(j)
111109
}
112-
}()
110+
})
113111
}
114112

115113
wg.Wait()
@@ -579,9 +577,7 @@ func TestMemoryCache_GetSet_IntKeys(t *testing.T) {
579577
// Test thundering herd with int keys (uses different flightShard path)
580578
var wg sync.WaitGroup
581579
for i := range 50 {
582-
wg.Add(1)
583-
go func() {
584-
defer wg.Done()
580+
wg.Go(func() {
585581
// All goroutines request the same key
586582
val, err := cache.GetSet(123, loader)
587583
if err != nil {
@@ -590,7 +586,7 @@ func TestMemoryCache_GetSet_IntKeys(t *testing.T) {
590586
if val != 42 {
591587
t.Errorf("GetSet value = %d; want 42", val)
592588
}
593-
}()
589+
})
594590
// Stagger slightly to ensure overlap
595591
if i%10 == 0 {
596592
time.Sleep(time.Millisecond)

persistent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewTiered[K comparable, V any](store Store[K, V], opts ...Option) (*TieredC
6464
// Get retrieves a value from the cache.
6565
// It first checks the memory cache, then falls back to persistence.
6666
//
67-
//nolint:gocritic // unnamedResult - public API signature is intentionally clear without named returns
67+
//nolint:gocritic // unnamedResult: public API signature is intentionally clear
6868
func (c *TieredCache[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
6969
// Check memory first
7070
if val, ok := c.memory.get(key); ok {

persistent_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,13 +783,11 @@ func TestTieredCache_Concurrent(t *testing.T) {
783783

784784
// Concurrent readers
785785
for range 10 {
786-
wg.Add(1)
787-
go func() {
788-
defer wg.Done()
786+
wg.Go(func() {
789787
for j := range 100 {
790788
_, _, _ = cache.Get(ctx, j) //nolint:errcheck // Test concurrent access
791789
}
792-
}()
790+
})
793791
}
794792

795793
wg.Wait()

pkg/store/null/null.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (*Store[K, V]) ValidateKey(_ K) error {
2424

2525
// Get always returns not found.
2626
//
27-
//nolint:revive // function-result-limit - required by Store interface
27+
//nolint:revive // function-result-limit: required by Store interface
2828
func (*Store[K, V]) Get(_ context.Context, _ K) (value V, expiry time.Time, found bool, err error) {
2929
var zero V
3030
return zero, time.Time{}, false, nil

0 commit comments

Comments
 (0)