Skip to content

Commit c0b6272

Browse files
authored
upgrade to xxh3 hashing pkg (#54)
1 parent 9d627fb commit c0b6272

File tree

8 files changed

+62
-65
lines changed

8 files changed

+62
-65
lines changed

_example/go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/go-chi/httprate/_example
22

3-
go 1.22.5
3+
go 1.23.0
4+
5+
toolchain go1.24.1
46

57
replace github.com/go-chi/httprate => ../
68

@@ -9,4 +11,8 @@ require (
911
github.com/go-chi/httprate v0.0.0-00010101000000-000000000000
1012
)
1113

12-
require github.com/cespare/xxhash/v2 v2.3.0 // indirect
14+
require (
15+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
16+
github.com/zeebo/xxh3 v1.0.2 // indirect
17+
golang.org/x/sys v0.30.0 // indirect
18+
)

_example/go.sum

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
2-
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
31
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
42
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
5-
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
6-
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
4+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
5+
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
6+
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
7+
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
8+
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
9+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
10+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
11+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
12+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

go.mod

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
module github.com/go-chi/httprate
22

3-
go 1.17
3+
go 1.23.0
44

5-
require github.com/cespare/xxhash/v2 v2.3.0
5+
toolchain go1.24.1
66

7-
require golang.org/x/sync v0.7.0 // indirect
7+
require github.com/zeebo/xxh3 v1.0.2
8+
9+
require golang.org/x/sys v0.30.0 // indirect
10+
11+
require (
12+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
13+
golang.org/x/sync v0.12.0
14+
)

go.sum

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
2-
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3-
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
4-
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
5-
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
6-
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
2+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
3+
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
4+
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
5+
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
6+
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
7+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
8+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
9+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
10+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

limit_key.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"fmt"
55
"time"
66

7-
"github.com/cespare/xxhash/v2"
7+
"github.com/zeebo/xxh3"
88
)
99

1010
func LimitCounterKey(key string, window time.Time) uint64 {
11-
h := xxhash.New()
11+
h := xxh3.New()
1212
h.WriteString(key)
1313
h.WriteString(fmt.Sprintf("%d", window.Unix()))
1414
return h.Sum64()

local_counter.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sync"
55
"time"
66

7-
"github.com/cespare/xxhash/v2"
7+
"github.com/zeebo/xxh3"
88
)
99

1010
// NewLocalLimitCounter creates an instance of localCounter,
@@ -71,8 +71,28 @@ func (c *localCounter) Increment(key string, currentWindow time.Time) error {
7171
return c.IncrementBy(key, currentWindow, 1)
7272
}
7373

74+
func (c *localCounter) evict(currentWindow time.Time) {
75+
if c.latestWindow == currentWindow {
76+
return
77+
}
78+
79+
previousWindow := currentWindow.Add(-c.windowLength)
80+
if c.latestWindow == previousWindow {
81+
c.latestWindow = currentWindow
82+
// Shift the windows without map re-allocation.
83+
clear(c.previousCounters)
84+
c.latestCounters, c.previousCounters = c.previousCounters, c.latestCounters
85+
return
86+
}
87+
88+
c.latestWindow = currentWindow
89+
90+
clear(c.previousCounters)
91+
clear(c.latestCounters)
92+
}
93+
7494
func limitCounterKey(key string) uint64 {
75-
h := xxhash.New()
95+
h := xxh3.New()
7696
h.WriteString(key)
7797
return h.Sum64()
7898
}

local_counter_go1.20.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

local_counter_go1.21.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)