Skip to content

Commit

Permalink
fix: update golangci-lint and fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Sep 15, 2024
1 parent 46933d6 commit 1662d47
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ jobs:
- name: lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
version: v1.61.0
args: --timeout=4m -v --out-${NO_FUTURE}format colored-line-number
8 changes: 5 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ run:
build-tags:
- integration
modules-download-mode: readonly
go: '1.17'
go: '1.19'
output:
format: tab:lint.txt
formats:
- format: tab
path: lint.txt
print-issued-lines: false
uniq-by-line: false
sort-results: true
Expand All @@ -17,7 +19,7 @@ linters:
- bidichk
- bodyclose
- contextcheck
- copyloopvar
#- copyloopvar
- durationcheck
- errcheck
- errname
Expand Down
1 change: 1 addition & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (b *Builder[K, V]) Logger(logger Logger) *Builder[K, V] {

func (b *Builder[K, V]) getMaximum() *uint64 {
if b.maximumSize != nil {
//nolint:gosec // there is no overflow
ms := uint64(*b.maximumSize)
return &ms
}
Expand Down
7 changes: 2 additions & 5 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestCache_PinnedWeight(t *testing.T) {
}
return 1
}).
WithTTL(3 * time.Second).
WithTTL(2 * time.Second).
OnDeletion(func(e DeletionEvent[int, int]) {
mutex.Lock()
m[e.Cause]++
Expand All @@ -127,9 +127,6 @@ func TestCache_PinnedWeight(t *testing.T) {
}
for i := size; i < 2*size; i++ {
c.Set(i, i)
}
time.Sleep(time.Second)
for i := size; i < 2*size; i++ {
if !c.Has(i) {
t.Fatalf("the key must exist: %d", i)
}
Expand All @@ -139,7 +136,7 @@ func TestCache_PinnedWeight(t *testing.T) {
t.Fatalf("the key must exist: %d", pinned)
}

time.Sleep(3 * time.Second)
time.Sleep(4 * time.Second)

if c.Has(pinned) {
t.Fatalf("the key must not exist: %d", pinned)
Expand Down
3 changes: 3 additions & 0 deletions internal/expiry/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (v *Variable[K, V]) findBucket(expiration uint64) node.Node[K, V] {

// Add schedules a timer event for the node.
func (v *Variable[K, V]) Add(n node.Node[K, V]) {
//nolint:gosec // there is no overflow
root := v.findBucket(uint64(n.Expiration()))
link(root, n)
}
Expand All @@ -94,6 +95,7 @@ func (v *Variable[K, V]) Delete(n node.Node[K, V]) {
}

func (v *Variable[K, V]) DeleteExpired(nowNanos int64) {
//nolint:gosec // there is no overflow
currentTime := uint64(nowNanos)
prevTime := v.time
v.time = currentTime
Expand Down Expand Up @@ -130,6 +132,7 @@ func (v *Variable[K, V]) deleteExpiredFromBucket(index int, prevTicks, delta uin
n.SetPrevExp(nil)
n.SetNextExp(nil)

//nolint:gosec // there is no overflow
if uint64(n.Expiration()) <= v.time {
v.deleteNode(n)
} else {
Expand Down
4 changes: 4 additions & 0 deletions internal/hashtable/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ type table[K comparable] struct {
}

func (t *table[K]) addSize(bucketIdx uint64, delta int) {
//nolint:gosec // there is no overflow
counterIdx := uint64(len(t.size)-1) & bucketIdx
atomic.AddInt64(&t.size[counterIdx].c, int64(delta))
}

func (t *table[K]) addSizePlain(bucketIdx uint64, delta int) {
//nolint:gosec // there is no overflow
counterIdx := uint64(len(t.size)-1) & bucketIdx
t.size[counterIdx].c += int64(delta)
}
Expand Down Expand Up @@ -159,6 +161,7 @@ func newTable[K comparable](bucketCount int, prevHasher maphash.Hasher[K]) *tabl
counterLength = maxCounterLength
}
counter := make([]paddedCounter, counterLength)
//nolint:gosec // there is no overflow
mask := uint64(len(buckets) - 1)
t := &table[K]{
buckets: buckets,
Expand Down Expand Up @@ -435,6 +438,7 @@ func (m *Map[K, V]) resize(known *table[K], hint resizeHint) {
if hint != clearHint {
for i := 0; i < tableLen; i++ {
copied := m.copyBuckets(&t.buckets[i], nt)
//nolint:gosec // there is no overflow
nt.addSizePlain(uint64(i), copied)
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

(which golangci-lint > /dev/null) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
bash -s -- -b "$(go env GOPATH)"/bin v1.60.3
bash -s -- -b "$(go env GOPATH)"/bin v1.61.0

go install github.com/daixiang0/gci@latest
GO111MODULE=on go install mvdan.cc/gofumpt@latest
Expand Down
5 changes: 5 additions & 0 deletions stats/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ func (c *Counter) Snapshot() Stats {

// RecordHits records cache hits. This should be called when a cache request returns a cached value.
func (c *Counter) RecordHits(count int) {
//nolint:gosec // there is no overflow
c.hits.Add(uint64(count))
}

// RecordMisses records cache misses. This should be called when a cache request returns a value that was not
// found in the cache.
func (c *Counter) RecordMisses(count int) {
//nolint:gosec // there is no overflow
c.misses.Add(uint64(count))
}

Expand All @@ -90,19 +92,22 @@ func (c *Counter) RecordEviction(weight uint32) {

// RecordRejections records rejections of entries. Cache rejects entries only if they have too much weight.
func (c *Counter) RecordRejections(count int) {
//nolint:gosec // there is no overflow
c.rejectedSets.Add(uint64(count))
}

// RecordLoadSuccess records the successful load of a new entry. This method should be called when a cache request
// causes an entry to be loaded and the loading completes successfully.
func (c *Counter) RecordLoadSuccess(loadTime time.Duration) {
c.loadSuccesses.Add(1)
//nolint:gosec // there is no overflow
c.totalLoadTime.Add(uint64(loadTime))
}

// RecordLoadFailure records the failed load of a new entry. This method should be called when a cache request
// causes an entry to be loaded, but the loading function returns an error.
func (c *Counter) RecordLoadFailure(loadTime time.Duration) {
c.loadFailures.Add(1)
//nolint:gosec // there is no overflow
c.totalLoadTime.Add(uint64(loadTime))
}
1 change: 0 additions & 1 deletion stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (s Stats) AverageLoadPenalty() time.Duration {
if loads > uint64(math.MaxInt64) {
return s.totalLoadTime / time.Duration(math.MaxInt64)
}
//nolint:gosec // overflow is handled above
return s.totalLoadTime / time.Duration(loads)
}

Expand Down

0 comments on commit 1662d47

Please sign in to comment.