Skip to content

Commit 062c0b2

Browse files
author
bloom
committed
improve rate limits
1 parent 26d4dfc commit 062c0b2

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/aws/aws-sdk-go-v2/service/sesv2 v1.59.1
1313
github.com/fxamacker/cbor/v2 v2.9.0
1414
github.com/go-chi/chi/v5 v5.2.4
15-
github.com/klauspost/compress v1.18.2
15+
github.com/klauspost/compress v1.18.3
1616
github.com/microcosm-cc/bluemonday v1.0.27
1717
github.com/pdfcpu/pdfcpu v0.11.1
1818
github.com/skerkour/stdx-go v0.0.0-20260115085829-3511513e653e
@@ -28,7 +28,7 @@ require (
2828
)
2929

3030
require (
31-
github.com/alecthomas/chroma/v2 v2.22.0 // indirect
31+
github.com/alecthomas/chroma/v2 v2.23.0 // indirect
3232
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 // indirect
3333
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 // indirect
3434
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
22
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
33
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
44
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
5-
github.com/alecthomas/chroma/v2 v2.22.0 h1:PqEhf+ezz5F5owoDeOUKFzW+W3ZJDShNCaHg4sZuItI=
6-
github.com/alecthomas/chroma/v2 v2.22.0/go.mod h1:NqVhfBR0lte5Ouh3DcthuUCTUpDC9cxBOfyMbMQPs3o=
5+
github.com/alecthomas/chroma/v2 v2.23.0 h1:u/Orux1J0eLuZDeQ44froV8smumheieI0EofhbyKhhk=
6+
github.com/alecthomas/chroma/v2 v2.23.0/go.mod h1:NqVhfBR0lte5Ouh3DcthuUCTUpDC9cxBOfyMbMQPs3o=
77
github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs=
88
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
99
github.com/aws/aws-sdk-go-v2 v1.41.1 h1:ABlyEARCDLN034NhxlRUSZr4l71mh+T5KAeGh6cerhU=
@@ -85,8 +85,8 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
8585
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
8686
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
8787
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
88-
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
89-
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
88+
github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=
89+
github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
9090
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
9191
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
9292
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=

pkg/ratelimit/ratelimit.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func New() *Limiter {
4343
func (l *Limiter) RateLimit(action string, actor []byte, timeBucket time.Duration, allowed uint64) bool {
4444
now := time.Now()
4545
bucketStart := now.Truncate(timeBucket)
46-
key := makeKey(action, actor, bucketStart)
46+
key := makeKey(action, actor, uint64(bucketStart.UnixNano()), uint64(timeBucket.Nanoseconds()))
4747

4848
l.mutex.Lock()
4949
defer l.mutex.Unlock()
@@ -70,7 +70,7 @@ func (l *Limiter) RateLimit(action string, actor []byte, timeBucket time.Duratio
7070
func (l *Limiter) Count(action string, actor []byte, timeBucket time.Duration) uint64 {
7171
now := time.Now()
7272
bucketStart := now.Truncate(timeBucket)
73-
key := makeKey(action, actor, bucketStart)
73+
key := makeKey(action, actor, uint64(bucketStart.UnixNano()), uint64(timeBucket.Nanoseconds()))
7474

7575
l.mutex.Lock()
7676
defer l.mutex.Unlock()
@@ -123,12 +123,14 @@ func (l *Limiter) cleanup() {
123123
}
124124
}
125125

126-
func makeKey(action string, actor []byte, bucketStart time.Time) [32]byte {
126+
func makeKey(action string, actor []byte, bucketStartNanos uint64, timeBucketNanos uint64) [32]byte {
127127
var hash [32]byte
128+
128129
hasher := blake3.New(32, nil)
129130
hasher.Write([]byte(action))
130131
hasher.Write(actor)
131-
binary.Write(hasher, binary.LittleEndian, bucketStart.UnixNano())
132+
binary.Write(hasher, binary.LittleEndian, bucketStartNanos)
133+
binary.Write(hasher, binary.LittleEndian, timeBucketNanos)
132134

133135
hasher.Sum(hash[:0])
134136
return hash

pkg/services/site/service/login.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"context"
55
"strings"
6+
"time"
67

78
"github.com/skerkour/stdx-go/crypto"
89
"github.com/skerkour/stdx-go/log/slogx"
@@ -33,6 +34,11 @@ func (service *SiteService) Login(ctx context.Context, input site.LoginInput) (r
3334
return
3435
}
3536

37+
if !service.rateLimiter.RateLimit("SiteService.Login", httpCtx.Client.IP.AsSlice(), time.Hour, 20) {
38+
err = errs.InvalidArgument("Too many requests. Please try again later.")
39+
return
40+
}
41+
3642
contact, err := service.contactsService.FindContactByEmail(ctx, service.db, website.ID, email)
3743
if err != nil {
3844
if errs.IsNotFound(err) {

0 commit comments

Comments
 (0)