From 507bc49da1ccc3b93c6bb96ae01569881388de52 Mon Sep 17 00:00:00 2001 From: maypok86 Date: Thu, 29 Aug 2024 11:31:42 +0300 Subject: [PATCH] [Chore] Add fixes for linters --- cmd/generator/main.go | 1 + internal/core/cache.go | 13 ++++++++----- internal/hashtable/map.go | 1 + internal/lossy/buffer.go | 2 ++ internal/unixtime/unixtime.go | 1 + internal/xruntime/runtime_1.22.go | 1 + internal/xruntime/xruntime.go | 2 ++ 7 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/generator/main.go b/cmd/generator/main.go index 757e18f..6ef1a44 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -152,6 +152,7 @@ func (g *generator) printStructComment() { i := 2 for _, f := range declaredFeatures { if g.features[f] { + //nolint:staticcheck // used only for unicode featureTitle := strings.Title(strings.ToLower(f.name)) g.p("//") g.p("// %d. %s", i, featureTitle) diff --git a/internal/core/cache.go b/internal/core/cache.go index 09dd9e5..bac512d 100644 --- a/internal/core/cache.go +++ b/internal/core/cache.go @@ -71,6 +71,7 @@ var ( func init() { parallelism := xruntime.Parallelism() roundedParallelism := int(xmath.RoundUpPowerOf2(parallelism)) + //nolint:gosec // there will never be an overflow maxWriteBufferSize = uint32(128 * roundedParallelism) maxStripedBufferSize = 4 * roundedParallelism } @@ -81,6 +82,7 @@ func zeroValue[V any]() V { } func getTTL(ttl time.Duration) uint32 { + //nolint:gosec // there will never be an overflow return uint32((ttl + time.Second - 1) / time.Second) } @@ -149,11 +151,12 @@ func NewCache[K comparable, V any](c Config[K, V]) *Cache[K, V] { } cache := &Cache[K, V]{ - nodeManager: nodeManager, - hashmap: hashmap, - stripedBuffer: stripedBuffer, - writeBuffer: queue.NewGrowable[task[K, V]](minWriteBufferSize, maxWriteBufferSize), - doneClear: make(chan struct{}), + nodeManager: nodeManager, + hashmap: hashmap, + stripedBuffer: stripedBuffer, + writeBuffer: queue.NewGrowable[task[K, V]](minWriteBufferSize, maxWriteBufferSize), + doneClear: make(chan struct{}), + //nolint:gosec // there will never be an overflow mask: uint32(maxStripedBufferSize - 1), costFunc: c.CostFunc, deletionListener: c.DeletionListener, diff --git a/internal/hashtable/map.go b/internal/hashtable/map.go index 2ee48a2..fec5fa6 100644 --- a/internal/hashtable/map.go +++ b/internal/hashtable/map.go @@ -142,6 +142,7 @@ func newMap[K comparable, V any](nodeManager *node.Manager[K, V], size int) *Map if size <= minNodeCount { t = newTable(minBucketCount, maphash.NewHasher[K]()) } else { + //nolint:gosec // there will never be an overflow bucketCount := xmath.RoundUpPowerOf2(uint32(size / bucketSize)) t = newTable(int(bucketCount), maphash.NewHasher[K]()) } diff --git a/internal/lossy/buffer.go b/internal/lossy/buffer.go index 6bb35a6..a0a1d55 100644 --- a/internal/lossy/buffer.go +++ b/internal/lossy/buffer.go @@ -86,6 +86,7 @@ func (b *Buffer[K, V]) Add(n node.Node[K, V]) *PolicyBuffers[K, V] { } if b.tail.CompareAndSwap(tail, tail+1) { // success + //nolint:gosec // there will never be an overflow index := int(tail & mask) atomic.StorePointer(&b.buffer[index], n.AsPointer()) if size == capacity-1 { @@ -97,6 +98,7 @@ func (b *Buffer[K, V]) Add(n node.Node[K, V]) *PolicyBuffers[K, V] { pb := (*PolicyBuffers[K, V])(b.policyBuffers) for i := 0; i < capacity; i++ { + //nolint:gosec // there will never be an overflow index := int(head & mask) v := atomic.LoadPointer(&b.buffer[index]) if v != nil { diff --git a/internal/unixtime/unixtime.go b/internal/unixtime/unixtime.go index a446921..38f7b19 100644 --- a/internal/unixtime/unixtime.go +++ b/internal/unixtime/unixtime.go @@ -42,6 +42,7 @@ func startTimer() { for { select { case t := <-ticker.C: + //nolint:gosec // there will never be an overflow atomic.StoreUint32(&now, uint32(t.Unix()-StartTime())) case <-done: return diff --git a/internal/xruntime/runtime_1.22.go b/internal/xruntime/runtime_1.22.go index acfb18b..4827a34 100644 --- a/internal/xruntime/runtime_1.22.go +++ b/internal/xruntime/runtime_1.22.go @@ -21,5 +21,6 @@ import ( ) func Fastrand() uint32 { + //nolint:gosec // we don't need a cryptographically secure random number generator return rand.Uint32() } diff --git a/internal/xruntime/xruntime.go b/internal/xruntime/xruntime.go index 6c4e486..9c5de93 100644 --- a/internal/xruntime/xruntime.go +++ b/internal/xruntime/xruntime.go @@ -25,7 +25,9 @@ const ( // Parallelism returns the maximum possible number of concurrently running goroutines. func Parallelism() uint32 { + //nolint:gosec // there will never be an overflow maxProcs := uint32(runtime.GOMAXPROCS(0)) + //nolint:gosec // there will never be an overflow numCPU := uint32(runtime.NumCPU()) if maxProcs < numCPU { return maxProcs